yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// d3d12-command-encoder.h 2#pragma once 3 4#include "d3d12-base.h" 5#include "d3d12-buffer.h" 6#include "d3d12-framebuffer.h" 7#include "d3d12-render-pass.h" 8#include "d3d12-submitter.h" 9 10namespace gfx 11{ 12namespace d3d12 13{ 14 15using namespace Slang ; 16 17static const Int kMaxRTVCount = 8 ; 18 19class PipelineCommandEncoder 20{ 21public : 22bool m_isOpen = false; 23bool m_bindingDirty = true; 24CommandBufferImpl * m_commandBuffer ; 25TransientResourceHeapImpl * m_transientHeap ; 26DeviceImpl * m_renderer ; 27ID3D12Device * m_device ; 28ID3D12GraphicsCommandList * m_d3dCmdList ; 29ID3D12GraphicsCommandList6 * m_d3dCmdList6 ; 30ID3D12GraphicsCommandList * m_preCmdList = nullptr ; 31 32RefPtr < PipelineStateBase > m_currentPipeline ; 33 34static int getBindPointIndex (PipelineType type ); 35 36void init (CommandBufferImpl * commandBuffer ); 37 38void endEncodingImpl () {m_isOpen = false; } 39 40Result bindPipelineImpl (IPipelineState * pipelineState ,IShaderObject ** outRootObject ); 41 42Result bindPipelineWithRootObjectImpl (IPipelineState * pipelineState ,IShaderObject * rootObject ); 43 44/// Specializes the pipeline according to current root-object argument values, 45/// applys the root object bindings and binds the pipeline state. 46/// The newly specialized pipeline is held alive by the pipeline cache so users of 47/// `newPipeline` do not need to maintain its lifespan. 48Result _bindRenderState (Submitter * submitter ,RefPtr < PipelineStateBase >& newPipeline ); 49}; 50 51class ResourceCommandEncoderImpl :public IResourceCommandEncoder ,public PipelineCommandEncoder 52{ 53public : 54virtual void * getInterface (SlangUUID const & uuid ) 55 { 56if (uuid == GfxGUID ::IID_IResourceCommandEncoder || uuid == ISlangUnknown ::getTypeGuid ()) 57return this ; 58return nullptr ; 59 } 60virtual SLANG_NO_THROW SlangResult SLANG_MCALL 61queryInterface (SlangUUID const & uuid ,void ** outObject )override 62 { 63if (auto ptr = getInterface (uuid )) 64 { 65* outObject = ptr ; 66return SLANG_OK ; 67 } 68return SLANG_E_NO_INTERFACE ; 69 } 70virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef ()override {return 1 ; } 71virtual SLANG_NO_THROW uint32_t SLANG_MCALL release ()override {return 1 ; } 72 73virtual SLANG_NO_THROW void SLANG_MCALL copyBuffer ( 74IBufferResource * dst , 75Offset dstOffset , 76IBufferResource * src , 77Offset srcOffset , 78Size size )override ; 79virtual SLANG_NO_THROW void SLANG_MCALL 80uploadBufferData (IBufferResource * dst ,Offset offset ,Size size ,void * data )override ; 81virtual SLANG_NO_THROW void SLANG_MCALL textureBarrier ( 82GfxCount count , 83ITextureResource * const * textures , 84ResourceState src , 85ResourceState dst )override ; 86virtual SLANG_NO_THROW void SLANG_MCALL bufferBarrier ( 87GfxCount count , 88IBufferResource * const * buffers , 89ResourceState src , 90ResourceState dst )override ; 91virtual SLANG_NO_THROW void SLANG_MCALL endEncoding ()override {} 92virtual SLANG_NO_THROW void SLANG_MCALL 93writeTimestamp (IQueryPool * pool ,GfxIndex index )override ; 94virtual SLANG_NO_THROW void SLANG_MCALL copyTexture ( 95ITextureResource * dst , 96ResourceState dstState , 97SubresourceRange dstSubresource , 98ITextureResource ::Offset3D dstOffset , 99ITextureResource * src , 100ResourceState srcState , 101SubresourceRange srcSubresource , 102ITextureResource ::Offset3D srcOffset , 103ITextureResource ::Extents extent )override ; 104 105virtual SLANG_NO_THROW void SLANG_MCALL uploadTextureData ( 106ITextureResource * dst , 107SubresourceRange subResourceRange , 108ITextureResource ::Offset3D offset , 109ITextureResource ::Extents extent , 110ITextureResource ::SubresourceData * subResourceData , 111GfxCount subResourceDataCount )override ; 112 113virtual SLANG_NO_THROW void SLANG_MCALL clearResourceView ( 114IResourceView * view , 115ClearValue * clearValue , 116ClearResourceViewFlags ::Enum flags )override ; 117 118virtual SLANG_NO_THROW void SLANG_MCALL resolveResource ( 119ITextureResource * source , 120ResourceState sourceState , 121SubresourceRange sourceRange , 122ITextureResource * dest , 123ResourceState destState , 124SubresourceRange destRange )override ; 125 126virtual SLANG_NO_THROW void SLANG_MCALL resolveQuery ( 127IQueryPool * queryPool , 128GfxIndex index , 129GfxCount count , 130IBufferResource * buffer , 131Offset offset )override ; 132 133virtual SLANG_NO_THROW void SLANG_MCALL copyTextureToBuffer ( 134IBufferResource * dst , 135Offset dstOffset , 136Size dstSize , 137Size dstRowStride , 138ITextureResource * src , 139ResourceState srcState , 140SubresourceRange srcSubresource , 141ITextureResource ::Offset3D srcOffset , 142ITextureResource ::Extents extent )override ; 143 144virtual SLANG_NO_THROW void SLANG_MCALL textureSubresourceBarrier ( 145ITextureResource * texture , 146SubresourceRange subresourceRange , 147ResourceState src , 148ResourceState dst )override ; 149 150virtual SLANG_NO_THROW void SLANG_MCALL 151beginDebugEvent (const char * name ,float rgbColor [3 ])override ; 152virtual SLANG_NO_THROW void SLANG_MCALL endDebugEvent ()override ; 153}; 154 155class ComputeCommandEncoderImpl :public IComputeCommandEncoder ,public ResourceCommandEncoderImpl 156{ 157public : 158SLANG_GFX_FORWARD_RESOURCE_COMMAND_ENCODER_IMPL (ResourceCommandEncoderImpl ) 159virtual void * getInterface (SlangUUID const & uuid )override 160 { 161if (uuid == GfxGUID ::IID_IComputeCommandEncoder || 162uuid == GfxGUID ::IID_IResourceCommandEncoder || uuid == ISlangUnknown ::getTypeGuid ()) 163return this ; 164return nullptr ; 165 } 166 167public : 168virtual SLANG_NO_THROW void SLANG_MCALL endEncoding ()override ; 169void init ( 170DeviceImpl * renderer, 171TransientResourceHeapImpl * transientHeap, 172CommandBufferImpl * cmdBuffer); 173 174virtual SLANG_NO_THROW Result SLANG_MCALL 175bindPipeline ( IPipelineState * state, IShaderObject ** outRootObject) override; 176 177virtual SLANG_NO_THROW Result SLANG_MCALL 178bindPipelineWithRootObject ( IPipelineState * state, IShaderObject * rootObject) override; 179 180virtual SLANG_NO_THROW Result SLANG_MCALL dispatchCompute ( int x, int y, int z) override; 181 182virtual SLANG_NO_THROW Result SLANG_MCALL 183dispatchComputeIndirect ( IBufferResource * argBuffer, Offset offset) override; 184}; 185 186struct BoundVertexBuffer 187{ 188RefPtr < BufferResourceImpl > m_buffer; 189int m_offset; 190}; 191 192class RenderCommandEncoderImpl : public IRenderCommandEncoder, public ResourceCommandEncoderImpl 193{ 194public : 195SLANG_GFX_FORWARD_RESOURCE_COMMAND_ENCODER_IMPL ( ResourceCommandEncoderImpl ) 196virtual void * getInterface ( SlangUUID const & uuid) override 197{ 198if (uuid == GfxGUID::IID_IRenderCommandEncoder || 199uuid == GfxGUID::IID_IResourceCommandEncoder || uuid == ISlangUnknown:: getTypeGuid ()) 200return this; 201return nullptr ; 202} 203 204public : 205RefPtr < RenderPassLayoutImpl > m_renderPass; 206RefPtr < FramebufferImpl > m_framebuffer; 207 208List < BoundVertexBuffer > m_boundVertexBuffers; 209 210RefPtr < BufferResourceImpl > m_boundIndexBuffer; 211 212D3D12_VIEWPORT m_viewports[kMaxRTVCount]; 213D3D12_RECT m_scissorRects[kMaxRTVCount]; 214 215DXGI_FORMAT m_boundIndexFormat; 216UINT m_boundIndexOffset; 217 218D3D12_PRIMITIVE_TOPOLOGY_TYPE m_primitiveTopologyType; 219D3D12_PRIMITIVE_TOPOLOGY m_primitiveTopology; 220 221void init ( 222DeviceImpl * renderer, 223TransientResourceHeapImpl * transientHeap, 224CommandBufferImpl * cmdBuffer, 225RenderPassLayoutImpl * renderPass, 226FramebufferImpl * framebuffer); 227 228virtual SLANG_NO_THROW Result SLANG_MCALL 229bindPipeline ( IPipelineState * state, IShaderObject ** outRootObject) override; 230 231virtual SLANG_NO_THROW Result SLANG_MCALL 232bindPipelineWithRootObject ( IPipelineState * state, IShaderObject * rootObject) override; 233 234virtual SLANG_NO_THROW void SLANG_MCALL 235setViewports ( GfxCount count, const Viewport * viewports) override; 236 237virtual SLANG_NO_THROW void SLANG_MCALL 238setScissorRects ( GfxCount count, const ScissorRect * rects) override; 239 240virtual SLANG_NO_THROW void SLANG_MCALL 241setPrimitiveTopology ( PrimitiveTopology topology) override; 242 243virtual SLANG_NO_THROW void SLANG_MCALL setVertexBuffers ( 244GfxIndex startSlot, 245GfxCount slotCount, 246IBufferResource * const * buffers, 247const Offset * offsets) override; 248 249virtual SLANG_NO_THROW void SLANG_MCALL 250setIndexBuffer ( IBufferResource * buffer, Format indexFormat, Offset offset = 0 ) override; 251 252Result prepareDraw (); 253virtual SLANG_NO_THROW Result SLANG_MCALL 254draw ( GfxCount vertexCount, GfxIndex startVertex = 0 ) override; 255virtual SLANG_NO_THROW Result SLANG_MCALL 256drawIndexed ( GfxCount indexCount, GfxIndex startIndex = 0 , GfxIndex baseVertex = 0 ) override; 257virtual SLANG_NO_THROW void SLANG_MCALL endEncoding () override ; 258 259virtual SLANG_NO_THROW void SLANG_MCALL setStencilReference ( uint32_t referenceValue) override; 260 261virtual SLANG_NO_THROW Result SLANG_MCALL drawIndirect ( 262GfxCount maxDrawCount, 263IBufferResource * argBuffer, 264Offset argOffset, 265IBufferResource * countBuffer, 266Offset countOffset) override; 267 268virtual SLANG_NO_THROW Result SLANG_MCALL drawIndexedIndirect ( 269GfxCount maxDrawCount, 270IBufferResource * argBuffer, 271Offset argOffset, 272IBufferResource * countBuffer, 273Offset countOffset) override; 274 275virtual SLANG_NO_THROW Result SLANG_MCALL setSamplePositions ( 276GfxCount samplesPerPixel, 277GfxCount pixelCount, 278const SamplePosition * samplePositions) override; 279 280virtual SLANG_NO_THROW Result SLANG_MCALL drawInstanced ( 281GfxCount vertexCount, 282GfxCount instanceCount, 283GfxIndex startVertex, 284GfxIndex startInstanceLocation) override; 285 286virtual SLANG_NO_THROW Result SLANG_MCALL drawIndexedInstanced ( 287GfxCount indexCount, 288GfxCount instanceCount, 289GfxIndex startIndexLocation, 290GfxIndex baseVertexLocation, 291GfxIndex startInstanceLocation) override; 292 293virtual SLANG_NO_THROW Result SLANG_MCALL drawMeshTasks ( int x, int y, int z) override; 294}; 295 296#if SLANG_GFX_HAS_DXR_SUPPORT 297class RayTracingCommandEncoderImpl : public IRayTracingCommandEncoder, 298public ResourceCommandEncoderImpl 299{ 300public: 301SLANG_GFX_FORWARD_RESOURCE_COMMAND_ENCODER_IMPL ( ResourceCommandEncoderImpl ) 302virtual void * getInterface ( SlangUUID const & uuid) override 303{ 304if ( uuid == GfxGUID::IID_IRayTracingCommandEncoder || 305uuid == GfxGUID::IID_IResourceCommandEncoder || uuid == ISlangUnknown:: getTypeGuid ()) 306return this; 307return nullptr; 308} 309 310public: 311virtual SLANG_NO_THROW void SLANG_MCALL buildAccelerationStructure ( 312const IAccelerationStructure ::BuildDesc & desc, 313GfxCount propertyQueryCount, 314AccelerationStructureQueryDesc * queryDescs) override; 315virtual SLANG_NO_THROW void SLANG_MCALL copyAccelerationStructure ( 316IAccelerationStructure * dest, 317IAccelerationStructure * src, 318AccelerationStructureCopyMode mode) override; 319virtual SLANG_NO_THROW void SLANG_MCALL queryAccelerationStructureProperties ( 320GfxCount accelerationStructureCount, 321IAccelerationStructure * const * accelerationStructures, 322GfxCount queryCount, 323AccelerationStructureQueryDesc * queryDescs) override; 324virtual SLANG_NO_THROW void SLANG_MCALL 325serializeAccelerationStructure ( DeviceAddress dest, IAccelerationStructure * source) override; 326virtual SLANG_NO_THROW void SLANG_MCALL 327deserializeAccelerationStructure ( IAccelerationStructure * dest, DeviceAddress source) override; 328virtual SLANG_NO_THROW Result SLANG_MCALL 329bindPipeline ( IPipelineState * state, IShaderObject ** outRootObject) override; 330virtual SLANG_NO_THROW Result SLANG_MCALL 331bindPipelineWithRootObject ( IPipelineState * state, IShaderObject * rootObject) override 332{ 333return bindPipelineWithRootObjectImpl ( state , rootObject ); 334} 335virtual SLANG_NO_THROW Result SLANG_MCALL dispatchRays ( 336GfxIndex rayGenShaderIndex, 337IShaderTable * shaderTable, 338GfxCount width, 339GfxCount height, 340GfxCount depth) override; 341virtual SLANG_NO_THROW void SLANG_MCALL endEncoding () override {} 342}; 343#endif 344 345} // namespace d3d12 346} // namespace gfx