yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1#pragma once 2 3#include "core/slang-basic.h" 4#include "renderer-shared.h" 5#include "slang-com-ptr.h" 6#include "slang-gfx.h" 7 8namespace gfx 9{ 10 11enum class CommandName 12{ 13SetPipelineState , 14BindRootShaderObject , 15SetFramebuffer , 16ClearFrame , 17SetViewports , 18SetScissorRects , 19SetPrimitiveTopology , 20SetVertexBuffers , 21SetIndexBuffer , 22Draw , 23DrawIndexed , 24DrawInstanced , 25DrawIndexedInstanced , 26SetStencilReference , 27DispatchCompute , 28UploadBufferData , 29CopyBuffer , 30WriteTimestamp , 31}; 32 33const uint8_t kMaxCommandOperands = 5 ; 34 35struct Command 36{ 37CommandName name ; 38uint32_t operands [kMaxCommandOperands ]; 39Command ()= default ; 40Command (CommandName inName ,uint32_t op ) 41 :name (inName ) 42 { 43operands [0 ]= op ; 44 } 45Command (CommandName inName ,uint32_t op1 ,uint32_t op2 ) 46 :name (inName ) 47 { 48operands [0 ]= op1 ; 49operands [1 ]= op2 ; 50 } 51Command (CommandName inName ,uint32_t op1 ,uint32_t op2 ,uint32_t op3 ) 52 :name (inName ) 53 { 54operands [0 ]= op1 ; 55operands [1 ]= op2 ; 56operands [2 ]= op3 ; 57 } 58Command (CommandName inName ,uint32_t op1 ,uint32_t op2 ,uint32_t op3 ,uint32_t op4 ) 59 :name (inName ) 60 { 61operands [0 ]= op1 ; 62operands [1 ]= op2 ; 63operands [2 ]= op3 ; 64operands [3 ]= op4 ; 65 } 66Command ( 67CommandName inName , 68uint32_t op1 , 69uint32_t op2 , 70uint32_t op3 , 71uint32_t op4 , 72uint32_t op5 ) 73 :name (inName ) 74 { 75operands [0 ]= op1 ; 76operands [1 ]= op2 ; 77operands [2 ]= op3 ; 78operands [3 ]= op4 ; 79operands [4 ]= op5 ; 80 } 81}; 82 83class CommandWriter 84{ 85public : 86Slang ::List < Command > m_commands ; 87Slang ::List < Slang ::RefPtr < Slang ::RefObject >>m_objects ; 88Slang ::List < uint8_t > m_data ; 89bool m_hasWriteTimestamps = false; 90 91public : 92void clear () 93 { 94m_commands .clear (); 95for (auto& obj :m_objects ) 96obj = nullptr ; 97m_objects .clear (); 98m_data .clear (); 99m_hasWriteTimestamps = false; 100 } 101 102// Copies user data into `m_data` buffer and returns the offset to retrieve the data. 103Offset encodeData (const void * data ,Size size ) 104 { 105Offset offset = (Offset )m_data .getCount (); 106m_data .setCount (m_data .getCount ()+ size ); 107memcpy (m_data .getBuffer ()+ offset ,data ,size ); 108return offset ; 109 } 110 111Offset encodeObject (Slang ::RefObject * obj ) 112 { 113Offset offset = (Offset )m_objects .getCount (); 114m_objects .add (obj ); 115return offset ; 116 } 117 118template < typename T > 119T * getObject (uint32_t offset ) 120 { 121return static_cast < T *> (m_objects [offset ].Ptr ()); 122 } 123 124template < typename T > 125T * getData (Offset offset ) 126 { 127return reinterpret_cast < T *> (m_data .getBuffer ()+ offset ); 128 } 129 130void setPipelineState (IPipelineState * state ) 131 { 132 autooffset = encodeObject (static_cast < PipelineStateBase *> (state )); 133m_commands .add (Command (CommandName ::SetPipelineState , (uint32_t )offset )); 134 } 135 136void bindRootShaderObject (IShaderObject * object ) 137 { 138 autorootOffset = encodeObject (static_cast < ShaderObjectBase *> (object )); 139m_commands .add (Command (CommandName ::BindRootShaderObject , (uint32_t )rootOffset )); 140 } 141 142void uploadBufferData (IBufferResource * buffer ,Offset offset ,Size size ,void * data ) 143 { 144 autobufferOffset = encodeObject (static_cast < BufferResource *> (buffer )); 145 autodataOffset = encodeData (data ,size ); 146m_commands .add (Command ( 147CommandName ::UploadBufferData , 148 (uint32_t )bufferOffset , 149 (uint32_t )offset , 150 (uint32_t )size , 151 (uint32_t )dataOffset )); 152 } 153 154void copyBuffer ( 155IBufferResource * dst , 156Offset dstOffset , 157IBufferResource * src , 158Offset srcOffset , 159Size size ) 160 { 161 autodstBuffer = encodeObject (static_cast < BufferResource *> (dst )); 162 autosrcBuffer = encodeObject (static_cast < BufferResource *> (src )); 163m_commands .add (Command ( 164CommandName ::CopyBuffer , 165 (uint32_t )dstBuffer , 166 (uint32_t )dstOffset , 167 (uint32_t )srcBuffer , 168 (uint32_t )srcOffset , 169 (uint32_t )size )); 170 } 171 172void setFramebuffer (IFramebuffer * frameBuffer ) 173 { 174 autoframebufferOffset = encodeObject (static_cast < FramebufferBase *> (frameBuffer )); 175m_commands .add (Command (CommandName ::SetFramebuffer , (uint32_t )framebufferOffset )); 176 } 177 178void clearFrame (uint32_t colorBufferMask ,bool clearDepth ,bool clearStencil ) 179 { 180m_commands .add (Command ( 181CommandName ::ClearFrame , 182colorBufferMask , 183clearDepth ?1 :0 , 184clearStencil ?1 :0 )); 185 } 186 187void setViewports (GfxCount count ,const Viewport * viewports ) 188 { 189 autooffset = encodeData (viewports ,sizeof (Viewport )* count ); 190m_commands .add (Command (CommandName ::SetViewports , (uint32_t )count , (uint32_t )offset )); 191 } 192 193void setScissorRects (GfxCount count ,const ScissorRect * scissors ) 194 { 195 autooffset = encodeData (scissors ,sizeof (ScissorRect )* count ); 196m_commands .add (Command (CommandName ::SetScissorRects , (uint32_t )count , (uint32_t )offset )); 197 } 198 199void setPrimitiveTopology (PrimitiveTopology topology ) 200 { 201m_commands .add (Command (CommandName ::SetPrimitiveTopology , (uint32_t )topology )); 202 } 203 204void setVertexBuffers ( 205GfxIndex startSlot , 206GfxCount slotCount , 207IBufferResource * const * buffers , 208const Offset * offsets ) 209 { 210Offset bufferOffset = 0 ; 211for (GfxCount i = 0 ;i < slotCount ;i ++ ) 212 { 213 autooffset = encodeObject (static_cast < BufferResource *> (buffers [i ])); 214if (i == 0 ) 215bufferOffset = offset ; 216 } 217 autooffsetsOffset = encodeData (offsets ,sizeof (Size )* slotCount ); 218m_commands .add (Command ( 219CommandName ::SetVertexBuffers , 220 (uint32_t )startSlot , 221 (uint32_t )slotCount , 222 (uint32_t )bufferOffset , 223 (uint32_t )offsetsOffset )); 224 } 225 226void setIndexBuffer (IBufferResource * buffer ,Format indexFormat ,Offset offset ) 227 { 228 autobufferOffset = encodeObject (static_cast < BufferResource *> (buffer )); 229m_commands .add (Command ( 230CommandName ::SetIndexBuffer , 231 (uint32_t )bufferOffset , 232 (uint32_t )indexFormat , 233 (uint32_t )offset )); 234 } 235 236void draw (GfxCount vertexCount ,GfxIndex startVertex ) 237 { 238m_commands .add (Command (CommandName ::Draw , (uint32_t )vertexCount , (uint32_t )startVertex )); 239 } 240 241void drawIndexed (GfxCount indexCount ,GfxIndex startIndex ,GfxIndex baseVertex ) 242 { 243m_commands .add (Command ( 244CommandName ::DrawIndexed , 245 (uint32_t )indexCount , 246 (uint32_t )startIndex , 247 (uint32_t )baseVertex )); 248 } 249 250void drawInstanced ( 251GfxCount vertexCount , 252GfxCount instanceCount , 253GfxIndex startVertex , 254GfxIndex startInstanceLocation ) 255 { 256m_commands .add (Command ( 257CommandName ::DrawInstanced , 258 (uint32_t )vertexCount , 259 (uint32_t )instanceCount , 260 (uint32_t )startVertex , 261 (uint32_t )startInstanceLocation )); 262 } 263 264void drawIndexedInstanced ( 265GfxCount indexCount , 266GfxCount instanceCount , 267GfxIndex startIndexLocation , 268GfxIndex baseVertexLocation , 269GfxIndex startInstanceLocation ) 270 { 271m_commands .add (Command ( 272CommandName ::DrawIndexedInstanced , 273 (uint32_t )indexCount , 274 (uint32_t )instanceCount , 275 (uint32_t )startIndexLocation , 276 (uint32_t )baseVertexLocation , 277 (uint32_t )startInstanceLocation )); 278 } 279 280void setStencilReference (uint32_t referenceValue ) 281 { 282m_commands .add (Command (CommandName ::SetStencilReference ,referenceValue )); 283 } 284 285void dispatchCompute (int x ,int y ,int z ) 286 { 287m_commands .add ( 288Command (CommandName ::DispatchCompute , (uint32_t )x , (uint32_t )y , (uint32_t )z )); 289 } 290 291void writeTimestamp (IQueryPool * pool ,GfxIndex index ) 292 { 293 autopoolOffset = encodeObject (static_cast < QueryPoolBase *> (pool )); 294m_commands .add (Command (CommandName ::WriteTimestamp , (uint32_t )poolOffset , (uint32_t )index )); 295m_hasWriteTimestamps = true; 296 } 297}; 298}// namespace gfx