Jump to content

  • Log In with Google      Sign In   
  • Create Account

read and write texture2D


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
14 replies to this topic

#1 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 03 March 2012 - 04:40 PM

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?

Sponsor:

#2 Hyunkel   Members   -  Reputation: 368

Like
0Likes
Like

Posted 03 March 2012 - 04:48 PM

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.

#3 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 03 March 2012 - 04:50 PM

ohhh i thought i was missing something there, but apart from that do i need anything else.

#4 Hyunkel   Members   -  Reputation: 368

Like
0Likes
Like

Posted 03 March 2012 - 05:14 PM

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.

#5 MJP   Moderators   -  Reputation: 11330

Like
0Likes
Like

Posted 03 March 2012 - 05:44 PM

Yeah use D3D10_CREATE_DEVICE_DEBUG when creating your device, and then check the debug output window for error messages.

#6 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 04 March 2012 - 03:45 PM

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.

#7 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 04 March 2012 - 05:36 PM

please answer
for the second question can i do this inside the pixel shader method?
my texture is only RGB
uint3 sampleCoord = uint3(x, y, mipLevel);
float3 sample = texture.Load(sampleCoord);

#8 Hyunkel   Members   -  Reputation: 368

Like
0Likes
Like

Posted 04 March 2012 - 06:06 PM

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?


No, you cannot bind a resource as a render target and shader resource 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.


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

#9 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 04 March 2012 - 07:15 PM

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)

uint3 sampleCoord = uint3(x, y, 1);
float3 sample = Vectors.Load(sampleCoord);
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(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.....?

#10 Hyunkel   Members   -  Reputation: 368

Like
0Likes
Like

Posted 04 March 2012 - 07:28 PM

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.

#11 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 04 March 2012 - 09:05 PM

NOW I SEE. I remembered that the SDK tutorial 3 existed, a simple yellow triangle is drawn. I discovered that the pixel shader runs depending on the PrimitiveTopology and the vertex shader position. With only one point in the vertex buffer and the primitive tipology set to point list, the point position will be transformed into a pixel position of the back buffer texture, using the viewport, so normally without world, view, projection matrices and depth things, if the point position is D3DXVECTOR3( 1.0f, 1.0f, 0.0f ), the pixel in the upper right corner will be the one processed by the pixel shader method(the z value in the point never matters here). With 2 points in the vertex buffer one in the upper left corner D3DXVECTOR3( -1.0f, 1.0f, 0.0f ) and the other in the upper right corner D3DXVECTOR3( 1.0f, 1.0f, 0.0f ) and the PrimitiveTopology set to line strip, the pixel shader method will run across all the pixels between thos point(and logically in the point position too), yeahhhhh, now maybe you think that was simple, yesss it is, but if you understand it, that will take your knowledge of directx to a next LEVEL!! MOAHAHAHAHAHAHAHAHA
Now the problem is solved, bye.

#12 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 05 March 2012 - 07:20 PM

I have succeed, very fast without any problem, but one last question, having this:

Texture2D Vectors;
uint3 sampleCoord = uint3(x, y, 1);
float3 sample = Vectors.Load(sampleCoord);

then if (x,y)=(0,0), do I get the pixel that is in the upper left corner of the texture?
lest say the image is 50x50 pixels, then if (x,y)=(50,50), that will be the down right corner pixel of the texture?

#13 MJP   Moderators   -  Reputation: 11330

Like
0Likes
Like

Posted 05 March 2012 - 07:39 PM

That's right, except that the bottom right corner will be (49,49)

#14 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 07 March 2012 - 06:50 AM

Damm i was wrong when i say, 3 posts back , if the point position is D3DXVECTOR3( 1.0f, 1.0f, 0.0f ), the pixel in the upper right corner will be the one processed by the pixel shader method, that pixel isn't in the upper right corner, that pixel is out of view, i mean it is just a little bit more up-right, north-east, that's really bad, why did the creators of directx did it like that.

#15 lomateron   Members   -  Reputation: 342

Like
0Likes
Like

Posted 07 March 2012 - 06:57 AM

hoo now i know how to fix that, just change the top left and right of the viewport.................. ohh no that dint fixed it, the pixel is never draw. what can i do :(




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