Understanding Shader Programming

人盡茶涼 提交于 2019-12-06 05:54:40

1-As you can see from the struct's name, it is not the input but the output of the vertex shader. Vertex shader should output position variable as 4 floats(homogeneus coordinates). So somewhere in the shader file, there should be an operation which expands the vector to a float4 variable(something like "float4(inputPos, 1.0);").

2-It's probably an alignment issue. GPU's are designed to work with 4D vectors. While using constant buffers, try to create your structures with matrices first, 4D variables second, 3D variables third, and so on. Or you can add extra unused padding bytes like you said you did. If you have too much non-4D vectors, you can pack them in one slot with 'packoffset' keyword for not wasting GPU registers. Detailed explanation is here: http://msdn.microsoft.com/en-us/library/windows/desktop/bb509581(v=vs.85).aspx

  1. SimpleVertex is the C++ side structure defining the input vertex layout. VS_OUTPUT is the hlsl side structure defining the vertex shader output / pixel shader input. The layout of SimpleVertex corresponds to the vertex shader input which is the arguments to the vertex shader function VS(float4 Pos : POSITION, float4 Color: COLOR). In D3D11 you use an ID3D11InputLayout object (on the C++ side) to describe how the input vertex layout (SimpleVertex structure) should be bound to the vertex shader inputs. If you search in the C++ source code for the tutorial for InputLayout you will see where this is created.
  2. You need to account for alignment with constant buffers. Constant buffer members are aligned on float4 boundaries but C++ structs are not so when you use an XMFLOAT3 for direction your XMFLOAT4 color doesn't line up with the layout your constant buffer defines in hlsl.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!