ddf8196 / MaterialBinTool

MCBE RenderDragon着色器解包/打包/编译工具
MIT License
108 stars 12 forks source link

Uniforms do not work in fragment shaders (Direct3D) #5

Closed SIsilicon closed 1 year ago

SIsilicon commented 1 year ago

Whenever I attempt to use a uniform in the fragment shader, it acts as if all its values are zero (never set). After digging into the Direct3D shader assembly, I notice that the one Minecraft has uses CB1 constant buffer in the fragment shader ( dcl_constantbuffer CB1[1], immediateIndexed), while the one made when compiling uses CB0 ( dcl_constantbuffer CB0[2], immediateIndexed). This same constant buffer is used in the Vertex shaders. Maybe that has something to do with it?

ddf8196 commented 1 year ago

Yes, it looks like they are using the cb0 register in the vertex shader and the cb1 register in the fragment shader

ddf8196 commented 1 year ago
#if BGFX_SHADER_LANGUAGE_HLSL >= 400 && BGFX_SHADER_TYPE_FRAGMENT
cbuffer __placeholder__ : REGISTER(b, 0) {};
#endif

You can add this code to the front of the shader (or in the header file) to occupy the cb0 register in the fragment shader, so that the uniform variables are automatically assigned to the cb1 register

SIsilicon commented 1 year ago

Would this be a solution or workaround? Will it be added to bgfx_shader.sh by default in the next release?

ddf8196 commented 1 year ago

This is a workaround. Actually it's not quite right, but it works. This code will be added to bgfx_shader.sh in the next release, but I'm still looking for a better solution.