Jump to content
  • Advertisement
evelyn4you

DX11 SharpDX ShaderReflection; get ShaderByteCode from EffectPass

Recommended Posts

Hello,

i want to use

- fxc  Effect compiler to compile my Fx Files with, Techniques, Passes, Shaders to ByteCode  => no Problem

- SharpDX ShaderReflection to get attributes of every used Shader to generate Constant Buffer Structures an other things

- to use ShaderReflection  i have to feed the ShaderByteCode of every single Shader i want to operate with.

I was succesfull to iterate through the Effect for the Techniques, an iterate through the Techniques for the Passes an the Descriptions

I struggled a lot but cannot find out where is the connection to get the ShaderByteCode of the Shaders that are used by a EffectPass ??

Here is the anser of

AlexandreMutel

And even then, you can still use the FX file format - the difference is you'd have an offline compiler tool that parses the format for techniq

ues/passes in order to acquire the necessary shader profiles + entry points. So with the profile/entry point in hand, you can run the FX file through the D3DCompiler to get a ShaderByteCode object for each shader, then use that to create a reflection object to query all your meta data. Then write out the reflected meta data to a file, that gets consumed by your application at runtime - which would be your own implementation of Effects11 (or something completely different, either way...you use the meta data to automatically setup your constant buffers, bind resources, and manage the shader pipeline by directly using the Direct3D11 shader interfaces).

How do i get the pofile/entry point at hand ??

Please give me some help

Share this post


Link to post
Share on other sites
Advertisement

 Hello,

after much, much searching the internet and reading of the Directx 11Documentation on the Microsoft Server i could eventually

find the solution.

In the following code fragment we read the compiled shaderbytecode of a effect.
For easy understanding we get one Technik, then the pass and so on.
The iteration of the techniques and passes is no problem.
The problem was how to get the shaderbytecode from the EffectPass.

This is important because the ShaderReflection is only working per ShaderbyteCode of only one Shader.

I really hope that someone finds this code helpful. I had to spend much time to find this solution.

Now why this all ? From the given infos i can easily, automatically create all my ConstantBuffer Structures and inputlayouts and many other things.

           SharpDX.Direct3D11.Effect                      a_Effect           = new SharpDX.Direct3D11.Effect(Gl.Device, File.ReadAllBytes(filename),0);
           SharpDX.Direct3D11.EffectTechnique             a_technique        = a_Effect.GetTechniqueByIndex(0);
           SharpDX.Direct3D11.EffectPass                  a_pass             = a_technique.GetPassByIndex(0);
           SharpDX.Direct3D11.EffectPassShaderDescription a_pass_shader_desc = a_pass.VertexShaderDescription;
           SharpDX.Direct3D11.EffectShaderVariable        a_Effect_SVar      = a_pass_shader_desc.Variable ;
           SharpDX.Direct3D11.VertexShader                a_VertexShader     = a_Effect_SVar.GetVertexShader(0);
           SharpDX.Direct3D11.EffectShaderDescription     a_Effect_SDesc     = a_Effect_SVar.GetShaderDescription(0);
           SharpDX.D3DCompiler.ShaderBytecode             a_ShaderByteCode   = a_Effect_SDesc.Bytecode;
           
           // Now we can apply ShaderReflection per ShaderByteCode
           var SR = new SharpDX.D3DCompiler.ShaderReflection(a_Effect_SDesc.Bytecode);
           
           string []   cb_name_array = new string[100];
 
           // for example Loop the Constantbuffer
           for (int i = 0; i < SR.Description.ConstantBuffers; i++)
           {
               SharpDX.D3DCompiler.ConstantBuffer cb = SR.GetConstantBuffer(i);
 
               // loop the Variables of the ConstantBuffer
               for (int j = 0; j < cb.Description.VariableCount; j++)
               {
                   SharpDX.D3DCompiler.ShaderReflectionVariable variable = cb.GetVariable(j);
 
                   cb_name_array[j] = variable.Description.Name;
               }
           }

 

 

 

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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!