Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

GeometryPool.hlsl 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef GEOMETRY_POOL_H
  2. #define GEOMETRY_POOL_H
  3. namespace GeometryPool
  4. {
  5. float2 msign(float2 v)
  6. {
  7. return float2(
  8. (v.x >= 0.0) ? 1.0 : -1.0,
  9. (v.y >= 0.0) ? 1.0 : -1.0);
  10. }
  11. uint NormalToOctahedral32(float3 normal)
  12. {
  13. normal.xy /= (abs(normal.x) + abs(normal.y) + abs(normal.z));
  14. normal.xy = (normal.z >= 0.0) ? normal.xy : (1.0 - abs(normal.yx)) * msign(normal.xy);
  15. uint2 d = uint2(round(32767.5 + normal.xy * 32767.5));
  16. return d.x | (d.y << 16u);
  17. }
  18. float3 Octahedral32ToNormal(uint data)
  19. {
  20. uint2 iv = uint2(data, data >> 16u) & 65535u;
  21. float2 v = float2(iv) / 32767.5 - 1.0;
  22. float3 normal = float3(v, 1.0 - abs(v.x) - abs(v.y));
  23. float t = max(-normal.z, 0.0);
  24. normal.x += (normal.x > 0.0) ? -t : t;
  25. normal.y += (normal.y > 0.0) ? -t : t;
  26. return normalize(normal);
  27. }
  28. void StoreVertex(
  29. uint vertexIndex,
  30. in GeoPoolVertex vertex,
  31. int outputBufferSize,
  32. RWStructuredBuffer<uint> output)
  33. {
  34. uint posIndex = vertexIndex * GEO_POOL_VERTEX_BYTE_SIZE / 4;
  35. output[posIndex] = asuint(vertex.pos.x);
  36. output[posIndex+1] = asuint(vertex.pos.y);
  37. output[posIndex+2] = asuint(vertex.pos.z);
  38. uint uv0Index = (vertexIndex * GEO_POOL_VERTEX_BYTE_SIZE + GEO_POOL_UV0BYTE_OFFSET) / 4;
  39. output[uv0Index] = asuint(vertex.uv0.x);
  40. output[uv0Index + 1] = asuint(vertex.uv0.y);
  41. output[uv0Index + 2] = asuint(vertex.uv0.z);
  42. output[uv0Index + 3] = asuint(vertex.uv0.w);
  43. uint uv1Index = (vertexIndex * GEO_POOL_VERTEX_BYTE_SIZE + GEO_POOL_UV1BYTE_OFFSET) / 4;
  44. output[uv1Index] = asuint(vertex.uv1.x);
  45. output[uv1Index + 1] = asuint(vertex.uv1.y);
  46. output[uv1Index + 2] = asuint(vertex.uv1.z);
  47. output[uv1Index + 3] = asuint(vertex.uv1.w);
  48. uint normalIndex = (vertexIndex * GEO_POOL_VERTEX_BYTE_SIZE + GEO_POOL_NORMAL_BYTE_OFFSET) / 4;
  49. output[normalIndex] = NormalToOctahedral32(vertex.N);
  50. }
  51. void LoadVertex(
  52. uint vertexIndex,
  53. int vertexFlags,
  54. StructuredBuffer<uint> vertexBuffer,
  55. out GeoPoolVertex outputVertex)
  56. {
  57. uint posIndex = vertexIndex * GEO_POOL_VERTEX_BYTE_SIZE / 4;
  58. float3 pos = asfloat(uint3(vertexBuffer[posIndex], vertexBuffer[posIndex + 1], vertexBuffer[posIndex + 2]));
  59. uint uv0Index = (vertexIndex * GEO_POOL_VERTEX_BYTE_SIZE + GEO_POOL_UV0BYTE_OFFSET) / 4;
  60. float4 uv0 = asfloat(uint4(vertexBuffer[uv0Index], vertexBuffer[uv0Index + 1], vertexBuffer[uv0Index + 2], vertexBuffer[uv0Index + 3]));
  61. uint uv1Index = (vertexIndex * GEO_POOL_VERTEX_BYTE_SIZE + GEO_POOL_UV1BYTE_OFFSET) / 4;
  62. float4 uv1 = asfloat(uint4(vertexBuffer[uv1Index], vertexBuffer[uv1Index + 1], vertexBuffer[uv1Index + 2], vertexBuffer[uv1Index + 3]));
  63. uint normalIndex = (vertexIndex * GEO_POOL_VERTEX_BYTE_SIZE + GEO_POOL_NORMAL_BYTE_OFFSET) / 4;
  64. uint normal = uint(vertexBuffer[normalIndex]);
  65. outputVertex.pos = pos;
  66. outputVertex.uv0 = uv0;
  67. outputVertex.uv1 = uv1;
  68. outputVertex.N = Octahedral32ToNormal(normal);
  69. }
  70. }
  71. #endif