Advertisement

Attempting to make a pass through Geometry Shader

Started by February 14, 2025 11:14 PM
5 comments, last by DividedByZero 6 days, 6 hours ago

Hi Guys,

I am in the process of getting my head around playing with Geometry Shaders and so far so good.

I am understanding the geometry side of things, but am having trouble with passing the UV information through to the pixel shader.

At the moment I have this;

 

struct GSOutput
{
   float4 pos          : SV_POSITION;
   float2 texcoord     : TEXCOORD0;
};

[maxvertexcount(3)]
void gs_main(triangle float4 input[3] : SV_POSITION, inout TriangleStream< GSOutput > output)
{
   for (uint i = 0; i < 3; i++)
   {
       GSOutput element;
       element.pos = input[i];
       element.texcoord = input[i];
       output.Append(element);
   }
}

 

Which is giving the incorrect output as per below - 

 

If I adjust the pixel shader to fill the quads with white, I get the desired look (although without the texture, obviously).

 

Texture2D TextureStandard    : register(t0);
SamplerState SamplerClamp    : register(s0);

struct PS_Input
{
   float4 position         : SV_POSITION;
   float2 textureCoord     : TEXCOORD0;
};

float4 ps_main(PS_Input input) : SV_TARGET
{
   float4 texColor = TextureStandard.Sample(SamplerClamp, input.textureCoord);

   texColor.r = 1.0f;      // Added to show what the geometry itself should look like.
   texColor.g = 1.0f;
   texColor.b = 1.0f;
   texColor.a = 1.0f;
   
   return texColor;
}

 

 

From this we can tell that in the Geometry Shader that the UV data isn't getting correctly sent to the Pixel Shader.

If I remove the Geometry Shader entirely, I get the desired output of below. Eliminating any issues in the Vertex and Pixel Shaders.

 

So essentially, I am wondering how you correctly pass the UV data along in the Geometry Shader?

Any help would by hugely appreciated.

Many thanks. 🙂

 

 

 

DividedByZero said:
element.pos = input[i];
element.texcoord = input[i];

I have no experience with geometry shaders or DirectX, but I'd guess the problems is that you are assigning the same vec4 input position to both the output position and the texture coordinate, which is why the textures are not correct. I'm surprised that this doesn't produce a compilation error for vec4→vec2 conversion.

Advertisement

Aressera said:

DividedByZero said:
element.pos = input[i];
element.texcoord = input[i];

I have no experience with geometry shaders or DirectX, but I'd guess the problems is that you are assigning the same vec4 input position to both the output position and the texture coordinate, which is why the textures are not correct. I'm surprised that this doesn't produce a compilation error for vec4→vec2 conversion.

Thanks for the reply. I'll look in to that right now.

Weird that I get the anticipated correct looking result if I tell the PS to fill the quads white though. But yeah, I can certainly see what you are getting at there and I believe that does need to be address as part of the problem.

Oooh, I just pulled out my Frank D. Luna book and made a little more sense of it.

I can probably re-use the structs at the top, but I have now got it up and running.

struct GSOutput
{
	float4 pos          : SV_POSITION;
    float2 texcoord     : TEXCOORD0;
};

struct VSOutput
{
    float4 pos          : SV_POSITION;
    float2 texcoord     : TEXCOORD0;
};


[maxvertexcount(3)]
void gs_main(triangle VSOutput input[3] : SV_POSITION, inout TriangleStream< GSOutput > output)
{
    for (uint i = 0; i < 3; i++)
    {
        GSOutput element;
        element.pos = input[i].pos;
        element.texcoord = input[i].texcoord;
        output.Append(element);
    }
}

Huge thanks, Aressera for giving me a nudge in the right direction.

DividedByZero said:
Weird that I get the anticipated correct looking result if I tell the PS to fill the quads white though.

It makes perfect sense to me. There is no problem with the vertex positions, only the associated texture coordinates, so by ignoring the texture coordinates you get geometry in the right places. The vertex positions are in range [-1,1] (I think), and so I bet that the wrong image you posted (2nd one) is just a 4-way mirrored version of the original texture map, which can be seen if you look closely. I bet your original texture has 5 fruits, in order of banana, tomato, pear, watermelon?, persimmon. This is the result of sampling the texture with UVs that are the same as the XY components of screen-space position.

Just posted two seconds before you did. :D

Many thanks, once again.

Advertisement