Read Shaders from CPU in DirectX 11

Started by
8 comments, last by fs1 10 years, 3 months ago
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
Advertisement

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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.
Is Shader Reflection the way to go?

Any takes?

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

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

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

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?)

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?

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

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

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

This topic is closed to new replies.

Advertisement