Permalink
Browse files

Added shadow samplers and updated example 15-shadowmaps-simple to use…

… them.
  • Loading branch information...
1 parent a68ffbf commit 6eee3cca63274af17b44df2e0d8204c08bc1518e @bkaradzic committed Feb 25, 2014
Showing with 178 additions and 843 deletions.
  1. +11 −0 examples/15-shadowmaps-simple/fs_sms_mesh.sc
  2. +11 −0 examples/15-shadowmaps-simple/fs_sms_mesh_pd.sc
  3. +11 −0 examples/15-shadowmaps-simple/fs_sms_shadow.sc
  4. +20 −16 examples/15-shadowmaps-simple/{fs_smsimple_draw.sc → fs_sms_shadow.sh}
  5. +0 −1 examples/15-shadowmaps-simple/{fs_smsimple_packdepth.sc → fs_sms_shadow_pd.sc}
  6. +45 −26 examples/15-shadowmaps-simple/shadowmaps_simple.cpp
  7. 0 examples/15-shadowmaps-simple/{vs_smsimple_draw.sc → vs_sms_mesh.sc}
  8. +13 −0 examples/15-shadowmaps-simple/vs_sms_shadow.sc
  9. 0 examples/15-shadowmaps-simple/{vs_smsimple_packdepth.sc → vs_sms_shadow_pd.sc}
  10. BIN examples/runtime/shaders/dx11/fs_sms_mesh.bin
  11. BIN examples/runtime/shaders/dx11/fs_sms_mesh_pd.bin
  12. BIN examples/runtime/shaders/dx11/fs_sms_shadow.bin
  13. BIN examples/runtime/shaders/dx11/{fs_smsimple_packdepth.bin → fs_sms_shadow_pd.bin}
  14. BIN examples/runtime/shaders/dx11/fs_smsimple_draw.bin
  15. BIN examples/runtime/shaders/dx11/{vs_smsimple_draw.bin → vs_sms_mesh.bin}
  16. BIN examples/runtime/shaders/dx11/vs_sms_shadow.bin
  17. BIN examples/runtime/shaders/dx11/{vs_smsimple_packdepth.bin → vs_sms_shadow_pd.bin}
  18. BIN examples/runtime/shaders/dx9/fs_sms_mesh.bin
  19. BIN examples/runtime/shaders/dx9/fs_sms_mesh_pd.bin
  20. BIN examples/runtime/shaders/dx9/fs_sms_shadow.bin
  21. BIN examples/runtime/shaders/dx9/{fs_smsimple_packdepth.bin → fs_sms_shadow_pd.bin}
  22. BIN examples/runtime/shaders/dx9/fs_smsimple_draw.bin
  23. BIN examples/runtime/shaders/dx9/{vs_smsimple_draw.bin → vs_sms_mesh.bin}
  24. BIN examples/runtime/shaders/dx9/vs_sms_shadow.bin
  25. BIN examples/runtime/shaders/dx9/{vs_smsimple_packdepth.bin → vs_sms_shadow_pd.bin}
  26. BIN examples/runtime/shaders/gles/fs_sms_mesh.bin
  27. BIN examples/runtime/shaders/gles/fs_sms_mesh_pd.bin
  28. BIN examples/runtime/shaders/gles/fs_sms_shadow.bin
  29. BIN examples/runtime/shaders/gles/{fs_smsimple_packdepth.bin → fs_sms_shadow_pd.bin}
  30. +0 −389 examples/runtime/shaders/gles/fs_smsimple_draw.bin
  31. BIN examples/runtime/shaders/gles/{vs_smsimple_draw.bin → vs_sms_mesh.bin}
  32. BIN examples/runtime/shaders/gles/vs_sms_shadow.bin
  33. BIN examples/runtime/shaders/gles/{vs_smsimple_packdepth.bin → vs_sms_shadow_pd.bin}
  34. BIN examples/runtime/shaders/glsl/fs_sms_mesh.bin
  35. BIN examples/runtime/shaders/glsl/fs_sms_mesh_pd.bin
  36. BIN examples/runtime/shaders/glsl/fs_sms_shadow.bin
  37. BIN examples/runtime/shaders/glsl/{fs_smsimple_packdepth.bin → fs_sms_shadow_pd.bin}
  38. +0 −389 examples/runtime/shaders/glsl/fs_smsimple_draw.bin
  39. BIN examples/runtime/shaders/glsl/{vs_smsimple_draw.bin → vs_sms_mesh.bin}
  40. BIN examples/runtime/shaders/glsl/vs_sms_shadow.bin
  41. BIN examples/runtime/shaders/glsl/{vs_smsimple_packdepth.bin → vs_sms_shadow_pd.bin}
  42. +12 −4 src/bgfx_shader.sh
  43. +21 −6 src/renderer_d3d11.cpp
  44. +4 −4 src/renderer_d3d11.h
  45. +13 −8 src/renderer_gl.cpp
  46. +2 −0 src/renderer_gl.h
  47. +15 −0 tools/shaderc/shaderc.cpp
@@ -0,0 +1,11 @@
+$input v_view, v_normal, v_shadowcoord
+
+/*
+ * Copyright 2013-2014 Dario Manesku. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#include "../common/common.sh"
+
+#define SHADOW_PACKED_DEPTH 0
+#include "fs_sms_shadow.sh"
@@ -0,0 +1,11 @@
+$input v_view, v_normal, v_shadowcoord
+
+/*
+ * Copyright 2013-2014 Dario Manesku. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#include "../common/common.sh"
+
+#define SHADOW_PACKED_DEPTH 1
+#include "fs_sms_shadow.sh"
@@ -0,0 +1,11 @@
+/*
+ * Copyright 2013-2014 Dario Manesku. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#include "../common/common.sh"
+
+void main()
+{
+ gl_FragColor = vec4_splat(0.0);
+}
@@ -1,5 +1,3 @@
-$input v_view, v_normal, v_shadowcoord
-
/*
* Copyright 2013-2014 Dario Manesku. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
@@ -8,7 +6,13 @@ $input v_view, v_normal, v_shadowcoord
#include "../common/common.sh"
uniform vec4 u_lightPos;
+#if SHADOW_PACKED_DEPTH
SAMPLER2D(u_shadowMap, 4);
+# define Sampler sampler2D
+#else
+SAMPLER2DSHADOW(u_shadowMap, 4);
+# define Sampler sampler2DShadow
+#endif // SHADOW_PACKED_DEPTH
vec2 lit(vec3 _ld, vec3 _n, vec3 _vd, float _exp)
{
@@ -23,7 +27,17 @@ vec2 lit(vec3 _ld, vec3 _n, vec3 _vd, float _exp)
return max(vec2(ndotl, spec), 0.0);
}
-float hardShadow(sampler2D _sampler, vec4 _shadowCoord, float _bias)
+float hardShadow(Sampler _sampler, vec4 _shadowCoord, float _bias)
+{
+ vec3 texCoord = _shadowCoord.xyz/_shadowCoord.w;
+#if SHADOW_PACKED_DEPTH
+ return step(texCoord.z-_bias, unpackRgbaToFloat(texture2D(_sampler, texCoord.xy) ) );
+#else
+ return shadow2D(_sampler, vec3(texCoord.xy, texCoord.z-_bias) );
+#endif // SHADOW_PACKED_DEPTH
+}
+
+float PCF(Sampler _sampler, vec4 _shadowCoord, float _bias, vec2 _texelSize)
{
vec2 texCoord = _shadowCoord.xy/_shadowCoord.w;
@@ -36,15 +50,6 @@ float hardShadow(sampler2D _sampler, vec4 _shadowCoord, float _bias)
return 1.0;
}
- float receiver = (_shadowCoord.z-_bias)/_shadowCoord.w;
- float occluder = unpackRgbaToFloat(texture2D(_sampler, texCoord) );
-
- float visibility = step(receiver, occluder);
- return visibility;
-}
-
-float PCF(sampler2D _sampler, vec4 _shadowCoord, float _bias, vec2 _texelSize)
-{
float result = 0.0;
vec2 offset = _texelSize * _shadowCoord.w;
@@ -73,8 +78,8 @@ float PCF(sampler2D _sampler, vec4 _shadowCoord, float _bias, vec2 _texelSize)
void main()
{
- const float shadowMapBias = 0.005;
- const vec3 color = vec3_splat(1.0);
+ float shadowMapBias = 0.005;
+ vec3 color = vec3_splat(1.0);
vec3 v = v_view;
vec3 vd = -normalize(v);
@@ -91,6 +96,5 @@ void main()
vec3 brdf = (lc.x + lc.y) * color * visibility;
vec3 final = toGamma(abs(ambient + brdf) );
- gl_FragColor.xyz = final;
- gl_FragColor.w = 1.0;
+ gl_FragColor = vec4(final, 1.0);
}
@@ -12,4 +12,3 @@ void main()
float depth = v_position.z/v_position.w * 0.5 + 0.5;
gl_FragColor = packFloatToRgba(depth);
}
-
@@ -462,10 +462,6 @@ int _main_(int /*_argc*/, char** /*_argv*/)
bgfx::UniformHandle u_lightPos = bgfx::createUniform("u_lightPos", bgfx::UniformType::Uniform4fv);
bgfx::UniformHandle u_lightMtx = bgfx::createUniform("u_lightMtx", bgfx::UniformType::Uniform4x4fv);
- // Programs.
- bgfx::ProgramHandle progPackDepth = loadProgram("vs_smsimple_packdepth", "fs_smsimple_packdepth");
- bgfx::ProgramHandle progDraw = loadProgram("vs_smsimple_draw", "fs_smsimple_draw");
-
// Vertex declarations.
bgfx::VertexDecl PosNormalDecl;
PosNormalDecl.begin();
@@ -486,12 +482,41 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// Render targets.
uint16_t shadowMapSize = 512;
- bgfx::TextureHandle fbtextures[] =
+ // Get renderer capabilities info.
+ //const bgfx::Caps* caps = bgfx::getCaps();
+ // Shadow samplers are supported at least partially supported if texture
+ // compare less equal feature is supported.
+ bool shadowSamplerSupported = false; //0 != (caps->supported & BGFX_CAPS_TEXTURE_COMPARE_LEQUAL);
+
+ bgfx::ProgramHandle progShadow;
+ bgfx::ProgramHandle progMesh;
+
+ if (shadowSamplerSupported)
{
- bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
- bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY),
- };
- s_shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
+ // Depth textures and shadow samplers are supported.
+ progShadow = loadProgram("vs_sms_shadow", "fs_sms_shadow");
+ progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh");
+
+ bgfx::TextureHandle fbtextures[] =
+ {
+ bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_COMPARE_LEQUAL),
+ };
+ s_shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
+ }
+ else
+ {
+ // Depth textures and shadow samplers are not supported. Use float
+ // depth packing into color buffer instead.
+ progShadow = loadProgram("vs_sms_shadow_pd", "fs_sms_shadow_pd");
+ progMesh = loadProgram("vs_sms_mesh", "fs_sms_mesh_pd");
+
+ bgfx::TextureHandle fbtextures[] =
+ {
+ bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::BGRA8, BGFX_TEXTURE_RT),
+ bgfx::createTexture2D(shadowMapSize, shadowMapSize, 1, bgfx::TextureFormat::D16, BGFX_TEXTURE_RT_BUFFER_ONLY),
+ };
+ s_shadowMapFB = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true);
+ }
// Set view and projection matrices.
float view[16];
@@ -527,7 +552,7 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// Use debug font to print information about this example.
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/15-shadowmaps-simple");
- bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Shadow maps example.");
+ bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Shadow maps example (technique: %s).", shadowSamplerSupported ? "depth texture and shadow samplers" : "shadow depth packed into color texture");
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
// Setup lights.
@@ -569,11 +594,6 @@ int _main_(int /*_argc*/, char** /*_argv*/)
);
// Define matrices.
- float screenView[16];
- float screenProj[16];
- mtxIdentity(screenView);
- mtxOrtho(screenProj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f);
-
float lightView[16];
float lightProj[16];
@@ -601,7 +621,6 @@ int _main_(int /*_argc*/, char** /*_argv*/)
, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
, 0x303030ff, 1.0f, 0
);
- bgfx::submitMask(RENDER_SHADOW_PASS_BIT|RENDER_SCENE_PASS_BIT);
// Render.
float mtxShadow[16];
@@ -623,26 +642,26 @@ int _main_(int /*_argc*/, char** /*_argv*/)
// Floor.
mtxMul(lightMtx, mtxFloor, mtxShadow);
bgfx::setUniform(u_lightMtx, lightMtx);
- hplaneMesh.submit(RENDER_SCENE_PASS_ID, mtxFloor, progDraw);
- hplaneMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxFloor, progPackDepth);
+ hplaneMesh.submit(RENDER_SCENE_PASS_ID, mtxFloor, progMesh);
+ hplaneMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxFloor, progShadow);
// Bunny.
mtxMul(lightMtx, mtxBunny, mtxShadow);
bgfx::setUniform(u_lightMtx, lightMtx);
- bunnyMesh.submit(RENDER_SCENE_PASS_ID, mtxBunny, progDraw);
- bunnyMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxBunny, progPackDepth);
+ bunnyMesh.submit(RENDER_SCENE_PASS_ID, mtxBunny, progMesh);
+ bunnyMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxBunny, progShadow);
// Hollow cube.
mtxMul(lightMtx, mtxHollowcube, mtxShadow);
bgfx::setUniform(u_lightMtx, lightMtx);
- hollowcubeMesh.submit(RENDER_SCENE_PASS_ID, mtxHollowcube, progDraw);
- hollowcubeMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxHollowcube, progPackDepth);
+ hollowcubeMesh.submit(RENDER_SCENE_PASS_ID, mtxHollowcube, progMesh);
+ hollowcubeMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxHollowcube, progShadow);
// Cube.
mtxMul(lightMtx, mtxCube, mtxShadow);
bgfx::setUniform(u_lightMtx, lightMtx);
- cubeMesh.submit(RENDER_SCENE_PASS_ID, mtxCube, progDraw);
- cubeMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxCube, progPackDepth);
+ cubeMesh.submit(RENDER_SCENE_PASS_ID, mtxCube, progMesh);
+ cubeMesh.submitShadow(RENDER_SHADOW_PASS_ID, mtxCube, progShadow);
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
@@ -655,8 +674,8 @@ int _main_(int /*_argc*/, char** /*_argv*/)
hollowcubeMesh.unload();
hplaneMesh.unload();
- bgfx::destroyProgram(progPackDepth);
- bgfx::destroyProgram(progDraw);
+ bgfx::destroyProgram(progShadow);
+ bgfx::destroyProgram(progMesh);
bgfx::destroyFrameBuffer(s_shadowMapFB);
@@ -0,0 +1,13 @@
+$input a_position
+
+/*
+ * Copyright 2013-2014 Dario Manesku. All rights reserved.
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
+ */
+
+#include "../common/common.sh"
+
+void main()
+{
+ gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0) );
+}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.

0 comments on commit 6eee3cc

Please sign in to comment.