read and write texture2D

Started by
13 comments, last by lomateron 12 years, 1 month ago
its simple, but i am not secure if my code is correct, this is what i have made.
the texture will be used on this way: each pixel is a vector.

What i need is a:
new Texture------the main thing
ShaderResouceView with an effectShaderResourceVariable---- to read it
RenderTargetView-------- to write to it

This are the variables used:
ID3D10Texture2D* VectorsBuffer;
ID3D10ShaderResourceView* g_pTexture2 = NULL;
ID3D10EffectShaderResourceVariable* g_pTx2 = NULL;
ID3D10RenderTargetView* g_pRenderTargetView2 = NULL;

AND THE CODE

ID3D10Texture2D* VectorsBuffer;
D3D10_TEXTURE2D_DESC texD;
texD.Width = width;
texD.Height = height;
texD.MipLevels = 1;
texD.ArraySize = 1;
texD.Format = DXGI_FORMAT_R32G32B32_FLOAT;
texD.SampleDesc.Count = 1;
texD.SampleDesc.Quality = 0;
texD.Usage = D3D10_USAGE_DEFAULT;
texD.BindFlags = D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE;
texD.CPUAccessFlags = 0;
texD.MiscFlags = 0;
hr = g_pd3dDevice->CreateTexture2D(&texD, 0, &VectorsBuffer);
if( FAILED( hr ) )
return hr;
hr = g_pd3dDevice->CreateRenderTargetView( VectorsBuffer, 0, &g_pRenderTargetView2 );
if( FAILED( hr ) )
return hr;
D3D10_SHADER_RESOURCE_VIEW_DESC srvD;
srvD.Format = DXGI_FORMAT_R32G32B32_FLOAT;
srvD.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D;
srvD.Texture2D.MipLevels = 1;
srvD.Texture2D.MostDetailedMip = 0;
hr = g_pd3dDevice->CreateShaderResourceView(VectorsBuffer,&srvD, &g_pTexture2);
if( FAILED( hr ) )
return hr;
g_pTx2 = g_pEffect->GetVariableByName( "TextureRW" )->AsShaderResource();
g_pTx2->SetResource( g_pTexture2 );


is it ok?
Advertisement
I'm pretty sure you'll have to use
D3D10_BIND_RENDER_TARGET | D3D10_BIND_SHADER_RESOURCE
as Bind flags.

Without this CreateRenderTargetView() should fail.
ohhh i thought i was missing something there, but apart from that do i need anything else.
Why don't you just run the code with a directx debug device?
If something is wrong dx will most likely print error messages to the console explaining to you what you're doing wrong.
Yeah use D3D10_CREATE_DEVICE_DEBUG when creating your device, and then check the debug output window for error messages.
questions!
Can i put a texture in a hlsl texture variable to read it and put it in the render target of that same hlsl code at the same time?
I have already a texture in an hlsl variable now how do i get a pixel of that texture and put it in a float3 if the resolution of the texture is 800X600? or whatever resolution.
please answer
for the second question can i do this inside the pixel shader method?
my texture is only RGB
[color=#000000]uint3 sampleCoord [color=#666600]=[color=#000000] uint3[color=#666600]([color=#000000]x[color=#666600],[color=#000000] y[color=#666600],[color=#000000] mipLevel[color=#666600]);
[color=#000000] float3 sample [color=#666600]=[color=#000000] texture[color=#666600].[color=#660066]Load[color=#666600]([color=#000000]sampleCoord[color=#666600]);
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Can i put a texture in a hlsl texture variable to read it and put it in the render target of that same hlsl code at the same time?[/quote]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

No, you cannot bind a resource as a render target and shader resource at the same time.

[/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

I have already a texture in an hlsl variable now how do i get a pixel of that texture and put it in a float3 if the resolution of the texture is 800X600? or whatever resolution.[/quote]

[/font]
What are you trying to do? That sounds like standard texture sampling to me.
Define a sampler state and use texture.sample() in dx10/dx11 or tex2d() in dx9.
There are other methods to get get pixels from specific mip levels or directly read specific samples from multisampled textures.
http://msdn.microsoft.com/en-us/library/windows/desktop/bb509700(v=vs.85).aspx

i am using the texture as a place to put vectors where each pixel is a 3D vector, so then i want to make calculations with the vectors i have,
HOW DO I GET A PIXEL? i have the

Texture2D Vectors;

then inside the pixel shader metod i put this code to GET A PIXEL, can i?(this is my first question now)

[color=#000000]uint3 sampleCoord [color=#666600]=[color=#000000] uint3[color=#666600]([color=#000000]x[color=#666600],[color=#000000] y[color=#666600],[color=#000000] 1[color=#666600]);
[color=#000000]float3 sample [color=#666600]=[color=#000000] Vectors[color=#666600].[color=#660066]Load[color=#666600]([color=#000000]sampleCoord[color=#666600]);
so "sample" is the pixel at the (x, y) coordinate(is it?)

If that's true then i can continue and now i wanna modify the vectors depending on the calculations, how do i do that? writing them to another texture, yeah a render target texture.
Now in the .fx file what's the code to write a vector that is float3([color=#000000]float3 sample) into a pixel of the render target texture? the return value of the pixel shader method is the one that will be in a pixel of the render target texture, isnt it? then if for example my (Texture2D Vectors;) has only 3 pixels then, how do i run across those 3 pixels so i modify the 3 pixels? thats my second question because i am stuck there, the pixel shader method runs when.....?
Yes, you can sample individual pixels like that.

A common way to modify texture data is to just run a fullscreen pass. (render a screenspace quad covering the whole screen)
This way every iteration of the pixelshader will match 1 pixel from the texture.
You can use texture coordinates from the fullscreen quad to sample the texture.

You'll have to bind another texture with the same dimensions to write to, as you cannot write to the texture you're currently reading.
You write to it by returning a value from the pixel shader.
If you need to perform multiple operations on the texture, you can ping pong between 2 textures of the same dimension.

This topic is closed to new replies.

Advertisement