Microsoft DirectX 8.1 (pixel shader versions 1.0, 1.1, 1.2, 1.3)

tex

Loads the destination register with color data (RGBA) sampled from a texture.

The texture must be bound to a particular texture stage (n) using SetTexture. Texture sampling is controlled by the texture stage state attributes, set with SetTextureStageState.

tex dest

Registers

Argument Description RegistersVersion
vn cn tn rn
dest Destination register x 1.0, 1.1, 1.2, 1.3

To learn more about registers, see Registers.

Remarks

The destination register number specifies the texture stage number.

Texture sampling uses texture coordinates to look up, or sample, a color value at the specified (u,v,w,q) coordinates while taking into account the texture stage state attributes.

The texture coordinate data is interpolated from the vertex texture coordinate data and is associated with a specific texture stage. The default association is a one-to-one mapping between texture stage number and texture coordinate declaration order. This means that the first set of texture coordinates defined in the vertex format are by default associated with texture stage 0.

Texture coordinates may be associated with any stage using two techniques. When using a fixed function vertex shader or the fixed function pipeline, the texture stage state flag TSS_TEXCOORDINDEX can be used in SetTextureStageState to associate coordinates to a stage. Otherwise, the texture coordinates are output by the vertex shader oTn registers when using a programmable vertex shader.

Example

This example is for illustration only. The C code accompanying the shader has not been optimized for performance. It can use helper functions from the Sample Framework. The sample framework is the foundation on which many of the samples are built.

// This example applies a texture to a quad.

// The shader is shown below.
ps.1.0        // version instruction
tex t0        // samples the texture at stage 0 using texture coordinates from stage 0
mov r0, t0    // copies the color in t0 to output register r0

// The rendered output from the pixel shader is shown below. It is 
// simply a texture map applied to a quad object.
// Additional code is required to use this shader and an example 
// scenario is shown below.


// Load the texture in texture stage 0.
LPDIRECT3DDEVICE8   m_pd3dDevice;	// Initialize the pointer before using
LPDIRECT3DTEXTURE8  m_pTexture0;	// a pointer for the texture
TCHAR               strPath[512] = "DX5_Logo.bmp";
// Helper function from the SDK
D3DUtil_CreateTexture( m_pd3dDevice, strPath, &m_pTexture0, D3DFMT_R5G6B5 );
m_pd3dDevice->SetTexture( 0, m_pTexture0 );

// This code creates the shader from a file. The contents of the shader 
// file can also be supplied as a text string.
TCHAR               strPShaderPath[512];
LPD3DXBUFFER        pCode;
// Helper function from the SDK
DXUtil_FindMediaFile( strPShaderPath, _T("shaderFile.txt") );

// Assemble the vertex shader from the file.
D3DXAssembleShaderFromFile( strPShaderPath, 0, NULL, &pCode, NULL );
m_pd3dDevice->CreatePixelShader((DWORD*)pCode->GetBufferPointer(),
                   &m_hPixelShader );
pCode->Release();

// Define the object vertex data.
struct CUSTOMVERTEX
{
    FLOAT x, y, z;
    FLOAT tu1, tv1;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_TEX1|TEXCOORD2(0))
static CUSTOMVERTEX g_Vertices[]=
{
    //  x      y     z     u1    v1   
    { -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, },
    { +1.0f, -1.0f, 0.0f, 1.0f, 1.0f, },
    { +1.0f, +1.0f, 0.0f, 1.0f, 0.0f, },
    { -1.0f, +1.0f, 0.0f, 0.0f, 0.0f, },
    // v1 is flipped to meet the top down convention in Windows
    // the upper left texture coordinate is (0,0)
    // the lower right texture coordinate is (1,1). 
};

// Create and fill the quad vertex buffer.
m_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
		D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX,
		D3DPOOL_MANAGED, &m_pQuadVB );

CUSTOMVERTEX* pVertices = NULL;
m_pQuadVB->Lock( 0, 4*sizeof(CUSTOMVERTEX), (BYTE**)&pVertices, 0 );
for( DWORD i=0; i<4; i++ )
    pVertices[i] = g_Vertices[i];

m_pQuadVB->Unlock();


// Check to see if the hardware supports pixel shaders.
if( D3DSHADER_VERSION_MAJOR( pCaps->PixelShaderVersion ) < 1 )
	return E_FAIL;

// Set up the transforms.
D3DXVECTOR3 from( 0, 0, -5.0f );
D3DXVECTOR3 at( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 up( 0.0f, 1.0f, 0.0f );

D3DXMATRIX matWorld;
D3DXMatrixIdentity( &matWorld );
m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &from, &at, &up );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 0.5f, 1000.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

// Render the output.
// Clear the back buffer to black.
m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, 0x00000000, 1.0f, 0L );

// Set device state.
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
m_pd3dDevice->SetRenderState( D3DRS_CLIPPING, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,  FALSE );

m_pd3dDevice->SetTexture( 0, m_pTexture0 );
m_pd3dDevice->SetStreamSource( 0, m_pQuadVB, sizeof(CUSTOMVERTEX) );
m_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
m_pd3dDevice->SetPixelShader( m_hPixelShader );
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 2 );
m_pd3dDevice->SetTexture( 0, NULL );