Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 460 core
- #extension GL_NV_mesh_shader : enable
- #extension GL_NV_gpu_shader5 : enable
- #extension GL_KHR_shader_subgroup_ballot : enable
- // Set the number of threads per workgroup (always one-dimensional).
- // The limitations may be different than in actual compute shaders.
- layout(local_size_x=32, local_size_y=1,local_size_z=1) in;
- // the primitive type (points,lines or triangles)
- layout(triangles) out;
- // maximum allocation size for each meshlet
- layout(max_vertices=96, max_primitives=96) out;
- out Interpolant {
- vec3 col;
- //vec2 uv;
- } OUT[];
- struct Mesh
- {
- uint64_t count;
- uint64_t vertimage;
- uint64_t normal_image;
- uint64_t meshletimage;
- };
- struct AppInfo {
- uint16_t viewport_x;
- uint16_t viewport_y;
- uint16_t task_size;
- };
- layout(std430, binding = 1) buffer ViewInfo
- {
- AppInfo info;
- };
- layout(std430, binding = 5) buffer MeshStorage
- {
- Mesh Meshes[];
- };
- taskNV in Task {
- uint baseID;
- uint meshID;
- } IN;
- vec3 GetVertexPositionFromMesh(Mesh mesh, uint IDX) {
- uint tx = (IDX) %2048;
- uint ty = (IDX) /2048;
- ivec2 texcoords = ivec2(tx,ty);
- return texelFetch(sampler2D(mesh.vertimage), texcoords ,0).xyz;
- }
- vec3 GetVertexColorFromMesh(Mesh mesh, uint IDX) {
- uint tx = (IDX) %2048;
- uint ty = (IDX) /2048;
- ivec2 texcoords = ivec2(tx,ty);
- return texelFetch(sampler2D(mesh.normal_image), texcoords ,0).xyz;
- }
- vec4 ProcessVertexPosition(vec3 original) {
- uint axisnum = info.task_size;
- float step = 2 / (float)axisnum;
- float scale = 1.5f/axisnum;
- vec4 pos = vec4(original,1.f) * vec4(scale,scale,scale,1);
- float yoffset = -0.9 + (float)(IN.baseID/axisnum) * step;
- float xoffset = -0.9 + (float)(IN.baseID%axisnum) * step;
- pos.x += xoffset;
- pos.y += yoffset;
- return pos;
- }
- void main()
- {
- const int meshlet_lenght = 96;
- Mesh myMesh = Meshes[IN.meshID];
- const uint meshletstart = (gl_WorkGroupID.x *(uint)meshlet_lenght);
- const uint meshletend =min( meshletstart+meshlet_lenght, (uint)myMesh.count);
- const int count = max(0, (int)meshletend - (int)meshletstart);
- if (count > 0) {
- const uint li =gl_LocalInvocationIndex*3;
- const uint gi =meshletstart + (li);
- const vec4 v1 = ProcessVertexPosition(GetVertexPositionFromMesh(myMesh, gi));
- const vec4 v2 = ProcessVertexPosition(GetVertexPositionFromMesh(myMesh, gi+1));
- const vec4 v3 = ProcessVertexPosition(GetVertexPositionFromMesh(myMesh, gi+2));
- float max_x = max(v1.x,max(v2.x,v3.x)) / 2.0;
- float max_y = max(v1.y,max(v2.y,v3.y)) / 2.0;
- float min_x = min(v1.x,min(v2.x,v3.x)) / 2.0;
- float min_y = min(v1.y,min(v2.y,v3.y)) / 2.0;
- float px_max_x = round(max_x * info.viewport_x);
- float px_max_y = round(max_y * info.viewport_y);
- float px_min_x = round(min_x * info.viewport_x);
- float px_min_y = round(min_y * info.viewport_y);
- bool Culled = px_max_x == px_min_x ||px_max_y == px_min_y;
- uvec4 vote = subgroupBallot(!Culled);
- const uint final_count = subgroupBallotBitCount(vote);
- if (final_count > 0) {
- uint idx = subgroupBallotExclusiveBitCount(vote) *3;
- if (!Culled) {
- gl_MeshVerticesNV[li].gl_Position = v1;
- gl_MeshVerticesNV[li+1].gl_Position = v2;
- gl_MeshVerticesNV[li+2].gl_Position = v3;
- gl_PrimitiveIndicesNV[idx] = li;
- gl_PrimitiveIndicesNV[idx+1] = li+1;
- gl_PrimitiveIndicesNV[idx+2] = li+2;
- OUT[li].col = GetVertexColorFromMesh(myMesh,gi);
- OUT[li+1].col = GetVertexColorFromMesh(myMesh,gi+1);
- OUT[li+2].col = GetVertexColorFromMesh(myMesh,gi+1);
- }
- }
- //if(gl_LocalInvocationID.x == 0){
- gl_PrimitiveCountNV = final_count;
- //}
- }
- else {
- gl_PrimitiveCountNV = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement