Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Passing Variables to Pixel Shader using Shader class

This topic is 898 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to create a class that pass variables to Pixel Shader in a similar way to ID3DXEffect::SetFloat, but using DirectX 11 and my own class
class ShaderClass
{
public:
    ShaderClass(const char* shaderFile);
    ~ShaderClass();
    void SetFloat4(...);
    void SetFloat3(...);
    void SetFloat(...);
};
 
I want to be able to do something like this in DirectX 11 and HLSL:
ShaderClass* Shader = new ShaderClass("pixelshader.fx");

// In a shader that handle fog effect:
Shader->SetFloat4("fogColor", fogColor);

OR

// In a shader that handle lighting
Shader->SetFloat3("lightPos", lightPos);
Shader->SetFloat3("lightColor", lightColor);
So instead of writing code to create constant buffer every time I want to pass variables to Pixel Shader, I just use Shader->SetFloat3() and ShaderClass should handle that.
 
How can I accomplish that?
Edited by Medo3337

Share this post


Link to post
Share on other sites
Advertisement

Hi,

 

Just offhand your class can have either it's own data struct matching the constant buffer or a pointer to a global one, then when you use the shader just call UpdateSubresource().

 

Something like this:

 
// generic buffer
 
struct ConstBuffer
{
float4 data0;
float4 data1;
};
 
// create the d3d11 const buffer
 
ID3D11Buffer* d3d_const_buffer;
 
D3D11_BUFFER_DESC desc;
ZeroMemory(&bd, sizeof(desc));
 
desc.Usage = D3D11_USAGE_DEFAULT;
desc.ByteWidth = sizeof(ConstBuffer);
desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
desc.CPUAccessFlags = 0;
 
hResult = d3d_device->CreateBuffer(&desc, nullptr, &d3d_const_buffer);
 
// now make it class-friendy
 
class Shader
{
// local buffer
ConstBuffer const_buffer;
 
// easy functions to fill in the local buffer
void SetLightPos(float4 v) {const_buffer.data0 = v;}
void SetLightColour(float4 v) {const_buffer.data1 = v;}
};
 
// use the shader when rendering
 
Shader light_shader;
light_shader.SetLightPos(vLightPos);
light_shader.SetLightColour(vLightColour);
 
d3d_context->UpdateSubresource(d3d_const_buffer, 0, nullptr, &light_shader.const_buffer, 0, 0);
 

Is that what you mean ?

Edited by Endemoniada

Share this post


Link to post
Share on other sites

@Endemoniada:

 

1. I don't know how many variables will be assigned to ShaderClass, because ShaderClass can be used by too many different Pixel Shaders and any future Pixel Shaders that will be written in the future, so the following will not work:

struct ConstBuffer
{
float4 data0;
float4 data1;
};

 

2. I want to use something similar to Shader->SetFloat4(VarName, Value) and NOT light_shader.SetLightPos(), ShaderClass is just a class to handle any shader file, the class will not know what kind of variables will be passed, so basically, I want to pass the variables by name as the following;

Shader->SetFloat4("lightPos", lightPos, sizeof(lightPos));
Edited by Medo3337

Share this post


Link to post
Share on other sites
I don't know how many variables will be assigned to ShaderClass, because ShaderClass can be used by too many different Pixel Shaders and any future Pixel Shaders that will be written in the future, so the following will not work

 

 

You might be interested in checking something called shader reflection. Basically it allows you to poll in your C/C++ Code the data types, and structures the shader you, or someone else wrote requires utilizing the D3D API

 

As far as X number of variables required by Y number shaders; the way I've found around that is something akin to Variadic function arguments (Something it seems you are already utilizing in your .h file). Though, utilizing the old style of variadic parameter passing is generally considered bad practice due to suspended type checking. I believe this is the reason most people defer to using Variadic Templates.

 

More along the lines of being generally inquisitive; are you building an engine, or a game? It seems the functionality your trying to add to your shader class would be more akin to an engine than a game. 

 

Marcus

Edited by markypooch

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!