Sign in to follow this  
Pyrogame

DX11 [Solved] DX11 (SlimDX) - Multithreaded rendering setShaderValue problem

Recommended Posts

Pyrogame    106
First of all, I'm using SlimDX in C#. I'm rendering (or trying to ^^) my new awesome game (6 quads â 4 vertices!) in a few threads. I do this this way (pseudo-code):
foreach(m as material) {
  shader.setResource(m);
  foreach(i as indexbuffer[material]) {
     deferredContext.renderIndexed(i);
  }
}
Then I render this in an other thread by creating the command list from the deferred context and executing this on the immediate context. It renders well. But maybe, you see my problem already: Because the call "setResource" on the shader is not deferred, if the immediate context executes the command list, it uses the last state of the shader (ofcourse). How I can manage this? Is there something ready I can use? Or had the DX team forget to add this feature to their cool new DX11? ^^ [Edited by - Pyrogame on February 9, 2010 5:27:35 PM]

Share this post


Link to post
Share on other sites
MJP    19790
In D3D11 you set shader resources using the device context (DeviceContext.PixelShader.SetShaderResource in SlimDX). So if you're using a deferred context, the setting of the resource is deferred and the state change could happen in the right order.

What is "shader.setResource" doing?

Share this post


Link to post
Share on other sites
Pyrogame    106
"shader.setResource" is doing this:

effectVariable.AsResource().SetResource((ShaderResourceView)value);


I have an effect file (.fx) wich contains some global values, for example the view matrix. Then I set a value by its name (vMatrix = myMatrix). I do not know (or I want not to pay attention), which shader uses this value. Is there something deferred I can use for the whole effect? Or may I set the value for all shaders? Or can I investigate, which shaders need the value? Or is the way of using effects in Dx11 the wrong one and I have to separate the whole shaders in my C# code?

And if I have to set the values for each shader separately, then the method "SetShaderResource()" needs a slot number. What is that? Have I to count the number of each value, or something like this?

Share this post


Link to post
Share on other sites
MJP    19790
Hmmm...I'm not sure how Effects11 handles deferred contexts. I would think that this would be a case they handle, but I'd have to check through the docs and maybe the source code.

EDIT: Looking through the docs, it looks like an Effect just keeps all of its state internally and then applies it to a particular device context when you call Apply().

Quote:
Original post by Pyrogame
And if I have to set the values for each shader separately, then the method "SetShaderResource()" needs a slot number. What is that? Have I to count the number of each value, or something like this?


Textures, buffers, and samplers and mapped to specific registers in a particular shader. The index of that register specifies what slot you would need to pass to SetShaderResource. Typically when you don't use the effects framework you manually assign the register, so that you know which slot you have to pass. If you do use the effects framework, it handles the mapping register mapping for you so that you don't have to worry about it.

Share this post


Link to post
Share on other sites
Pyrogame    106
Quote:
Original post by MJP
EDIT: Looking through the docs, it looks like an Effect just keeps all of its state internally and then applies it to a particular device context when you call Apply().


Yes, but this is the core problem. I do not known, how I can set the state of an Effect in a deferred context. The problem is:

1) Set Effect A variable p to 1,0,0.
2) Effect.Apply(deferredContext)
3) render(deferredContext)
4) Set Effect A variable p to 0,1,0.
5) Effect.Apply(deferredContext)
6) render(deferredContext)
7) deferredContext.getCommandList().execute(immediateContext)

On step 7, the effects variable p is set to 0,1,0. The command list calls then:
1) Effect.Apply(immediateContext)
2) render(deferredContext)
3) Effect.Apply(immediateContext)
4) render(deferredContext)

It does not set the effect variable p to 1,0,0 before executing the first apply, because this assignment to the effect is not stored on the deferred context.

I think, if we couldn't get this working, that this may be a big design isue on the DX11-Framework. But I can't imagine, that the DX-DevTeam had forget such big thing. There must be something, I can do. The last thing I want to do, is, to create my own command list (list of delegates), and then call to execute it in the same way I'm calling the deferredContext command list execution.

Quote:
Original post by MJP
Textures, buffers, and samplers and mapped to specific registers in a particular shader. The index of that register specifies what slot you would need to pass to SetShaderResource. Typically when you don't use the effects framework you manually assign the register, so that you know which slot you have to pass. If you do use the effects framework, it handles the mapping register mapping for you so that you don't have to worry about it.

Thank you for that info. But how I can distribute the registers on my resources? Every resource needs a separate register? Are there some types of registers or are they unified?... But you will understand me, if I say: no no no, I want to use the effect framework, which should be the solution for exactly this problem of managing registers etc. :)

Share this post


Link to post
Share on other sites
hornet1990    100
I think you need a copy of the Effect for each thread/deferred context. AIUI from the documentation you should create the Effect and then use its Clone method to create a copy for each deferred context that you have (which should be one per thread).

That way they'll all share the same actual shaders but will have unique register state. Then when you call Apply on the effect you pass in the deferred context. So you end up with something like:

[Thread1]
1) Set ClonedEffect A variable p to 1,0,0.
2) ClonedEffect.Apply(deferredContext)
3) render(deferredContext)
4) Set ClonedEffect A variable p to 0,1,0.
5) ClonedEffect.Apply(deferredContext)
6) render(deferredContext)
7) deferredContext.getCommandList()

[Thread2]
1) OrigEffect.Apply(immediateContext)
2) render(immediateContext)
3) OrigEffect.Apply(immediateContext)
4) render(immediateContext)

Somewhere in that lot you need the immediateContext to execute the command list from the deferredContext (preferrably in Thread2). What you had with

deferredContext.getCommandList().execute(immediateContext)

and

3) Effect.Apply(immediateContext)
4) render(deferredContext)

doesn't make much sense unless you've got some method of stopping the execution from stomping all over the immediateContexts state - I mean what would happen if thread2 did step 1, then at that point thread2 did your step 7 execute and made state changes to the immediateContext that then affect thread2's step 2...

You'd probably be better of with:

[Thread1]
... Generate command list
send to thread3

[Thread2]
... Generate command list
send to thread3

[Thread3]
immediateContext.Execute(first_recieved_list)
immediateContext.Execute(next_recieved_list)

Thats my understanding anyway - I've not got to that point yet so I may be wrong! :)

[Edited by - hornet1990 on February 9, 2010 2:02:17 PM]

Share this post


Link to post
Share on other sites
MJP    19790
Indeed I believe that what hornet suggests is the intended way to handle effects across different device contexts. This way you're not modifying state in one thread that's shared in another.

Quote:
Original post by Pyrogame
Thank you for that info. But how I can distribute the registers on my resources? Every resource needs a separate register? Are there some types of registers or are they unified?... But you will understand me, if I say: no no no, I want to use the effect framework, which should be the solution for exactly this problem of managing registers etc. :)


You can find the low-level documentations for these things here, including a list of the registers I'm talking about. Unfortunately it's a little patchy and you have to look through a few sections to find out what the registers are for, so I'll give you a quick list:

r# - temporary registers - used for storing operands and results of math ops
x# - indexable temp. registers - same as above, except you can index into them using the value of an r# register
v# - vertex inputs - you map your vertex input streams to these registers
t# - resource registers - you map your texture resources to these registers
s# - sampler registers - you map your sampler states to these registers, and then specify one of these when you want to sample a texture resource
cb# - constant buffer registers - you map your constant buffers to these
icb# - immediate constant buffer register - you assign "hard-coded" values to this constant buffer, instead of mapping a buffer in your app

For any single shader you would just need to make sure than any particular register is only mapped once. So if you had two textures, you would probably just assign them to t0 and t1. A different shader could then use t0 and t1 afterwards.

I wasn't suggesting that you shouldn't use the effect framework...for anything non-trivial you'll definitely want to have a wrapper over the low-level stuff so that you don't have to explicitly manage registers and slot numbers. I was just trying to help you understand what's going on under the hood, because it's useful knowledge for when you're debugging problems like the one you're having.

Share this post


Link to post
Share on other sites
Pyrogame    106
Thank you for your comments :) I've found the problem.

@hornet1990:
Yes, I do it like you posted it on your last passage, except that I do not send the command lists to a 3rd thread. I render simultaneously on every cpu-core, and on the end I create the command list from what I rendered (on the same thread), and render this on the real device using the immediate context. This way, all threads can render parallel, but on the end all have to use the hardware, so only one thread can execute at time. I always have to wait, until all rendering threads are ready, so this might be the right thing I do (I think so ^^).

@MJP
You are the best! I had called the apply method, but only once per pass on the begining. But I have to call this after a change to the shader state ofcourse (like setting the texture) and before the render() method.

Problem solved. Thank you very much for your help, both of you! Sorry for bother you with this "bug" of my own.

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  

  • Partner Spotlight

  • Similar Content

    • By RubenRS
      How do i open an image to use it as Texture2D information without D3DX11CreateShaderResourceViewFromFile? And how it works for different formats like (JPG, PNG, BMP, DDS,  etc.)?
      I have an (512 x 512) image with font letters, also i have the position and texcoord of every letter. The main idea is that i want to obtain the image pixel info, use the position and texcoords to create a new texture with one letter and render it. Or am I wrong in something?
    • By thmfrnk
      Hey,
      I found a very interesting blog post here: https://bartwronski.com/2017/04/13/cull-that-cone/
      However, I didn't really got how to use his "TestConeVsSphere" test in 3D (last piece of code on his post). I have the frustumCorners of a 2D Tile cell in ViewSpace and my 3D Cone Origin and Direction, so where to place the "testSphere"? I thought about to also move the Cone into viewspace and put the sphere to the Center of the Cell with the radius of half-cellsize, however what about depth? A sphere does not have inf depth?
      I am missing anything? Any Ideas?
      Thx, Thomas
    • By Modymek
      hi all
      I want to enable and disable shader in MPCH Media player Classic
      the MPCH have shader option using HLSL shaders
      I want the shader to read each file extension before it plays the file
      so if the video file name is video.GR.Mp4 it will play it in Grayscale shader 
      if it is not and standard file name Video.Mp4 without GR. unique extension so it plays standard without shader or end the shader
      here is the shader I have for grayscale
      // $MinimumShaderProfile: ps_2_0
      sampler s0 : register(s0);
      float4 main(float2 tex : TEXCOORD0) : COLOR {
          float c0 = dot(tex2D(s0, tex), float4(0.299, 0.587, 0.114, 0));
          return c0;
      }
       
      I want to add if or block stantement or bloean to detect file name before it call the shader in order to go to the procedure or disable it or goto end direct without it
       
      any thoughts or help
    • By noodleBowl
      I've gotten to part in my DirectX 11 project where I need to pass the MVP matrices to my vertex shader. And I'm a little lost when it comes to the use of the constant buffer with the vertex shader
      I understand I need to set up the constant buffer just like any other buffer:
      1. Create a buffer description with the D3D11_BIND_CONSTANT_BUFFER flag 2. Map my matrix data into the constant buffer 3. Use VSSetConstantBuffers to actually use the buffer But I get lost at the VertexShader part, how does my vertex shader know to use this constant buffer when we get to the shader side of things
      In the example I'm following I see they have this as their vertex shader, but I don't understand how the shader knows to use the MatrixBuffer cbuffer. They just use the members directly. What if there was multiple cbuffer declarations like the Microsoft documentation says you could have?
      //Inside vertex shader cbuffer MatrixBuffer { matrix worldMatrix; matrix viewMatrix; matrix projectionMatrix; }; struct VertexInputType { float4 position : POSITION; float4 color : COLOR; }; struct PixelInputType { float4 position : SV_POSITION; float4 color : COLOR; }; PixelInputType ColorVertexShader(VertexInputType input) { PixelInputType output; // Change the position vector to be 4 units for proper matrix calculations. input.position.w = 1.0f; // Calculate the position of the vertex against the world, view, and projection matrices. output.position = mul(input.position, worldMatrix); output.position = mul(output.position, viewMatrix); output.position = mul(output.position, projectionMatrix); // Store the input color for the pixel shader to use. output.color = input.color; return output; }  
    • By gomidas
      I am trying to add normal map to my project I have an example of a cube: 
      I have normal in my shader I think. Then I set shader resource view for texture (NOT BUMP)
                  device.ImmediateContext.PixelShader.SetShaderResource(0, textureView);             device.ImmediateContext.Draw(VerticesCount,0); What should I do to set my normal map or how it is done in dx11 generally example c++?
  • Popular Now