Jump to content
  • Advertisement
Sign in to follow this  

DX11 Highly confused about simple quad texturing

This topic is 2084 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts



I am trying to texture a simple quad. My 'engine' can currently create a quad and I would like to expand it to be able to load textures.


My current attempt is as follows:

	Vertex vtx[] =
		Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
		Vertex(1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f),
		Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),
		Vertex(1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f),

	createVertexBuffer(ARRAYSIZE(vtx), vtx, &vertexBuffer);

	D3DX11CreateShaderResourceViewFromFile(d3d11Device, "textures\\t1.bmp", NULL, NULL, &texture, NULL);

	d3d11DevCon->PSSetShaderResources(0, 1, &texture);
	d3d11DevCon->Draw(4, 0);

where texture is of type ID3D11ShaderResourceView


The problem is I am reusing the same.fx file which is as follows:

cbuffer cbPerObject
	float4x4 WVP;

struct VS_OUTPUT
	float4 Pos : SV_POSITION;
	float4 Color : COLOR;

VS_OUTPUT VS(float4 inPos : POSITION, float4 inColor : COLOR)
    VS_OUTPUT output;
    output.Pos = mul(inPos, WVP);
    output.Color = inColor;

    return output;

float4 PS(VS_OUTPUT input) : SV_TARGET
    return input.Color;

I am stuck understanding how to edit the .fx file to allow for textures. And if I have correctly laid out code that will draw my textured quad.


Thank for your yout time



Share this post

Link to post
Share on other sites

1. Create a correct vertex buffer, no need for color declaration

struct SVertex
    D3DXVECTOR2 PosS;   // Screen space position
    D3DXVECTOR2 TexC;   // Texture coordinates

static const SVertex gFullScreenQuadVertices[] = 
    {D3DXVECTOR2(-1.0f, -1.0f), D3DXVECTOR2(0.0f,  1.0f)},
    {D3DXVECTOR2(-1.0f,  1.0f), D3DXVECTOR2(0.0f,  0.0f)},
    {D3DXVECTOR2( 1.0f, -1.0f), D3DXVECTOR2(1.0f,  1.0f)},
    {D3DXVECTOR2( 1.0f,  1.0f), D3DXVECTOR2(1.0f,  0.0f)},

2. Input layout is

    D3D11_INPUT_ELEMENT_DESC layoutDesc[] = 
        {"POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, offsetof(SVertex, PosS), D3D11_INPUT_PER_VERTEX_DATA, 0},                
        {"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,0, offsetof(SVertex, TexC), D3D11_INPUT_PER_VERTEX_DATA, 0},                

3. VS should be pass-through:

void VS(float4 PosL : POSITION, inout float2 TexC : TEXCOORD, out float4 svPos : SV_POSITION)
    svPos = PosL;

Note the TexC is declared as inout, so the compiler will create a code to pass it to the PS.


4. PS needs to do texture sampling:

SamplerState gSampler : register(s0);
Texture2D gSRV : register(t0);

float4 PS(float2 TexC : TEXCOORD) : SV_TARGET
    float3 c = gSRV.Sample(gSampler, TexC).rgb;
    return float4(c, 1);

Note that in order to do this you need to create a sample state. Check CreateSamplerState() for documentation. Basic tri-linear sampler is

// Create point sampler state
    D3D11_SAMPLER_DESC desc;
    desc.AddressU = desc.AddressV = desc.AddressW = D3D11_TEXTURE_ADDRESS_WARP;
    desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
    desc.MinLOD = 0;
    desc.MaxLOD = D3D11_FLOAT32_MAX;
    desc.MipLODBias = 0;
    desc.MaxAnisotropy = 0;
    V(pDevice->CreateSamplerState(&desc, &LinearSampler));

Then set it before the draw call using PSSetSamplers().

Share this post

Link to post
Share on other sites

I finally got around to making this work in its own separate program so thanks. I have a question though:


Right now my 'game' consists of primitive shapes that just have a color, and textured shapes which are the walls. When compiling the game objects and shaders, it is better to use separate .fx files for each different game asset, or just add everything you need in one big shader file.


I hope the question makes sense because I am still quite confused about vertex and pixel shaders.


Thanks again

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!