package rename + storage backend abstraction

This commit is contained in:
mcrcortex
2024-01-19 12:07:47 +10:00
parent 2345c8d692
commit e3bd036385
98 changed files with 268 additions and 242 deletions

View File

@@ -0,0 +1,5 @@
layout(binding=0) uniform sampler2D colourTexture;
in vec2 uv;
void main() {
gl_Colour = texture(colourTexture, uv);
}

View File

@@ -0,0 +1,4 @@
void main() {
gl_Position =
}

View File

@@ -0,0 +1,77 @@
struct Frustum {
vec4 planes[6];
};
layout(binding = 0, std140) uniform SceneUniform {
mat4 MVP;
ivec3 baseSectionPos;
int sectionCount;
Frustum frustum;
vec3 cameraSubPos;
uint frameId;
};
struct State {
uint biomeTintMsk;
uint faceColours[6];
};
struct Biome {
uint foliage;
uint water;
};
struct SectionMeta {
uvec4 header;
uvec4 drawdata;
};
//TODO: see if making the stride 2*4*4 bytes or something cause you get that 16 byte write
struct DrawCommand {
uint count;
uint instanceCount;
uint firstIndex;
int baseVertex;
uint baseInstance;
};
#ifndef Quad
#define Quad ivec2
#endif
layout(binding = 1, std430) readonly restrict buffer QuadBuffer {
Quad quadData[];
};
layout(binding = 2, std430) writeonly restrict buffer DrawBuffer {
DrawCommand cmdBuffer[];
};
layout(binding = 3, std430) readonly restrict buffer SectionBuffer {
SectionMeta sectionData[];
};
#ifndef VISIBILITY_ACCESS
#define VISIBILITY_ACCESS readonly
#endif
layout(binding = 4, std430) VISIBILITY_ACCESS restrict buffer VisibilityBuffer {
uint visibilityData[];
};
layout(binding = 5, std430) readonly restrict buffer StateBuffer {
State stateData[];
};
layout(binding = 6, std430) readonly restrict buffer BiomeBuffer {
Biome biomeData[];
};
layout(binding = 7, std430) readonly restrict buffer LightingBuffer {
uint lightData[];
};
vec4 getLighting(uint index) {
uvec4 arr = uvec4(lightData[index]);
arr = arr>>uvec4(16,8,0,24);
arr = arr & uvec4(0xFF);
return vec4(arr)*vec4(1.0f/255.0f);
}

View File

@@ -0,0 +1,74 @@
#version 460
#extension GL_ARB_gpu_shader_int64 : enable
layout(local_size_x = 128, local_size_y = 1, local_size_x = 1) in;
#import <zenith:lod/gl46/quad_format.glsl>
#import <zenith:lod/gl46/bindings.glsl>
#import <zenith:lod/gl46/frustum.glsl>
#import <zenith:lod/gl46/section.glsl>
//https://github.com/KhronosGroup/GLSL/blob/master/extensions/ext/GL_EXT_shader_16bit_storage.txt
// adds support for uint8_t which can use for compact visibility buffer
/*
uint count;
uint instanceCount;
uint firstIndex;
int baseVertex;
uint baseInstance;
*/
uint encodeLocalLodPos(uint detail, ivec3 pos) {
uvec3 detla = (pos - (baseSectionPos >> detail))&((1<<9)-1);
return (detail<<29)|(detla.x<<20)|(detla.y<<11)|(detla.z<<2);
}
//TODO: swap to a multidraw indirect counted
void main() {
if (gl_GlobalInvocationID.x >= sectionCount) {
return;
}
SectionMeta meta = sectionData[gl_GlobalInvocationID.x];
uint detail = extractDetail(meta);
ivec3 ipos = extractPosition(meta);
vec3 cornerPos = vec3(((ipos<<detail)-baseSectionPos)<<5)-cameraSubPos;
//Check the occlusion data from last frame
bool shouldRender = visibilityData[gl_GlobalInvocationID.x] == frameId - 1;
//Clear the occlusion data (not strictly? needed? i think???)
//visibilityData[gl_GlobalInvocationID.x] = 0;
//TODO: need to make it check that only if it was also in the frustum last frame does it apply the visibilityData check!
// this fixes temporal coherance
//This prevents overflow of the relative position encoder
if (shouldRender) {
shouldRender = !any(lessThan(ivec3(254), abs(ipos-(baseSectionPos>>detail))));
}
if (shouldRender) {
DrawCommand cmd;
cmd.count = extractQuadCount(meta) * 6;
cmd.instanceCount = 1;
cmd.firstIndex = 0;
cmd.baseVertex = int(extractQuadStart(meta))<<2;
cmd.baseInstance = encodeLocalLodPos(detail, ipos);
cmdBuffer[gl_GlobalInvocationID.x] = cmd;
} else {
DrawCommand cmd;
cmd.count = 0;
cmd.instanceCount = 0;
cmd.firstIndex = 0;
cmd.baseVertex = 0;
cmd.baseInstance = 0;
cmdBuffer[gl_GlobalInvocationID.x] = cmd;
}
}

View File

@@ -0,0 +1,12 @@
#version 460 core
#extension GL_ARB_gpu_shader_int64 : enable
#define VISIBILITY_ACCESS writeonly
#import <zenith:lod/gl46/bindings.glsl>
flat in uint id;
flat in uint value;
//out vec4 colour;
void main() {
visibilityData[id] = value;
//colour = vec4(float(id&7)/7, float((id>>3)&7)/7, float((id>>6)&7)/7, 1);
}

View File

@@ -0,0 +1,27 @@
#version 460 core
#extension GL_ARB_gpu_shader_int64 : enable
#define VISIBILITY_ACCESS writeonly
#import <zenith:lod/gl46/bindings.glsl>
#import <zenith:lod/gl46/section.glsl>
flat out uint id;
flat out uint value;
void main() {
uint sid = gl_InstanceID;
SectionMeta section = sectionData[sid];
uint detail = extractDetail(section);
ivec3 ipos = extractPosition(section);
//Transform ipos with respect to the vertex corner
ipos += ivec3(gl_VertexID&1, (gl_VertexID>>2)&1, (gl_VertexID>>1)&1);
vec3 cornerPos = vec3(((ipos<<detail)-baseSectionPos)<<5);
gl_Position = MVP * vec4(cornerPos,1);
//Write to this id
id = sid;
value = frameId;
}

View File

@@ -0,0 +1,13 @@
bool testFrustumPoint(vec4 plane, vec3 min, vec3 max) {
vec3 point = mix(max, min, lessThan(plane.xyz, vec3(0))) * plane.xyz;
return (point.x + point.y + point.z) >= -plane.w;
}
bool testFrustum(Frustum frust, vec3 min, vec3 max) {
return testFrustumPoint(frust.planes[0], min, max) &&
testFrustumPoint(frust.planes[1], min, max) &&
testFrustumPoint(frust.planes[2], min, max) &&
testFrustumPoint(frust.planes[3], min, max) &&
testFrustumPoint(frust.planes[4], min, max) &&
testFrustumPoint(frust.planes[5], min, max);
}

View File

@@ -0,0 +1,71 @@
#ifdef GL_ARB_gpu_shader_int64
#define Quad uint64_t
#define Eu32(data, amountBits, shift) (uint((data)>>(shift))&((1u<<(amountBits))-1))
vec3 extractPos(uint64_t quad) {
//TODO: pull out the majic constants into #defines (specifically the shift amount)
return vec3(Eu32(quad, 5, 21), Eu32(quad, 5, 16), Eu32(quad, 5, 11));
}
ivec2 extractSize(uint64_t quad) {
return ivec2(Eu32(quad, 4, 3), Eu32(quad, 4, 7)) + ivec2(1);//the + 1 is cause you cant actually have a 0 size quad
}
uint extractFace(uint64_t quad) {
return Eu32(quad, 3, 0);
}
uint extractStateId(uint64_t quad) {
return Eu32(quad, 20, 26);
}
uint extractBiomeId(uint64_t quad) {
return Eu32(quad, 9, 46);
}
uint extractLightId(uint64_t quad) {
return Eu32(quad, 8, 55);
}
#else
//TODO: FIXME, ivec2 swaps around the data of the x and y cause its written in little endian
#define Quad ivec2
//#define Eu32(data, amountBits, shift) (uint((data)>>(shift))&((1u<<(amountBits))-1))
uint Eu32v(ivec2 data, int amount, int shift) {
if (shift > 31) {
shift -= 32;
return (uint(data.y)>>uint(shift))&((1u<<uint(amount))-1);
} else {
return (uint(data.x)>>uint(shift))&((1u<<uint(amount))-1);
}
}
vec3 extractPos(ivec2 quad) {
return vec3(Eu32v(quad, 5, 21), Eu32v(quad, 5, 16), Eu32v(quad, 5, 11));
}
ivec2 extractSize(ivec2 quad) {
return ivec2(Eu32v(quad, 4, 3), Eu32v(quad, 4, 7)) + ivec2(1);//the + 1 is cause you cant actually have a 0 size quad
}
uint extractFace(ivec2 quad) {
return Eu32v(quad, 3, 0);
}
uint extractStateId(ivec2 quad) {
//Eu32(quad, 20, 26);
return 1;
}
uint extractBiomeId(ivec2 quad) {
return Eu32v(quad, 9, 46);
}
uint extractLightId(ivec2 quad) {
return Eu32v(quad, 8, 55);
}
#endif

View File

@@ -0,0 +1,8 @@
#version 460 core
layout(location = 0) in flat vec4 colour;
layout(location = 0) out vec4 outColour;
void main() {
//TODO: randomly discard the fragment with respect to the alpha value
outColour = colour;
}

View File

@@ -0,0 +1,104 @@
#version 460 core
#extension GL_ARB_gpu_shader_int64 : enable
#import <zenith:lod/gl46/quad_format.glsl>
#import <zenith:lod/gl46/bindings.glsl>
layout(location = 0) out flat vec4 colour;
uint extractLodLevel() {
return uint(gl_BaseInstance)>>29;
}
//Note the last 2 bits of gl_BaseInstance are unused
//Gives a relative position of +-255 relative to the player center in its respective lod
ivec3 extractRelativeLodPos() {
return (ivec3(gl_BaseInstance)<<ivec3(3,12,21))>>ivec3(23);
}
vec4 uint2vec4RGBA(uint colour) {
return vec4((uvec4(colour)>>uvec4(24,16,8,0))&uvec4(0xFF))/255;
}
void main() {
int cornerIdx = gl_VertexID&3;
Quad quad = quadData[uint(gl_VertexID)>>2];
vec3 innerPos = extractPos(quad);
uint face = extractFace(quad);
uint lodLevel = extractLodLevel();
ivec3 lodCorner = ((extractRelativeLodPos()<<lodLevel) - (baseSectionPos&(ivec3((1<<lodLevel)-1))))<<5;
vec3 corner = innerPos * (1<<lodLevel) + lodCorner;
//TODO: see if backface culling is even needed, since everything (should) be back culled already
//Flip the quad rotation by its face (backface culling)
if ((face&1) != 0) {
cornerIdx ^= 1;
}
if ((face>>1) == 0) {
cornerIdx ^= 1;
}
ivec2 size = extractSize(quad) * ivec2((cornerIdx>>1)&1, cornerIdx&1) * (1<<lodLevel);
vec3 pos = corner;
//NOTE: can just make instead of face, make it axis (can also make it 2 bit instead of 3 bit then)
// since the only reason face is needed is to ensure backface culling orientation thing
uint axis = face>>1;
if (axis == 0) {
pos.xz += size;
pos.y += (face&1)<<lodLevel;
} else if (axis == 1) {
pos.xy += size;
pos.z += (face&1)<<lodLevel;
} else {
pos.yz += size;
pos.x += (face&1)<<lodLevel;
}
gl_Position = MVP * vec4(pos,1);
uint stateId = extractStateId(quad);
uint biomeId = extractBiomeId(quad);
State stateInfo = stateData[stateId];
colour = uint2vec4RGBA(stateInfo.faceColours[face]);
colour *= getLighting(extractLightId(quad));
if (((stateInfo.biomeTintMsk>>face)&1) == 1) {
vec4 biomeColour = uint2vec4RGBA(biomeData[biomeId].foliage);
colour *= biomeColour;
}
//Apply water tint
if (((stateInfo.biomeTintMsk>>6)&1) == 1) {
colour *= vec4(0.247, 0.463, 0.894, 1);
}
//Apply face tint
if (face == 0) {
colour.xyz *= vec3(0.75, 0.75, 0.75);
} else if (face != 1) {
colour.xyz *= vec3((float(face-2)/4)*0.5 + 0.5);
}
}
//gl_Position = MVP * vec4(vec3(((cornerIdx)&1)+10,10,((cornerIdx>>1)&1)+10),1);
//uint i = uint(quad>>32);
//uint i = uint(gl_BaseInstance);
//i ^= i>>16;
//i *= 124128573;
//i ^= i>>16;
//i *= 4211346123;
//i ^= i>>16;
//i *= 462312435;
//i ^= i>>16;
//i *= 542354341;
//i ^= i>>16;
//uint i = uint(lodLevel+12)*215387625;
//colour *= vec4(vec3(float((uint(i)>>2)&7)/7,float((uint(i)>>5)&7)/7,float((uint(i)>>8)&7)/7)*0.7+0.3,1);
//colour = vec4(vec3(float((uint(i)>>2)&7)/7,float((uint(i)>>5)&7)/7,float((uint(i)>>8)&7)/7),1);

View File

@@ -0,0 +1,21 @@
uint extractDetail(SectionMeta section) {
return section.header.x>>28;
}
ivec3 extractPosition(SectionMeta section) {
int y = ((int(section.header.x)<<4)>>24);
int x = (int(section.header.y)<<4)>>8;
int z = int((section.header.x&((1<<20)-1))<<4);
z |= int(section.header.y>>28);
z <<= 8;
z >>= 8;
return ivec3(x,y,z);
}
uint extractQuadStart(SectionMeta meta) {
return meta.header.z;
}
uint extractQuadCount(SectionMeta meta) {
return meta.header.w;
}

View File

@@ -0,0 +1,11 @@
#version 460
layout(local_size_x = 32, local_size_y = 32, local_size_x = 1) in;
layout(binding=0,rgba8) restrict uniform image2D colourTexture;
layout(binding=1) uniform sampler2D depthTexture;
void main() {
float d = texture2D(depthTexture, vec2(gl_GlobalInvocationID.xy)/vec2(1024)).x;
d = 1/d;
d = d/2;
imageStore(colourTexture, ivec2(gl_GlobalInvocationID.xy), vec4(d,d,d,1));
}

View File

@@ -0,0 +1,8 @@
#version 460 core
layout(location = 0) in flat vec4 colour;
layout(location = 0) out vec4 outColour;
void main() {
//TODO: randomly discard the fragment with respect to the alpha value
outColour = colour;
}

View File

@@ -0,0 +1,18 @@
#version 460 core
layout(location = 0) out flat vec4 colour;
layout(binding = 0, std140) uniform SceneUniform {
mat4 MVP;
ivec3 baseSectionPos;
int _pad1;
};
void main() {
int cornerIdx = gl_VertexID&3;
gl_Position = MVP * vec4(vec3(cornerIdx&1,((cornerIdx>>1)&1),0),1);
colour = vec4(float((uint(gl_VertexID)>>2)&7)/7,float((uint(gl_VertexID)>>5)&7)/7,float((uint(gl_VertexID)>>8)&7)/7,1);
}

View File

@@ -0,0 +1,28 @@
//Contains the definision of a ray and step functions
struct Ray {
ivec3 pos;
vec3 innerPos;
vec3 dir;
vec3 invDir;
};
Ray ray;
void setup(vec3 origin, vec3 direction) {
ray.pos = ivec3(origin);
ray.innerPos = origin - ray.pos;
direction *= inversesqrt(direction);
ray.dir = direction;
ray.invDir = 1/direction;
}
void step(ivec3 aabb) {
//TODO:check for innerPos>=1 and step into that voxel
vec3 t2f = (aabb - ray.innerPos) * ray.invDir;
float mint2f = min(t2f.x, min(t2f.y, t2f.z));
bvec3 msk = lessThanEqual(t2f.xyz, vec3(mint2f));
vec3 newIP = mint2f * ray.dir + ray.innerPos;
ivec3 offset = min(aabb-1, ivec3(newIP));
ray.pos += offset + ivec3(msk);
ray.innerPos = mix(vec3(0), newIP - offset, not(msk));
}

View File

@@ -0,0 +1,8 @@
struct Voxel {
};
//TODO: add tlas and blas voxel fetching (rings and all)
void getVoxel() {
}

View File

@@ -0,0 +1,2 @@
//Glue code between ray stepper and voxel storage
// its the primary ray tracer