Merge pull request #659 from lunkhound/d3d-proj-fix

removed z coord adjustment in shadow2D() shader builtin for HLSL
This commit is contained in:
Branimir Karadžić 2016-01-18 19:01:53 -08:00
commit c47216a32c
10 changed files with 19 additions and 8 deletions

View file

@ -7,8 +7,12 @@ $input v_position
#include "../common/common.sh" #include "../common/common.sh"
uniform vec4 u_depthScaleOffset; // for GL, map depth values into [0, 1] range
#define u_depthScale u_depthScaleOffset.x
#define u_depthOffset u_depthScaleOffset.y
void main() void main()
{ {
float depth = v_position.z/v_position.w * 0.5 + 0.5; float depth = v_position.z/v_position.w * u_depthScale + u_depthOffset;
gl_FragColor = packFloatToRgba(depth); gl_FragColor = packFloatToRgba(depth);
} }

View file

@ -91,6 +91,13 @@ int _main_(int _argc, char** _argv)
bgfx::UniformHandle u_shadowMap = bgfx::createUniform("u_shadowMap", bgfx::UniformType::Int1); bgfx::UniformHandle u_shadowMap = bgfx::createUniform("u_shadowMap", bgfx::UniformType::Int1);
bgfx::UniformHandle u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Vec4); bgfx::UniformHandle u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Vec4);
bgfx::UniformHandle u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Mat4); bgfx::UniformHandle u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Mat4);
// When using GL clip space depth range [-1, 1] and packing depth into color buffer, we need to
// adjust the depth range to be [0, 1] for writing to the color buffer
bgfx::UniformHandle u_depthScaleOffset = bgfx::createUniform("u_depthScaleOffset", bgfx::UniformType::Vec4);
const float depthScale = flipV ? 0.5f : 1.0f;
const float depthOffset = flipV ? 0.5f : 0.0f;
float depthScaleOffset[4] = {depthScale, depthOffset, 0.0f, 0.0f};
bgfx::setUniform(u_depthScaleOffset, depthScaleOffset);
// Vertex declarations. // Vertex declarations.
bgfx::VertexDecl PosNormalDecl; bgfx::VertexDecl PosNormalDecl;
@ -303,8 +310,8 @@ int _main_(int _argc, char** _argv)
{ {
0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f,
0.0f, sy, 0.0f, 0.0f, 0.0f, sy, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, depthScale, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.5f, 0.5f, depthOffset, 1.0f,
}; };
float mtxTmp[16]; float mtxTmp[16];

View file

@ -75,13 +75,13 @@ struct BgfxSampler2DShadow
float bgfxShadow2D(BgfxSampler2DShadow _sampler, vec3 _coord) float bgfxShadow2D(BgfxSampler2DShadow _sampler, vec3 _coord)
{ {
return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, _coord.xy, _coord.z * 2.0 - 1.0); return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, _coord.xy, _coord.z);
} }
float bgfxShadow2DProj(BgfxSampler2DShadow _sampler, vec4 _coord) float bgfxShadow2DProj(BgfxSampler2DShadow _sampler, vec4 _coord)
{ {
vec3 coord = _coord.xyz * rcp(_coord.w); vec3 coord = _coord.xyz * rcp(_coord.w);
return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, coord.xy, coord.z * 2.0 - 1.0); return _sampler.m_texture.SampleCmpLevelZero(_sampler.m_sampler, coord.xy, coord.z);
} }
struct BgfxSampler3D struct BgfxSampler3D
@ -196,9 +196,9 @@ float bgfxShadow2D(sampler2DShadow _sampler, vec3 _coord)
{ {
#if 0 #if 0
float occluder = tex2D(_sampler, _coord.xy).x; float occluder = tex2D(_sampler, _coord.xy).x;
return step(_coord.z * 2.0 - 1.0, occluder); return step(_coord.z, occluder);
#else #else
return tex2Dproj(_sampler, vec4(_coord.xy, _coord.z * 2.0 - 1.0, 1.0) ).x; return tex2Dproj(_sampler, vec4(_coord.xy, _coord.z, 1.0) ).x;
#endif // 0 #endif // 0
} }
@ -207,7 +207,7 @@ float bgfxShadow2DProj(sampler2DShadow _sampler, vec4 _coord)
#if 0 #if 0
vec3 coord = _coord.xyz * rcp(_coord.w); vec3 coord = _coord.xyz * rcp(_coord.w);
float occluder = tex2D(_sampler, coord.xy).x; float occluder = tex2D(_sampler, coord.xy).x;
return step(coord.z * 2.0 - 1.0, occluder); return step(coord.z, occluder);
#else #else
return tex2Dproj(_sampler, _coord).x; return tex2Dproj(_sampler, _coord).x;
#endif // 0 #endif // 0