Hi,
After learning about shaders in past couple of weeks, I wanted to try something on my own.
So I tried to draw a face for a cube using Compute Shader and Geometry Shader.
Compute shader is generating 16 points ( 2x2 x 2x2) which are equally spaced by 1 unit and bottom left vertex is at origin. Below is the result without Geometry shader.
When I use Geometry Shader for generating triangles on each point. It is changing the vertices in x and y axis a little bit as you can see below. There is a gap between rows of triangles.
Here is my compute and geometry shader code.
Shader "Custom/CubeShader"
{
SubShader
{
Pass
{
CGPROGRAM
#pragma target 5.0
#pragma vertex vert
#pragma geometry GS_Main
#pragma fragment frag
#include "UnityCG.cginc"
StructuredBuffer<float3> square_points;
struct ps_input {
float4 pos : POSITION;
};
struct gs_input {
float4 pos : POSITION;
};
ps_input vert(uint id : SV_VertexID)
{
ps_input o;
float3 worldPos = square_points[id];
o.pos = mul(UNITY_MATRIX_MVP, float4(worldPos,1.0f));
return o;
}
[maxvertexcount(3)]
void GS_Main(point gs_input p[1], inout TriangleStream<gs_input> triStream)
{
float4 v[3];
v[0] = float4(p[0].pos.x, p[0].pos.y, p[0].pos.z, 1);
v[1] = float4(p[0].pos.x + 1.0f, p[0].pos.y + 0.0f, p[0].pos.z + 0.0f, 1);
v[2] = float4(p[0].pos.x + 1.0f, p[0].pos.y + 1.0f, p[0].pos.z + 0.0f, 1);
float4x4 vp = mul(UNITY_MATRIX_VP, _World2Object);
gs_input pIn;
pIn.pos = mul(vp, v[2]);
triStream.Append(pIn);
pIn.pos = mul(vp, v[1]);
triStream.Append(pIn);
pIn.pos = mul(vp, v[0]);
triStream.Append(pIn);
}
float4 frag(ps_input i) : COLOR
{
return float4(1,0,0,1);
}
ENDCG
}
}
Fallback Off
}
#pragma kernel CSMain
#define thread_group_size_x 2
#define thread_group_size_y 2
#define thread_group_size_z 1
#define group_size_x 2
#define group_size_y 2
#define group_size_z 1
struct PositionStruct
{
float3 pos;
};
RWStructuredBuffer<PositionStruct> output;
// I know this function is unnecessary. But only for now :) I would like to add more things here.
float3 GetPosition(float3 p, int idx)
{
return p;
}
[numthreads(thread_group_size_x, thread_group_size_y, thread_group_size_z)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
int idx = id.x + (id.y * thread_group_size_x * group_size_x) +
(id.z * thread_group_size_x * group_size_y * group_size_z);
float3 pos = float3(id.x, id.y, id.z);
pos = GetPosition(pos, idx);
output[idx].pos = pos;
}
Please help me debug it. Thanks in advance