www.xbdev.net
xbdev - software development
Thursday March 27, 2025
Home | Contact | Support | ShaderX.. Making our graphics card work for us..
     
 

ShaderX..

Making our graphics card work for us..

 

Vertex Shaders - Prt1 - Basics

by bkenwright@xbdev.net




While this might not seem very exciting - it's a very important first step in shader programming - shader assembly to pass the vertex positions onto the output.

This little function is called over and over again, and does the rendering of our simple triangle... notice how all our vertex shader code is all in this one function...I did that so you could see the birth and death of how it works in one easy step.... eventually you could put the creation..and destruction code in seperate function..etc...and optimise your code for better performance

////////////////////////////////////////////////////////////////////////////////////
//                                                                                \\
// This little function is called over and over again, and does the rendering of  \\
// our simple triangle... notice how all our vertex shader code is all in this    \\
// one function...I did that so you could see the birth and death of how it works \\
// in one easy step.... eventually you could put the creation..and destruction    \\
// code in seperate function..etc...and optimise your code for better performance \\
// ...                                                                            \\
//                                                                                \\
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
void Render()
{
    if(!
g_pd3dDevice)return;
 
    
//<-------------------Start of our vertex shader code-------------------------->
    
const char SimpleShader[] =
    {
          
"vs.1.1                     // Shader version 1.1 \n"
          "mov     oPos,  v0          // oPos-Output register, v0-Input register\n"
    
};
 
    
DWORD dwDecl[] =
    {
          
D3DVSD_STREAM(0),
          
D3DVSD_REG(D3DVSDE_POSITIOND3DVSDT_FLOAT2),
          
D3DVSD_END()
    };
 
    
DWORD dwShaderID;
 
    
ID3DXBufferpVS;
 
    
D3DXAssembleShaderSimpleShadersizeof(SimpleShader)-1,
                                  
0NULL, &pVSNULL );
 
    
g_pd3dDevice->CreateVertexShader(dwDecl, (DWORD*)pVS->GetBufferPointer(),
                                                     &
dwShaderID0);
    
g_pd3dDevice->SetVertexShaderdwShaderID );
 
    
// 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->DeleteVertexShader(dwDecl);
    //pVS->Release();
 
    //<-------------------End of our vertex shader code---------------------------->
 
    
struct stVert
    
{
          
FLOAT xyz;  // D3DFVF_XYZ
    
};
    
UINT myFVFFormat D3DFVF_XYZ ;
 
    
stVert myBox[]=
    {
          {-
0.5f, -0.5f0.0f},
          { 
0.5f,  0.5f0.0f},
          {-
0.5f,  0.5f0.0f},
 
          {-
0.5f, -0.5f0.0f},
          { 
0.5f,  0.5f0.0f},
          { 
0.5f, -0.5f0.0f}
    };
 
    
IDirect3DVertexBuffer8 pVertexBuffer;
    
g_pd3dDevice->CreateVertexBuffer6*sizeof(stVert), 0,
                                  
myFVFFormatD3DPOOL_MANAGED, &pVertexBuffer );
 
    
unsigned char *p;
    
pVertexBuffer->Lock(0,0, &p0);
    
memcpy(pmyBox6*sizeof(stVert) );
    
pVertexBuffer->Unlock();
 
    
//Turn off lighting becuase we are specifying that our vertices have colour
    
g_pd3dDevice->SetRenderStateD3DRS_LIGHTINGFALSE);
    
g_pd3dDevice->SetRenderStateD3DRS_ZENABLETRUE );
    
g_pd3dDevice->SetRenderStateD3DRS_CULLMODED3DCULL_NONE);
 
    
// Clear the backbuffer and the zbuffer
    
g_pd3dDevice->Clear0NULLD3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                                            
D3DCOLOR_XRGB(0x00,0x00,0xff), 1.0f);
  
    
// Draw our triangle.
    
g_pd3dDevice->SetStreamSource(0pVertexBuffersizeof(stVert));
   
    
//<-------------------Using our Vertex Shader Here ---------------------------->
    
g_pd3dDevice->SetVertexShaderdwShaderID );
 
    
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST02);
 
      
// End the scene
    
g_pd3dDevice->EndScene();
   
    
// Present the backbuffer contents to the display
    
g_pd3dDevice->PresentNULLNULLNULLNULL );
 
    
g_pd3dDevice->DeleteVertexShader(dwShaderID);
    
pVS->Release();
 
    
pVertexBuffer->Release();
}




 
Advert (Support Website)

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