Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Apply effect to texture using pixel shader


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
1 reply to this topic

#1 Slig Commando   Members   -  Reputation: 334

Like
0Likes
Like

Posted 23 September 2013 - 09:22 PM

First I want to apologize, I have been pretty active with questions the past few days so I hope nobody is getting annoyed with me!

 

The concept is as follows

 

Two identical textures loaded from the same resource.

Texture 1 used in the pixel shader, Texture 2 the device render target.

Pixel shader manipulates texture 1 to render results to texture 2 via full screen quad.

After draw calls, copy texture 2's surface back into texture 1's surface.

 

That is what I am attempting to accomplish, but alas am failing. I have been working on this all day and it is getting pretty frustrating. Can anyone give any advice or knit pick my code?

 

Thank you

 

Create textures and full screen quad

           //Create texture for display

            if(D3DXCreateTextureFromFileEx(

                                device,

                                texturePath.c_str(),

                                D3DX_DEFAULT,

                                D3DX_DEFAULT,

                                D3DX_DEFAULT,

                                D3DUSAGE_RENDERTARGET,

                                D3DFMT_A8R8G8B8,

                                D3DPOOL_DEFAULT,

                                D3DX_DEFAULT,

                                D3DX_DEFAULT,

                                0,

                                0,

                                0,

                                &texture1) != D3D_OK)

            {

                ErrorMessenger::ReportMessage("Failed to create texture!", __FILE__, __LINE__);

            }



            //Create texture for applying shader effect

            if(D3DXCreateTextureFromFileEx(

                                device,

                                texturePath.c_str(),

                                D3DX_DEFAULT,

                                D3DX_DEFAULT,

                                D3DX_DEFAULT,

                                D3DUSAGE_RENDERTARGET,

                                D3DFMT_A8R8G8B8,

                                D3DPOOL_DEFAULT,

                                D3DX_DEFAULT,

                                D3DX_DEFAULT,

                                0,

                                0,

                                0,

                                &texture2) != D3D_OK)

            {

                ErrorMessenger::ReportMessage("Failed to create texture!", __FILE__, __LINE__);

            }



            //Create full screen quad

            if(device->CreateVertexBuffer(6 * sizeof(LWVERTEX),

                                      0,

                                      0,

                                      D3DPOOL_DEFAULT,

                                      &fullScreenQuad,

                                      0) != D3D_OK)

                ErrorMessenger::ReportMessage("Failed to create vertex buffer!", __FILE__, __LINE__);



            LWVERTEX* fsQuad = new LWVERTEX[6];



            fsQuad[0].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);

            fsQuad[1].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);

            fsQuad[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);



            fsQuad[3].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);

            fsQuad[4].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);

            fsQuad[5].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);

            

            void* bufferData;

            fullScreenQuad->Lock(0, 0, &bufferData, 0);

            memcpy(bufferData, fsQuad, sizeof(LWVERTEX) * 6);

            fullScreenQuad->Unlock();

Here is the draw function

            //Set effect variables

            if(effect->SetTexture("scene", texture1) != D3D_OK)

                ErrorMessenger::ReportMessage("Failed to set texture", __FILE__, __LINE__);



            //Back up old render target

            IDirect3DSurface9* oldRT;

            if(device->GetRenderTarget(0, &oldRT) != D3D_OK)

                ErrorMessenger::ReportMessage("Failed to GetRenderTarget", __FILE__, __LINE__);



            //Set texture2 as new render target

            IDirect3DSurface9* newRT;

            texture2->GetSurfaceLevel(0, &newRT);

            if(device->SetRenderTarget(0, newRT) != D3D_OK)

                ErrorMessenger::ReportMessage("Failed to SetRenderTarget", __FILE__, __LINE__);
 
           //Draw

            device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0);

            device->BeginScene();



                UINT numPass;

                effect->Begin(&numPass, 0);



                    effect->BeginPass(0);



                    if(device->SetStreamSource(0, fullScreenQuad, 0, sizeof(LWVERTEX)) != D3D_OK)

                        ErrorMessenger::ReportMessage("Failed to set stream source!", __FILE__, __LINE__);



                    if(device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2) != D3D_OK)                            

                        ErrorMessenger::ReportMessage("Failed to draw primitave!", __FILE__, __LINE__);



                    effect->EndPass();



                effect->End();



            device->EndScene();



            device->SetRenderTarget(0, oldRT);



            //Copy results from texture2 back into texture 1

            IDirect3DSurface9* destSurf;

            texture1->GetSurfaceLevel(0, &destSurf);



            if(D3DXLoadSurfaceFromSurface(destSurf, nullptr, nullptr, newRT, nullptr, nullptr, 0, 0) != D3D_OK)

            {

                ErrorMessenger::ReportMessage("Failed to LoadSurfaceFromSurface", __FILE__, __LINE__);

            }

Edited by Slig Commando, 23 September 2013 - 09:24 PM.


Sponsor:

#2 unbird   Crossbones+   -  Reputation: 5992

Like
0Likes
Like

Posted 25 September 2013 - 02:16 AM

I don't see any call to SetVertexDeclaration or SetFVF. What does that vertex struct look anyway ? Does it have texture coordinates (doesn't look so, at least you're not setting them) ?

Any clues from the D3D debug runtimes ? Have you tried using PIX, the D3D debugger ? Also, check all the return codes.

Also be more clear about how it is failing (screenshots might help).




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