• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Juliean

Shader "plugin" system?

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 this post


Link to post
Share on other sites

In your ambient shader have something like:

#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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

 

The only other option I know of is to break your shaders into components written in your own meta language, usually just bits of HLSL or Cg with extra syntax to describe interfaces and what have you.  Once all your plugs-in are loaded the shader components can have the HLSL or Cg extracted and pasted together to form full shaders programs which can then be compiled.  Essentially this is the same as dynamic shader linkage but it's done at a higher level and by application code.  It sounds like a pain to get working but it's not too bad.  The primary headache (in my experience) is detecting when a permutation of components will form shader code that won't compile and spitting out an intelligent error so whoever knows what shader component is causing the problem and how to fix it.  Printing the compilation error works okay for a while until the code that builds the end game shader code starts doing sophisticated things like renaming variable or other direct modifications to the pure HLSL or Cg bits.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0