Advertisement
Guest User

Untitled

a guest
Apr 12th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #version 460 core
  2. #extension GL_NV_mesh_shader : enable
  3. #extension GL_NV_gpu_shader5 : enable
  4. #extension GL_KHR_shader_subgroup_ballot : enable
  5. // Set the number of threads per workgroup (always one-dimensional).
  6.   // The limitations may be different than in actual compute shaders.
  7. layout(local_size_x=32, local_size_y=1,local_size_z=1) in;
  8.  
  9. // the primitive type (points,lines or triangles)
  10. layout(triangles) out;
  11. // maximum allocation size for each meshlet
  12. layout(max_vertices=96, max_primitives=96) out;
  13.  
  14. out Interpolant {
  15.     vec3 col;
  16.     //vec2 uv;
  17. } OUT[];  
  18.  
  19.  
  20. struct Mesh
  21. {
  22.     uint64_t  count;
  23.     uint64_t  vertimage;
  24.     uint64_t  normal_image;
  25.     uint64_t  meshletimage;
  26. };
  27. struct AppInfo {
  28.     uint16_t viewport_x;
  29.     uint16_t viewport_y;
  30.     uint16_t task_size;
  31. };
  32.  
  33. layout(std430, binding = 1) buffer ViewInfo
  34. {
  35.     AppInfo info;
  36. };
  37.  
  38. layout(std430, binding = 5) buffer MeshStorage
  39. {
  40.     Mesh Meshes[];
  41. };
  42.  
  43.  taskNV in Task {
  44.      uint      baseID;
  45.      uint meshID;
  46.  } IN;
  47.  vec3 GetVertexPositionFromMesh(Mesh mesh, uint IDX) {
  48.      uint tx = (IDX) %2048;
  49.      uint ty = (IDX) /2048;
  50.      ivec2 texcoords = ivec2(tx,ty);
  51.      return texelFetch(sampler2D(mesh.vertimage), texcoords ,0).xyz;
  52.  }
  53.  vec3 GetVertexColorFromMesh(Mesh mesh, uint IDX) {
  54.      uint tx = (IDX) %2048;
  55.      uint ty = (IDX) /2048;
  56.      ivec2 texcoords = ivec2(tx,ty);
  57.      return texelFetch(sampler2D(mesh.normal_image), texcoords ,0).xyz;
  58.  }
  59.  
  60.  
  61.  vec4 ProcessVertexPosition(vec3 original) {
  62.      uint axisnum = info.task_size;
  63.      float step = 2 / (float)axisnum;
  64.      float scale = 1.5f/axisnum;
  65.      vec4 pos =  vec4(original,1.f) * vec4(scale,scale,scale,1);
  66.  
  67.      float yoffset = -0.9 + (float)(IN.baseID/axisnum) * step;
  68.      float xoffset = -0.9 + (float)(IN.baseID%axisnum) * step;
  69.      pos.x += xoffset;
  70.      pos.y += yoffset;
  71.      return pos;
  72.  }
  73. void main()
  74. {  
  75.     const int meshlet_lenght = 96;
  76.  
  77.     Mesh myMesh = Meshes[IN.meshID];
  78.     const uint meshletstart = (gl_WorkGroupID.x *(uint)meshlet_lenght);
  79.     const uint meshletend =min( meshletstart+meshlet_lenght, (uint)myMesh.count);
  80.  
  81.    
  82.  
  83.     const int count = max(0, (int)meshletend - (int)meshletstart);
  84.    
  85.     if (count > 0) {
  86.        
  87.         const uint li =gl_LocalInvocationIndex*3;
  88.         const uint gi =meshletstart + (li);
  89.    
  90.         const vec4 v1 = ProcessVertexPosition(GetVertexPositionFromMesh(myMesh, gi));
  91.         const vec4 v2 = ProcessVertexPosition(GetVertexPositionFromMesh(myMesh, gi+1));
  92.         const vec4 v3 = ProcessVertexPosition(GetVertexPositionFromMesh(myMesh, gi+2));
  93.  
  94.        
  95.  
  96.         float max_x = max(v1.x,max(v2.x,v3.x)) / 2.0;
  97.         float max_y = max(v1.y,max(v2.y,v3.y)) / 2.0;
  98.  
  99.         float min_x = min(v1.x,min(v2.x,v3.x)) / 2.0;
  100.         float min_y = min(v1.y,min(v2.y,v3.y)) / 2.0;
  101.  
  102.         float px_max_x = round(max_x * info.viewport_x);
  103.         float px_max_y = round(max_y * info.viewport_y);
  104.        
  105.         float px_min_x = round(min_x * info.viewport_x);
  106.         float px_min_y = round(min_y * info.viewport_y);
  107.  
  108.         bool Culled = px_max_x == px_min_x ||px_max_y == px_min_y; 
  109.        
  110.         uvec4 vote = subgroupBallot(!Culled);
  111.  
  112.         const uint final_count = subgroupBallotBitCount(vote);
  113.        
  114.         if (final_count > 0) {
  115.             uint idx = subgroupBallotExclusiveBitCount(vote) *3;
  116.  
  117.             if (!Culled) {
  118.  
  119.                 gl_MeshVerticesNV[li].gl_Position   = v1;  
  120.                 gl_MeshVerticesNV[li+1].gl_Position = v2;  
  121.                 gl_MeshVerticesNV[li+2].gl_Position = v3;      
  122.  
  123.                 gl_PrimitiveIndicesNV[idx] =   li;
  124.                 gl_PrimitiveIndicesNV[idx+1] = li+1;
  125.                 gl_PrimitiveIndicesNV[idx+2] = li+2;
  126.  
  127.                 OUT[li].col   = GetVertexColorFromMesh(myMesh,gi);
  128.                 OUT[li+1].col = GetVertexColorFromMesh(myMesh,gi+1);
  129.                 OUT[li+2].col = GetVertexColorFromMesh(myMesh,gi+1);
  130.             }
  131.         }      
  132.        
  133.         //if(gl_LocalInvocationID.x == 0){         
  134.         gl_PrimitiveCountNV = final_count;
  135.         //}  
  136.     }
  137.     else {
  138.         gl_PrimitiveCountNV = 0;
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement