• Advertisement
Sign in to follow this  

SharpDX.Toolkit EffectPass implementation question

This topic is 988 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 am trying to understand EffectPass implementation from SharpDX.Toolkit, which is now deprecated.

The most hard part to understand is ApplyInternal() and Unapply() methods.

 

As I understand, in those methos we iterate via shader stages and set/clear data for each stage.

 

But when I start implementing this, I faced with the issue that CommonShaderStage class has no those parameters, which were earlier when effects framework were written.

So, now I need to set not pointers, but real classes like Buffer, Sampler, ShaderResourceView and other.

Here is example of old implemetation:

// ----------------------------------------------
         // Iterate on each stage to setup all inputs
         // ----------------------------------------------
         for (int stageIndex = 0; stageIndex < pipeline.Stages.Length; stageIndex++)
         {
            var stageBlock = pipeline.Stages[stageIndex];
            if (stageBlock == null)
            {
               continue;
            }

            var shaderStage = stageBlock.ShaderStage;

            // ----------------------------------------------
            // Setup the shader for this stage.
            // ----------------------------------------------               
            shaderStage.SetShader(stageBlock.Shader, null, 0);
            

            // If Shader is a null shader, then skip further processing
            if (stageBlock.Index < 0)
            {
               continue;
            }

            var mergerStage = pipeline.OutputMergerStage;

            // ----------------------------------------------
            // Setup Constant Buffers
            // ----------------------------------------------

            // Upload all constant buffers to the GPU if they have been modified.
            for (int i = 0; i < stageBlock.ConstantBufferLinks.Length; i++)
            {
               var constantBufferLink = stageBlock.ConstantBufferLinks[i];
               var constantBuffer = constantBufferLink.ConstantBuffer;
               if (constantBuffer.IsDirty)
               {
                  constantBuffers[constantBufferLink.ResourceIndex].SetData(Effect.GraphicsDevice, new DataPointer(constantBuffer.BackingBuffer.DataPointer, constantBuffer.BackingBuffer.Size));
                  constantBuffer.IsDirty = false;
               }
            }

            var localLink = stageBlock.ConstantBufferSlotLinks;
            pLinks = localLink.Links;
            
            for (int i = 0; i < localLink.Count; i++)
            {
               shaderStage.SetConstantBuffers(pLinks->SlotIndex, pLinks->SlotCount, pLinks->Pointer);
               pLinks++;
            }

            // ----------------------------------------------
            // Setup ShaderResourceView
            // ----------------------------------------------
            localLink = stageBlock.ShaderResourceViewSlotLinks;
            pLinks = localLink.Links;
            for (int i = 0; i < localLink.Count; i++)
            {
               shaderStage.SetShaderResources(pLinks->SlotIndex, pLinks->SlotCount, pLinks->Pointer);
               pLinks++;
            }

            // ----------------------------------------------
            // Setup UnorderedAccessView
            // ----------------------------------------------
            localLink = stageBlock.UnorderedAccessViewSlotLinks;
            pLinks = localLink.Links;

            if (stageBlock.Type == EffectShaderType.Compute)
            {
               for (int i = 0; i < localLink.Count; i++)
               {
                  shaderStage.SetUnorderedAccessViews(pLinks->SlotIndex, pLinks->SlotCount, pLinks->Pointer, pLinks->UavInitialCount);
                  //shaderStage.Set
                  pLinks++;
               }
            }
            else
            {
               // Otherwise, for OutputMergerStage.
               for (int i = 0; i < localLink.Count; i++)
               {
                  mergerStage.SetUnorderedAccessViewsKeepRTV(pLinks->SlotIndex, pLinks->SlotCount, pLinks->Pointer, pLinks->UavInitialCount);
                  pLinks++;
               }
            }

            // ----------------------------------------------
            // Setup SamplerStates
            // ----------------------------------------------
            localLink = stageBlock.SamplerStateSlotLinks;
            pLinks = localLink.Links;
            for (int i = 0; i < localLink.Count; i++)
            {
               shaderStage.SetSamplers(pLinks->SlotIndex, pLinks->SlotCount, pLinks->Pointer);
               
               pLinks++;
            }
         }

I mark as bold code, which is not actual with latest SharpDX dlls.

 

For now I must apply real values to each stage. For ex. shaderStage.SetSamplers(SlotIndex, samplers);

 

But issue is that I cannot understand how to get all needed data to apply them to corresponding stages.

 

Each pass has EffectPass.Pass with properties, but I am not sure this is what I need.

 

Does anyone familiar with this implementation or at least can give me a hint how to solve this issue?

Share this post


Link to post
Share on other sites
Advertisement

Not exactly sure about your question, but this code was done to optimize the resource bindings so that if you have multiple resources that are consecutive in terms of slots: for example instead of setting a shader resource for each slot 0, 1, 2, 3 we could squash them into a single call 0-3.

 

The part that is making the slot allocation is actually the one that is complicated, otherwise the code above is pretty standard, though it uses directly pointers (that are setup by the allocator), instead of using plain method like `SetShaderResource(int slot, ShaderResource resource)` in purpose because we wanted to be able to batch/squash multiple consecutive resource bindings.

 

Hope it clarifies a bit...

Share this post


Link to post
Share on other sites

OK, seems I get it work. Not 100% sure that I did it optimal, but it works.

I found that resourceLinker contains all needed data and I am using it now instead of pointers

Edited by BlackJoker

Share this post


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

  • Advertisement