Followers 0

## 4 posts in this topic

Hello,

recently, I've been working on making my engine more modular, by splitting up most high-level/shader implementations of things like lighting, shadows, HDR, etc... into modules, that can (theoretically) easily be pluged/unplugged, and should later even be loaded from DLLs via a plugin manager rather than in code. Its quite a way there, but right now, I've got one big problem: What am I supposed to do with the shaders? For example, if this is my lighting shader:

#include "../../Base/Effects/Vertex.afx"

cbuffer instance : register(b2)
{
float4 cAmbientColor;
};

sampler InputSampler : register(s0);

Texture2D <float4> Material  : register(t0);

float4 mainPS(VS_OUTPUT i) : SV_Target0
{
float4 vDiffuseMaterial;
vDiffuseMaterial.rgb = Material.Sample(InputSampler, i.vTex0).rgb;
vDiffuseMaterial.rgb *= cAmbientColor.rgb;
vDiffuseMaterial.rgb *= cAmbientColor.a;

vDiffuseMaterial.a = 1.0;

return vDiffuseMaterial;
};

Say I want to make an SSAO-plugin, which isn't really that hard. Effects, textures etc.. are being loaded from a resource-config file, however, how am I supposed to apply the rendered ssao output to the ambient shader? I basically need to insert this line:

vDiffuseMaterial.rgb *= 1.0-SSAO.Sample(InputSampler, i.vTex0).r;



in the middle, alongside with the definition of the SSAO texture.

Whats a viable way to do so? I really don't have any clue how to do so, without simply shipping a seperate SSAO-enabled ambient lighting shader with the SSAO-plugin. Is there any other way? I'd prefer if it potentially could work with any lighting implementation that follows certain rules, but even if there is a way to enchance just that special shader offered by the DeferredLighting-plugin I have, I'd still be very glad. Ideas?

0

##### Share on other sites

#ifdef OPTION_04 // SSAO
vDiffuseMaterial.rgb *= 1.0-SSAO.Sample(InputSampler, i.vTex0).r;
#endif


Then you need to load a permutation of that shader with the macro "#define OPTION_04\n" prepended when your SSAO-plugin is enabled. In your config file you could specify which numbered options need to be enabled.

Edited by JackShannon
0

##### Share on other sites

It has been a long time since I touched any rendering related code, but I'll try to describe what I remember from my implementation.

Each surface shader (e.g. a shader that will be applied to the surface of a 3D model) can use one or more different plugins.

Shader plugins were implemented using Cg interfaces.

So, your example above would look something like this (pseudocode since I haven't written a line of Cg for a couple of years now).

IAmbientLighting g_AmbientLighting;
sampler2D DiffuseTex;

float4 mainPS(VS_OUTPUT i)
{
float4 diffuseColor;
diffuseColor.rgb = tex2D(DiffuseTex, i.UV.xy).rgb;
diffuseColor.rgb *= g_AmbientiLighting.CalcAmbientLight(i.PosWS.xyz);
diffuseColor.a = 1.0;

return diffuseColor;
}


The IAmbientLighting interface would look like this:

interface IAmbientLighting
{
float3 CalcAmbientLighting(float3 posWS);
}


Your current shader would have used a constant ambient color implementation. Something like:

class ConstAmbientLight : IAmbientLighting
{
float4 AmbientColor;

float3 CalcAmbientLighting(float3 posWS)
{
return AmbientColor.rgb * AmbientColor.a;
}
}


If you would like to change to an SSAO implementation, instead of using this class you would use:

class SSAO : IAmbientLighting
{
sampler2D SSAOTex;
float4 AmbientColor;
float4x4 WSToSSMatrix;

float3 CalcAmbientLighting(float3 posWS)
{
float2 screenSpacePos = TransformToSS(posWS, WSToSSMatrix);
float ssao = tex2D(SSAOTex, screenSpacePos).r;
return AmbientColor.rgb * AmbientColor.r * ssao;
}

With those two interface implementations available, the renderer is responsible for selecting the correct one at run-time, based on some criteria (user prefs, GPU caps, etc.) and linking it to all the surface shaders which use an IAmbientLighting object.

The idea can be extended to other things. E.g. different kind of lights (omni, point, directional) can be implemented as interfaces of one common ILight interface.

This way you can create (e.g.) a Phong shader with or without SSAO, using one or more lights of any type.

That's the basic idea. Hope it makes some sense. If not, just say it and I'll do my best to describe it better.

1

##### Share on other sites

Then you need to load a permutation of that shader with the macro "#define OPTION_04\n" prepended when your SSAO-plugin is enabled. In your config file you could specify which numbered options need to be enabled.

Thanks for the input, however my problem is slightly more complicated. I am well aware of shader permutations, however in my case I can't know in advance which options need to be available. Imagine someone was writing the lighting plugin, and only that; he/she wouldn't know what else someone using that plugin might need - SSAO, or even more complex global illumination models. I can't just pre-define some options to choose from, I need a way of introducing new functionality to a shader without directly modifying it.

That's the basic idea. Hope it makes some sense. If not, just say it and I'll do my best to describe it better.

Thanks, makes perfect sense, and that seems to be just what I'm looking for. A quick google-search brought me to "dynamic linkage" for HLSL 5, thats pretty much the same what you described, right? If so, is there any way to apply that to older versions of HLSL (like 3 for DX9), or would I have to use a 3rd-party shader language or unroll my own to make that happen?

0

##### Share on other sites

Thanks, makes perfect sense, and that seems to be just what I'm looking for. A quick google-search brought me to "dynamic linkage" for HLSL 5, thats pretty much the same what you described, right? If so, is there any way to apply that to older versions of HLSL (like 3 for DX9), or would I have to use a 3rd-party shader language or unroll my own to make that happen?

0

## Create an account

Register a new account