• Create Account

## Loop through an ID3D11Texture2D object

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.

3 replies to this topic

### #1iedoc  Members

2144
Like
0Likes
Like

Posted 26 February 2012 - 09:10 AM

I would like to know how to iterate through the texels of an ID3D11Texture2D object, so i can do an operation that replaces certain texels with that of another ID3D11Texture2D object.

So what i'm doing is rendering two scenes onto two different render targets. What i'd like to do is, is do an operation that replaces certain texels of one with the other (like every other texel). I was first looking at the pixel shader to do this, but i suppose that might not be possible (i'm not very good with hlsl). How would one go about doing this?

How to iterate through the texels of an ID3D11Texture2D and edit them, or how to know the current pixel in the pixel shader. Or another method that i didn't think of would be just as well

You might think this sounds exactly like blending, and it kind of is, but i don't want the pixels to "bleed" into the pixels next to them
Braynzar Soft - DirectX Lessons & Game Programming Resources!

### #2Tsus  Members

1178
Like
1Likes
Like

Posted 27 February 2012 - 08:02 AM

Hi!

You could do that with a pixel shader by rendering a viewport quad with the viewport set to the size of the textures (I assume their resolutions match?). Then use the load intrinsic (avoids texture samplers) to fetch the texture colors from both input textures and combine them however you want.
Texture2D<float4> inputTexture1;
Texture2D<float4> inputTexture2;
float4 PS(float4 pos: SV_Position) : SV_Target0
{
return magicallyCombineTheTextures(texA, texB, pos.xy);
}
You’d have to maintain two copies of the inputTexture1 in a ping-pong manner, since you read from and write to it. Well, unless the color in inputTexture1 doesn’t matter for your result color e.g. in case you just replace it with inputTexture2 under a certain condition (so no read involved). Then simple blending would do the trick.
In the sample above I passed the position to the magicallyCombineTheTextures function, since your ‘iterate’ sounded like you need to have the ID of the pixel for the combination of the textures. You could as well do this with a compute shader (and thereby bypass the rasterization overhead).

If you want to change (for instance) inputTexture1 in place (requires shader model 5) you could use an UnorderedAccessView (RWTexture2D<float> inputTexture1) and directly write the result back to inputTexture1. Though, you’d have to fetch the colors component wise from the UAV. Perhaps it would be beneficial to launch 4 threads and use the compute shader for that. Well, unless the context switch to the compute pipeline doesn’t cost too much. I guess you’d need some experiments here.

You might think this sounds exactly like blending, and it kind of is, but i don't want the pixels to "bleed" into the pixels next to them.

Shouldn’t happen if you don’t use samplers.

Or you could take a step back and look again whether you can avoid the rendering to the second texture at all. Maybe there is a clever way to use the stencil buffer or so to directly render the result to the target texture.

Cheers!

### #3iedoc  Members

2144
Like
0Likes
Like

Posted 27 February 2012 - 08:29 AM

you could use a point samper to avoid the blending problem. But even with blending, i needed to make sure that every other pixel was exactly the other texture, with blending, i can't be sure that the blending operation will do that perfectly. I needed an operation that was very predictable if you know what i mean

but this was actually a double post (although i only clicked the send button once...). The answer was solved already, all i needed was SV_Position to get the position of the current pixel. i didn't realize SV_Position could be used in the pixel shader (I mean, i knew that, but for some reason i had spaced it out... I'm really not very good with hlsl). Just knowing you can use SV_Position to get the position of the current pixel was all i really wanted to know though, but thanks for an in depth answer. My pixel shader ended up like this:

float4 _3D_PS(VS_OUTPUT input) : SV_TARGET
{
float4 LeftEye = LeftEyeTexture.Sample( TexSamplerState, input.TexCoord );
float4 RightEye = RightEyeTexture.Sample( TexSamplerState, input.TexCoord );

int tempIntX = input.Pos.x % 2;
int tempIntY = input.Pos.y % 2;

if(tempIntX == 0)
{
if(tempIntY != 0)
{
return LeftEye;
}
}

return RightEye;
}

Braynzar Soft - DirectX Lessons & Game Programming Resources!

### #4Tsus  Members

1178
Like
0Likes
Like

Posted 27 February 2012 - 08:59 AM

you could use a point samper to avoid the blending problem. But even with blending, i needed to make sure that every other pixel was exactly the other texture, with blending, i can't be sure that the blending operation will do that perfectly. I needed an operation that was very predictable if you know what i mean

Why not? Because of floating point inaccuracy?

but this was actually a double post.

Oh, haven’t seen that.

float4 LeftEye = LeftEyeTexture.Sample( TexSamplerState, input.TexCoord );
float4 RightEye = RightEyeTexture.Sample( TexSamplerState, input.TexCoord );

uint tempIntX = (uint)(input.Pos.x) % 2u;
uint tempIntY = (uint)(input.Pos.y) % 2u;
Modulo on uints might be faster.

I’d rather place the two textures in a Texture2DArray. This way you only need to load from one texture, since you can dynamically index from which one to load.

Well, considering it again… In your case a stencil mask would be the best choice, I think, because you only need to shade the samples you use in the end. So you can avoid the second render target completely and don’t have to worry about the blending stuff. Besides you halve the pixel shader invocations, so the rendering should be faster, too. (Unless you need the pixels someplace else. )

Cheers!

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.