Jump to content

  • Log In with Google      Sign In   
  • Create Account


Read Shaders from CPU in DirectX 11


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 fs1   Members   -  Reputation: 299

Like
0Likes
Like

Posted 23 December 2013 - 03:10 PM

I am reading the Constant Buffer to get the World View Projection Matrix through the VSGetConstantBuffers function prior to a DrawIndexed call.

That is working fine.

Can I map all shaders variables into CPU variables? Similar to how the VSGetConstantBuffers works? If so, how?

In addition, is there any API to debug the shader? Like the windows debug API?

Thanks

Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 7812

Like
0Likes
Like

Posted 23 December 2013 - 04:01 PM

Why would you even need to read them back?  In order to get them into the cbuffer, you must have had them somewhere CPU-side to begin with.  Cache and use that copy instead.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#3 fs1   Members   -  Reputation: 299

Like
0Likes
Like

Posted 23 December 2013 - 04:12 PM

I am doing a sprite model extractor.

I don't have them side to side. That's the point, although I have access to the shader source.

#4 fs1   Members   -  Reputation: 299

Like
0Likes
Like

Posted 24 December 2013 - 08:27 AM

Is Shader Reflection the way to go?

Any takes?

#5 Migi0027   Crossbones+   -  Reputation: 1607

Like
0Likes
Like

Posted 24 December 2013 - 09:12 AM

Tell us more about the process. huh.png

 

So when you say sprite model extractor do you mean like a 2d sprite, rendering to the screen, and then you somehow want to generate a model out of that? (Vertices... Indices...)?

 

And you said you're reading the "Constant Buffer", on the cpu side to get the variables. There I have to fully agree with mhagain, if you've written to them, you must have the data you mapped into them on the cpu side somewhere, maybe try to save them. And if I'm not mistaken, it sounds like (Maybe I'm wrong) you're trying to change some variables inside the constant buffer on the gpu side, see the problem with that is that it's impossible (Please correct me if I'm wrong), and that's the reason why they were named 'constant' buffers.

 

One way that you could get variables from the gpu is somehow writing them to the output, could be a render target, then on the cpu side extract the variables from the render target and process them there. You could even use a structured buffer, send the structure to the Gpu, and then you may edit (write to) this buffer, and you can write to any index of it. (It's usually an array).

 

PS. GPU to CPU operations aren't usually the fastest. smile.png

 

But give us some more info, then we could get closer to the solution.

 

EDIT:

"In addition, is there any API to debug the shader? Like the windows debug API?"

There is, Intel GPA is very good at this (well, it works for me), another option may be to enable the debug flag when creating the shader, then later on in either visual graphics debugger or pix, find the shader object, click on it, then somewhere you'll be able to open it and even sometimes debug it (This is possible with the visual graphics debugger, not sure about pix though).

 

Hope this helps somehow. wink.png

-MIGI0027


Edited by Migi0027, 24 December 2013 - 09:15 AM.

Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to kill everyone you meet, ohh, AND STEAL ALL ZE TRIANGLES FROM ZHEM!

#6 fs1   Members   -  Reputation: 299

Like
0Likes
Like

Posted 24 December 2013 - 09:55 AM

Migi0027 thanks for your response.

What I am trying to do is a research project to extract sprites (primitives and textures) from a certain DX11 engine.

The problem is that I am trying to do this not knowing the code, so I may have to hook some functions.

I can decompile the shaders and also I have access to the shader constants with the DirectX 11 API (for example the VSGetConstantBuffers function).

My question is how can I map the shader defined variables and structures into my CPU variables/structures (which I will have to mirror somehow)?

One way would be manually re-write the shader definitions in my CPU and then access the shader and assign them. Maybe doing this by the Shader Reflection APIs?

Any tips would be appreciated.

Thanks

#7 Samith   Members   -  Reputation: 2168

Like
1Likes
Like

Posted 24 December 2013 - 10:06 AM

One way would be manually re-writte the shader definitions in my CPU and then access the shader and assign them. I guess another way would be by Shader Reflection?


Yes, those are basically your only options. The DirectX shader reflection interface is kind of cumbersome, but it will get what you need. You won't be able to map the shader objects directly into C++ objects, but you'll at least be able to create a data structure that represents the layout and format the shader's constant buffers. You can use that layout structure to interpret the constant buffer data you have.

Note, however, that shader reflection will only get you the layouts and bindings of constant buffers/resources. You can't get the CONTENT of the constant buffers from shader reflection (though I'm pretty sure you're not trying to do that?)

#8 fs1   Members   -  Reputation: 299

Like
0Likes
Like

Posted 24 December 2013 - 10:12 AM

Note, however, that shader reflection will only get you the layouts and bindings of constant buffers/resources. You can't get the CONTENT of the constant buffers from shader reflection (though I'm pretty sure you're not trying to do that?)


Thanks Samith for your insights. I will need the Content as well. Can you get its content as well? Let's say with the VSGetConstantBuffers type of functions?

#9 Migi0027   Crossbones+   -  Reputation: 1607

Like
1Likes
Like

Posted 24 December 2013 - 11:30 AM

Please be aware that my knowledge on this is very limited, though it seems to work for me. So if you see anything wrong or complete nonsense, please tell me so I don't send the wrong message to fs1. wacko.png

 

If you already know the layout of the buffer, like:

struct CBUFFER
{
    D3DXMATRIX view, proj, world;
};

Maybe you could create a staging buffer with the same size as the origional one. Then with the staging, do something like this to copy the data:

DeviceContext->CopyResource(staging, buffer);

And remember that this staging buffer has different parameters than the original one, it should include this (the creation flags):

bufferDesc.Usage = D3D11_USAGE_STAGING;
bufferDesc.BindFlags = 0;
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;

So when you've copied the data from the original buffer to the staging, you may begin reading from it:

D3D11_MAPPED_SUBRESOURCE ms;
engine.devcon->Map((ID3D11Buffer*)staging, NULL, D3D11_MAP_READ, NULL, &ms);

CBUFFER cbData;
memcpy(&cbData, ms.pData, sizeof(cbData)); // I think the syntax looks somewhat like this

engine.devcon->Unmap((ID3D11Buffer*)staging, NULL);  

Now you have an instance of the data which you should be able to read from ( the cbData ).

Now this may not work if you don't know the layout/structure of the buffer, in that case I'll have to think a bit more. tongue.png

 

Hope this is useful!

-MIGI0027


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to kill everyone you meet, ohh, AND STEAL ALL ZE TRIANGLES FROM ZHEM!

#10 fs1   Members   -  Reputation: 299

Like
0Likes
Like

Posted 24 December 2013 - 06:44 PM

Please be aware that my knowledge on this is very limited, though it seems to work for me. So if you see anything wrong or complete nonsense, please tell me so I don't send the wrong message to fs1. :wacko:

If you already know the layout of the buffer, like:

struct CBUFFER{    D3DXMATRIX view, proj, world;};
Maybe you could create a staging buffer with the same size as the origional one. Then with the staging, do something like this to copy the data:
DeviceContext->CopyResource(staging, buffer);
And remember that this staging buffer has different parameters than the original one, it should include this (the creation flags):
bufferDesc.Usage = D3D11_USAGE_STAGING;bufferDesc.BindFlags = 0;bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
So when you've copied the data from the original buffer to the staging, you may begin reading from it:
D3D11_MAPPED_SUBRESOURCE ms;engine.devcon->Map((ID3D11Buffer*)staging, NULL, D3D11_MAP_READ, NULL, &ms);CBUFFER cbData;memcpy(&cbData, ms.pData, sizeof(cbData)); // I think the syntax looks somewhat like thisengine.devcon->Unmap((ID3D11Buffer*)staging, NULL);
Now you have an instance of the data which you should be able to read from ( the cbData ).
Now this may not work if you don't know the layout/structure of the buffer, in that case I'll have to think a bit more. :P

Hope this is useful!
-MIGI0027
Thanks MIGI0027, I will test this approach, seems like the right path.

EDIT: I guess you can get all structures through reflection and then read the binded buffers.

Thanks




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS