News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Shattering shaders with the real Slim Shady

Started by Caché GB, August 12, 2018, 10:34:47 PM

Previous topic - Next topic

Caché GB

Hello everyone.

Files with .cso are precompiled hlsl shaders generated by Visual Studio from the .hlsl file. When you add a .hlsl file to your project the HLSL Compiler
becomes available. Right click on the .hlsl file and select properties.

   HLSL Compiler -> General -> 'select' All Configurations
                            -> Entrypoint Name -> 'set this to the name of your entry proc'
                            -> Shader Type -> Vertex Shader (/vs) OR Pixel Shader (/ps) OR ...
                            -> Shader Model -> Shader Model 4.1 (/4_1)
Now very important:

   HLSL Compiler -> Output Files -> 'change this' $(OutDir)%(Filename).cso 'to this' $(Directory)%(Filename).cso

Remember to keep the Vertex Shader and Pixel Shader in separate files.

You can now use the the code below....


.code

Note: vsFilename and psFilename are pointers to Unicode strings.

    local  pShaderBlob:LPID3D10Blob
    Local  lpBuff:ptr dword
    Local  dwValue:dword


       invoke  D3DReadFileToBlob, vsFilename, addr pShaderBlob
       cocall  pShaderBlob::ID3D10Blob.GetBufferPointer
          mov  lpBuff, eax
       cocall  pShaderBlob::ID3D10Blob.GetBufferSize
          mov  dwValue, eax
       cocall  pDevice::ID3D11Device.CreateVertexShader, lpBuff, dwValue, null, &[edi].m_VertexShader
         test  eax, eax
           jz   @F
         push  offset szAppFail
          jmp  Error_Message
        @@:

       invoke  D3DReadFileToBlob, psFilename, addr pShaderBlob
       cocall  pShaderBlob::ID3D10Blob.GetBufferPointer
          mov  lpBuff, eax
       cocall  pShaderBlob::ID3D10Blob.GetBufferSize
          mov  dwValue, eax
       cocall  pDevice::ID3D11Device.CreatePixelShader, lpBuff, dwValue, null, &[edi].m_PixelShader
         test  eax, eax
           jz   @F
         push  offset szAppFail
          jmp  Error_Message
        @@:



Now for the real fun.

" ====== CODE TO COMPILE SHADERS ====== "


include d3dx11.inc
includelib d3dx11.lib

.data

    szShaderFail             db  "Compile Shader Fail", 0

    szFilename1              db  "light_vs.hlsl", 0
    szVS                     db  "LightVertexShader", 0   ;; Entrypoint Name
    szVS4_1                  db  "vs_4_1", 0              ;; Note: Older laptops with onboard graphics
                                                          ;;       MAY not suport shader model "xx_5_0"
    szFilename2              db  "light_ps.hlsl", 0
    szPS                     db  "LightPixelShader", 0    ;; Entrypoint Name
    szPS4_1                  db  "ps_4_1", 0

.code

    local  pShaderBlob:LPID3D10Blob
    local  pErrorBlob:LPID3D10Blob
    Local  lpBuff:ptr dword
    Local  dwValue:dword
    local  hFile:ptr
    local  dwBytesRead:dword


       invoke  D3DX11CompileFromFileA, addr szFilename1, 0, 0, addr szVS, addr szVS4_1, \
               D3DCOMPILE_ENABLE_STRICTNESS, 0, null, addr pShaderBlob, addr pErrorBlob, null
         test  eax, eax
           jz   @F
         push  offset szShaderFail
          jmp  Error_Message
        @@:

       cocall  pShaderBlob::ID3D10Blob.GetBufferPointer
          mov  lpBuff, eax
       cocall  pShaderBlob::ID3D10Blob.GetBufferSize
          mov  dwValue, eax

       invoke  CreateFileA, CSTR("Light_VS.shd"), GENERIC_WRITE or GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
          mov  hFile, eax
       invoke  SetFilePointer, hFile, 0, null, FILE_BEGIN
          mov  dwOffsetLow, eax
          mov  dwBytesRead, 0

       invoke  LockFile, hFile, dwOffsetLow, 0, DWORD, 0    ;; sizeof(DWORD)
       invoke  WriteFile, hFile, addr dwValue, DWORD, addr dwBytesRead, null
       invoke  UnlockFile, hFile, dwOffsetLow, 0, DWORD, 0

       invoke  LockFile, hFile, DWORD, 0, dwValue, 0
       invoke  WriteFile, hFile, lpBuff, dwValue, addr dwBytesRead, null
       invoke  UnlockFile, hFile, DWORD, 0, dwValue, 0
       invoke  CloseHandle, hFile



Note: this can also be done with D3DWriteBlobToFile but that's a story for another day.
     

" ====== CODE TO LOAD COMPILED SHADERS ====== "

CLightShader_InitializeShader proc uses edi lpThis:ptr CLightShader, pDevice:ptr ID3D11Device, vsFilename:ptr word, psFilename:ptr word

    local  Layout[3]:D3D11_INPUT_ELEMENT_DESC
    local  SamplerDesc:D3D11_SAMPLER_DESC
    local  BufferDesc:D3D11_BUFFER_DESC
    local  pShaderBlob:LPID3D10Blob
    local  NumElements:dword
    Local  lpBuff:ptr dword
    Local  dwValue:dword
    local  hFile:ptr
    local  dwBytesRead:dword


          mov  NumElements, LENGTHOF Layout
          mov  eax, D3D11_INPUT_ELEMENT_DESC
         imul  eax, NumElements

       invoke  RtlZeroMemory, addr Layout, eax
          lea  edx, Layout
       assume  edx:ptr D3D11_INPUT_ELEMENT_DESC
          mov [edx].SemanticName, offset szSemanticPosition            ;; "POSITION"
          mov [edx].Format, DXGI_FORMAT_R32G32B32_FLOAT
          mov [edx].AlignedByteOffset, 0                               ;; 0
          mov [edx].InputSlotClass, D3D11_INPUT_PER_VERTEX_DATA

          add  edx, TYPE D3D11_INPUT_ELEMENT_DESC
          mov [edx].SemanticName, offset szSemanticTexcoord            ;; "TEXCOORD"
          mov [edx].Format, DXGI_FORMAT_R32G32_FLOAT
          mov [edx].AlignedByteOffset, D3D11_APPEND_ALIGNED_ELEMENT    ;; 12
          mov [edx].InputSlotClass, D3D11_INPUT_PER_VERTEX_DATA

          add  edx, TYPE D3D11_INPUT_ELEMENT_DESC
          mov [edx].SemanticName, offset szSemanticNormal              ;; "NORMAL"
          mov [edx].Format, DXGI_FORMAT_R32G32B32_FLOAT
          mov [edx].AlignedByteOffset, D3D11_APPEND_ALIGNED_ELEMENT    ;; 20
          mov [edx].InputSlotClass, D3D11_INPUT_PER_VERTEX_DATA
       assume  edx:nothing

          mov  BufferDesc.Usage, D3D11_USAGE_DYNAMIC      ;; use D3D11_USAGE_DEFAULT when loading with -»UpdateSubresource
          mov  BufferDesc.ByteWidth, MATRIXBUFFERTYPE     ;; sizeof(MATRIXBUFFERTYPE)
          mov  BufferDesc.BindFlags, D3D11_BIND_CONSTANT_BUFFER
          mov  BufferDesc.CPUAccessFlags, D3D11_CPU_ACCESS_WRITE
          mov  BufferDesc.MiscFlags, 0
          mov  BufferDesc.StructureByteStride, 0

          mov  edi, lpThis
       assume  edi:ptr CLightShader

       invoke  CreateFile, vsFilename, GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
          mov  hFile, eax
       invoke  SetFilePointer, hFile, 0, null, FILE_BEGIN
          mov  dwBytesRead, 0
       invoke  ReadFile, hFile, addr dwValue, DWORD, addr dwBytesRead, null

       invoke  D3DCreateBlob, dwValue, addr pShaderBlob
       cocall  pShaderBlob::ID3D10Blob.GetBufferPointer
          mov  lpBuff, eax

       invoke  ReadFile, hFile, lpBuff, dwValue, addr dwBytesRead, null
       invoke  CloseHandle, hFile

       cocall  pDevice::ID3D11Device.CreateVertexShader, lpBuff, dwValue, null, &[edi].m_VertexShader
         test  eax, eax
           jz   @F
         push  offset szShaderFail
          jmp  Error_Message
        @@:

       cocall  pDevice::ID3D11Device.CreateInputLayout, &Layout, NumElements, lpBuff, dwValue, &[edi].m_VertexLayout
         test  eax, eax
           jz   @F
         push  offset szAppFail
          jmp  Error_Message
        @@:

       cocall  pShaderBlob::ID3D10Blob.Release ;; release AFTER -»CreateInputLayout or u will crash n' burn

       invoke  CreateFile, psFilename, GENERIC_READ, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
          mov  hFile, eax
       invoke  SetFilePointer, hFile, 0, null, FILE_BEGIN
          mov  dwBytesRead, 0
       invoke  ReadFile, hFile, addr dwValue, DWORD, addr dwBytesRead, null

       invoke  D3DCreateBlob, dwValue, addr pShaderBlob
       cocall  pShaderBlob::ID3D10Blob.GetBufferPointer
          mov  lpBuff, eax

       invoke  ReadFile, hFile, lpBuff, dwValue, addr dwBytesRead, null
       invoke  CloseHandle, hFile

       cocall  pDevice::ID3D11Device.CreatePixelShader, lpBuff, dwValue, null, &[edi].m_PixelShader
         test  eax, eax
           jz   @F
         push  offset szAppFail
          jmp  Error_Message
        @@:

       cocall  pShaderBlob::ID3D10Blob.Release

        .....

       assume  edi:nothing
          ret

CLightShader_InitializeShader endp


And now your app is "Windows Store apps" politically correct using D3DCreateBlob.

Enjoy.....

Caché GB's 1 and 0-nly language:MASM