I currently have a scenario where I am rendering terrain with a shadow map. I have two passes, one for the depth buffer to create the shadow map (which is rendered to a texture) and a second that takes the shadow map texture as input and actually shadows the terrain, the terrain gets drawn once for each pass. Within the second shader I also texture the terrain.

I now want to split the second shader so that shadows and texturing are separate shaders. How do I go about getting the output from the shadow shader to the texture shader (or vice versa), or do I simply do it in the same way as I have with the shadows (rendering to a texture, passing it as an input).

  • \$\begingroup\$ Why do you want to split the shader? I sense there might be a different problem and solution here \$\endgroup\$
    – Kromster
    Jan 21 '16 at 17:59
  • \$\begingroup\$ Currently, the texturing I also do in the shader applies some texture blending. I want to render an object with the shadow shader, but do not want to texture it in the same way. \$\endgroup\$
    – onemandan
    Jan 22 '16 at 12:46

I have solved this same problem (need to render the same geometry with simpler shaders for shadow mapping) in 2 ways:

  1. Wrap shader code sections into #IFDEF directives, so that on compile time I could enable or disable them, thus constructing 2 shaders from single source. (I mostly use it to enable-disable effects on-the-fly)

  2. Copy-paste the shader code and manually rip off everything for "light" version. In my case that was viable cos I have just a couple of shaders that got simplified to gl_Position = M*V*P*Position; and gl_FragColor = vec4(0,0,0,0);

In both cases, do not forget to also instrument your shader preparation code to assign only existing uniforms and attributes.


HLSL in Direc3D 11 has a feature called Class Instances, in which you can dynamically plug in what functionality should be used in a particular draw call. You keep the implementation in the shader file and query out the location via reflection. The instances are specified as an array to your SetShader call.

The MSDN page that outlines this is Instances and Classes and seems to rely on reflection to find the implementation and the binding points to attach the references to the code.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.