(DirectX) Creating and sampling a screen texture

sune 101 Dec 15, 2005 at 11:11


I need to create a Texture from scratch using (the texture is viewport sized):
Texture tex = new Texture(… );

Which I then fill with custom values using locks.
tex.Lock(…); … tex.Unlock();

When the texture is ready I would like to apply it using :
device.SetTexture(… );

And using a Pixel Shader I read the values using VPOS
coordinates. Ie. I create a screensized texture that is only
sampled at position where an object is rendered.

I´ve tried doing this but I dont get the expected results (for fun i tried saving my finished texture to file and creating a new texture from that file, this gives the expected visual result, but is naturally too slow).
Can anyone help me with a working way of doing this ? Could there be some mipmap issues ? Or something ?

2 Replies

Please log in or register to post a reply.

XORcist 101 Dec 15, 2005 at 17:44

First thing is that I vaguely understand what you are trying to do, but
i did not get what your question is..

Do you mean
“ How do i create a texture,
Fill it with color-data
How to Bind that texture for use with your pixel shader

Is your question about how to do the above three ?
You did the above two but, nothing changes ?

I need to create a Texture from scratch using (the texture is viewport sized):
Texture tex = new Texture(… );

All textures(surfaces) are created from scratch using D3DDevice->CreateTexture.

Standard textures (loaded from image files) are pow-2 dimensions
only & their data can be changed only by Lock, MemCopy and then Unlock.

To create a texture of your viewport dimensions & then change its data
with a pixel-shader or such, you must create a RenderTarget.

Pass D3DUSAGE_RENDERTARGET as the Usage parameter of CreateTexture

// After creating it set it as render-target instead of the framebuffer.
D3DDevice->SetRenderTarget( ScreenTex->pSurface );

// Set your pixelshader to render whatever values(output)  u want.
D3DDevice->SetPixelShader(  mumboJumboShader->d3DShader )

// Upload your shader constants 
D3DDevice->SetVertexShaderConstant(  Index, &Local_To_Screen_Matrix )

// Draw geometry onto which the pixel shader is applied to
D3DDevice->DrawPrimitive( ...  )

//Restore render-target to default FrameBuffer
D3DDevice->SetRenderTarget( NULL );

// Now you can use your ScreenTex as a standard texture.  
D3DDevice->SetTexture( ...  ScreenTex.. );

Note that on PC, you cannot read & write to a surface at the same time.
if u set the rt with Device->SetRenderTarget( ScreenTex )
You cannot call Device->SetTexture( ScreenTex )
untill you restore fb with SetRenderTarget( NULL );

kusma 101 Dec 15, 2005 at 20:44

have you tried disabling mipmapping etc?