Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DirectX Clip space texture coordinates

Okay first up I am using:

  • DirectX 10
  • C++

Okay this is a bit of a bizarre one to me, I wouldn't usually ask the question, but I've been forced by circumstance. I have two triangles (not a quad for reasons I wont go into!) full screen, aligned to screen through the fact they are not transformed.

In the DirectX vertex declaration I am passing a 3 component float (Pos x,y,z) and 2 component float (Texcoord x,y). Texcoord z is reserved for texture2d arrays, which I'm currently defaulting to 0 in the the pixel shader.

I wrote this to achieve the simple task:

float fStartX = -1.0f;
float fEndX = 1.0f;
float fStartY = 1.0f;
float fEndY = -1.0f;
float fStartU = 0.0f;
float fEndU = 1.0f;
float fStartV = 0.0f;
float fEndV = 1.0f;
vmvUIVerts.push_back(CreateVertex(fStartX, fStartY, 0, fStartU, fStartV));
vmvUIVerts.push_back(CreateVertex(fEndX, fStartY, 0, fEndU, fStartV));
vmvUIVerts.push_back(CreateVertex(fEndX, fEndY, 0, fEndU, fEndV));
vmvUIVerts.push_back(CreateVertex(fStartX, fStartY, 0, fStartU, fStartV));
vmvUIVerts.push_back(CreateVertex(fEndX, fEndY, 0, fEndU, fEndV));
vmvUIVerts.push_back(CreateVertex(fStartX, fEndY, 0, fStartU, fEndV));

IA Layout: (Update)

D3D10_INPUT_ELEMENT_DESC ieDesc[2] = {
    { "POSITION",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD",   0, DXGI_FORMAT_R32G32B32_FLOAT, 0,12, D3D10_INPUT_PER_VERTEX_DATA, 0 }
};

Data reaches the vertex shader in the following format: (Update)

struct VS_INPUT
{ 
    float3 fPos          :POSITION;
    float3 fTexcoord     :TEXCOORD0;
}

Within my Vertex and Pixel shader not a lot is happening for this particular draw call, the pixel shader does most of the work sampling from a texture using the specified UV coordinates. However, this isn't working quite as expected, it appears that I am getting only 1 pixel of the sampled texture.

The workaround was in the pixel shader to do the following: (Update)

sampler s0                      : register(s0);
Texture2DArray<float4> meshTex  : register(t0);
float4 psMain(in VS_OUTPUT vOut) : SV_TARGET
{
    float4 Color;
    vOut.fTexcoord.z = 0;
    vOut.fTexcoord.x = vOut.fPosObj.x * 0.5f;
    vOut.fTexcoord.y = vOut.fPosObj.y * 0.5f;
    vOut.fTexcoord.x += 0.5f;
    vOut.fTexcoord.y += 0.5f;
    Color = quadTex.Sample(s0, vOut.fTexcoord);
    Color.a = 1.0f;
    return Color;
}

It was also worth noting that this worked with the following VS out struct defined in the shaders:

struct VS_OUTPUT
{
    float4 fPos          :POSITION0; // SV_POSITION wont work in this case
    float3 fTexcoord    :TEXCOORD0;
}

Now I have a texture that's stretched to fit the entire screen, both triangles already cover this, but why did the texture UV's not get used as expected?

To clarify I am using a point sampler and have tried both clamp and wrapping UV.

I was a bit curious and found a solution / workaround mentioned above, however I'd prefer not to have to do it if anyone knows why it's happening?

like image 267
Jay Avatar asked Nov 04 '22 05:11

Jay


2 Answers

What semantics are you specifying for your vertex-type? Are they properly aligned with your vertices and also your shader? If you are using a D3DXVECTOR4, D3DXVECTOR3 setup (as shown in your VS code) this could be a problem if your CreateVertex() returns a D3DXVECTOR3, D3DXVECTOR2 struct. It would be reassuring to see your pixel-shader code too.

like image 149
axon Avatar answered Nov 09 '22 06:11

axon


Okay well, for one, the texture coordinates outside of 0..1 range get clamped. I made the mistake of assuming that by going to clip space -1 to +1 that the texture coordinates would be too. This is not the case, they still go from 0.0 to 1.0.

The reason why the code in the pixel shader worked, was because it was using the clip space x,y,z coordinates to automatically overwrite these texture coordinates; an oversight on my part. However, the pixel shader code results in texture stretch on a full screen 'quad', so it might be useful to someone somewhere ;)

like image 32
Jay Avatar answered Nov 09 '22 07:11

Jay