xbdev - software development
Sunday May 28, 2023
home | about | contact | Donations



Making our graphics card work for us..


Pixel Shaders - Prt1 - Your ABC's..

by bkenwright@xbdev.net








void Render()




      //<-------------------Start of our pixel shader code--------------------------->

      const char SimpleShader[] =


            "ps.1.0                     // Pixel Shader Version 1.0 \n"

            "tex     t0                 // Declare that where using texture t0\n"

        "mov     r0, t0             // Output texture to the screen\n"



      DWORD dwShaderID;


      ID3DXBuffer* pVS;


      HRESULT e =D3DXAssembleShader( SimpleShader, sizeof(SimpleShader)-1,

                                    0, NULL, &pVS, NULL );


      g_pd3dDevice->CreatePixelShader( (DWORD*)pVS->GetBufferPointer(), &dwShaderID );


      // g_pd3dDevice->SetPixelShader( dwShaderID );


      // Don't forget to put these lines in your code...I've added them to the bottom

      // of the function!...always release resources after creating them.

      // g_pd3dDevice->DeletePixelShader(dwShaderID);

      // pVS->Release();


      //<-------------------End of our Pixel Shader code----------------------------->


      struct stVert


            FLOAT x, y, z;  // D3DFVF_XYZ

            FLOAT tu, tv;   // D3DFVF_TEX1


      UINT myFVFFormat = D3DFVF_XYZ | D3DFVF_TEX1 ;


      stVert myBox[]=


            {-0.5f, -0.5f, 0.0f,   0.0f, 0.0f },

            { 0.5f,  0.5f, 0.0f,   1.0f, 1.0f },

            {-0.5f,  0.5f, 0.0f,   0.0f, 1.0f },


            {-0.5f, -0.5f, 0.0f,   0.0f, 0.0f },

            { 0.5f,  0.5f, 0.0f,   1.0f, 1.0f },

            { 0.5f, -0.5f, 0.0f,   1.0f, 0.0f }



      IDirect3DVertexBuffer8 * pVertexBuffer;

      g_pd3dDevice->CreateVertexBuffer( 6*sizeof(stVert), 0,

                                    myFVFFormat, D3DPOOL_MANAGED, &pVertexBuffer );


      unsigned char *p;

    pVertexBuffer->Lock(0,0, &p, 0);

    memcpy(p, myBox, 6*sizeof(stVert) );



      //Turn off lighting becuase we are specifying that our vertices have colour

    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);

      g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

      g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE);


      IDirect3DTexture8* pTexture;

      D3DXCreateTextureFromFile(g_pd3dDevice, "image.bmp", &pTexture);

      // Clear the backbuffer and the zbuffer

    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,

                                            D3DCOLOR_XRGB(0x00,0x00,0xff), 1.0f, 0 );


    g_pd3dDevice->SetStreamSource(0, pVertexBuffer, sizeof(stVert));


      //<-------------------Using our Pixel Shader Here ---------------------------->

      g_pd3dDevice->SetPixelShader( dwShaderID );





      // Draw our triangle.

    g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);


      // End the scene



    // Present the backbuffer contents to the display

    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );












So our Pixel Shader 'mini' program is as follows:


       ps.1.0                     // Pixel Shader Version 1.0

       tex     t0                 // Declare that where using texture t0

                                  // We must declare that our t0 register has an interpolated texture

                                  // coordinates - belonging to text stage 0

       mov     r0, t0             // Output texture to the screen



This would output a constant colour:


       ps.1.0                     // Pixel Shader Version 1.0

                                  // Next we 'def' - define a constant colour in c0

       def     c0,  1.0,  0.0,  0.0,  1.0

       mov     r0, c0             // Output it




IMPORTANT v1-# and v2.0

Well in version ps2.0, r0 isn't used as the output...instead you have to use oC0.


Copyright (c) 2002-2023 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.