Add GPU renderer

This commit is contained in:
Adam
2018-11-15 09:19:37 -05:00
parent 3c84d98638
commit 3f5e273349
74 changed files with 6201 additions and 22 deletions

View File

@@ -0,0 +1,166 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include to_screen.glsl
/*
* Rotate a vertex by a given orientation in JAU
*/
ivec4 rotate(ivec4 vertex, int orientation) {
int s = int(65536.0f * sin(orientation * UNIT));
int c = int(65536.0f * cos(orientation * UNIT));
int x = vertex.z * s + vertex.x * c >> 16;
int z = vertex.z * c - vertex.x * s >> 16;
return ivec4(x, vertex.y, z, vertex.w);
}
/*
* Calculate the distance to a vertex given the camera angle
*/
int distance(ivec4 vertex, int cameraYaw, int cameraPitch) {
int yawSin = int(65536.0f * sin(cameraYaw * UNIT));
int yawCos = int(65536.0f * cos(cameraYaw * UNIT));
int pitchSin = int(65536.0f * sin(cameraPitch * UNIT));
int pitchCos = int(65536.0f * cos(cameraPitch * UNIT));
int j = vertex.z * yawCos - vertex.x * yawSin >> 16;
int l = vertex.y * pitchSin + j * pitchCos >> 16;
return l;
}
/*
* Calculate the distance to a face
*/
int face_distance(ivec4 vA, ivec4 vB, ivec4 vC, int cameraYaw, int cameraPitch) {
int dvA = distance(vA, cameraYaw, cameraPitch);
int dvB = distance(vB, cameraYaw, cameraPitch);
int dvC = distance(vC, cameraYaw, cameraPitch);
int faceDistance = (dvA + dvB + dvC) / 3;
return faceDistance;
}
/*
* Test if a face is visible (not backward facing)
*/
bool face_visible(ivec4 vA, ivec4 vB, ivec4 vC, ivec4 position, int cameraYaw, int cameraPitch, int centerX, int centerY, int zoom) {
vA += position;
vB += position;
vC += position;
ivec3 sA = toScreen(vA.xyz, cameraYaw, cameraPitch, centerX, centerY, zoom);
ivec3 sB = toScreen(vB.xyz, cameraYaw, cameraPitch, centerX, centerY, zoom);
ivec3 sC = toScreen(vC.xyz, cameraYaw, cameraPitch, centerX, centerY, zoom);
return (sA.x - sB.x) * (sC.y - sB.y) - (sC.x - sB.x) * (sA.y - sB.y) > 0;
}
// Calculate adjusted priority for a face with a given priority, distance, and
// model global min10 and face distance averages. This allows positioning faces
// with priorities 10/11 into the correct 'slots' resulting in 18 possible
// adjusted priorities
int priority_map(int p, int distance, int _min10, int avg1, int avg2, int avg3) {
// (10, 11) 0 1 2 (10, 11) 3 4 (10, 11) 5 6 7 8 9 (10, 11)
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
switch (p) {
case 0: return 2;
case 1: return 3;
case 2: return 4;
case 3: return 7;
case 4: return 8;
case 5: return 11;
case 6: return 12;
case 7: return 13;
case 8: return 14;
case 9: return 15;
case 10:
if (distance > avg1) {
return 0;
} else if (distance > avg2) {
return 5;
} else if (distance > avg3) {
return 9;
} else {
return 16;
}
case 11:
if (distance > avg1 && _min10 > avg1) {
return 1;
} else if (distance > avg2 && (_min10 > avg1 || _min10 > avg2)) {
return 6;
} else if (distance > avg3 && (_min10 > avg1 || _min10 > avg2 || _min10 > avg3)) {
return 10;
} else {
return 17;
}
default:
return -1;
}
}
// calculate the number of faces with a lower adjusted priority than
// the given adjusted priority
int count_prio_offset(int priority) {
int total = 0;
switch (priority) {
case 17:
total += totalMappedNum[16];
case 16:
total += totalMappedNum[15];
case 15:
total += totalMappedNum[14];
case 14:
total += totalMappedNum[13];
case 13:
total += totalMappedNum[12];
case 12:
total += totalMappedNum[11];
case 11:
total += totalMappedNum[10];
case 10:
total += totalMappedNum[9];
case 9:
total += totalMappedNum[8];
case 8:
total += totalMappedNum[7];
case 7:
total += totalMappedNum[6];
case 6:
total += totalMappedNum[5];
case 5:
total += totalMappedNum[4];
case 4:
total += totalMappedNum[3];
case 3:
total += totalMappedNum[2];
case 2:
total += totalMappedNum[1];
case 1:
total += totalMappedNum[0];
case 0:
return total;
}
}

View File

@@ -0,0 +1,150 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#version 430 core
#define PI 3.1415926535897932384626433832795f
#define UNIT PI / 1024.0f
layout(std140) uniform uniforms {
int cameraYaw;
int cameraPitch;
int centerX;
int centerY;
int zoom;
};
shared int totalNum[12]; // number of faces with a given priority
shared int totalDistance[12]; // sum of distances to faces of a given priority
shared int totalMappedNum[18]; // number of faces with a given adjusted priority
shared int min10; // minimum distance to a face of priority 10
shared int dfs[4096]; // packed face id and distance
struct modelinfo {
int offset; // offset into buffer
int uvOffset; // offset into uv buffer
int length; // length in faces
int idx; // write idx in target buffer
int flags; // radius, orientation
int x; // scene position x
int y; // scene position y
int z; // scene position z
};
layout(std430, binding = 0) readonly buffer modelbuffer_in {
modelinfo ol[];
};
layout(std430, binding = 1) readonly buffer vertexbuffer_in {
ivec4 vb[];
};
layout(std430, binding = 2) readonly buffer tempvertexbuffer_in {
ivec4 tempvb[];
};
layout(std430, binding = 3) writeonly buffer vertex_out {
ivec4 vout[];
};
layout(std430, binding = 4) writeonly buffer uv_out {
vec4 uvout[];
};
layout(std430, binding = 5) readonly buffer uvbuffer_in {
vec4 uv[];
};
layout(std430, binding = 6) readonly buffer tempuvbuffer_in {
vec4 tempuv[];
};
layout(local_size_x = 1024) in;
#include common.glsl
#include priority_render.glsl
void main() {
uint groupId = gl_WorkGroupID.x;
uint localId = gl_LocalInvocationID.x * 4;
modelinfo minfo = ol[groupId];
int length = minfo.length;
if (localId == 0) {
min10 = 1600;
for (int i = 0; i < 12; ++i) {
totalNum[i] = 0;
totalDistance[i] = 0;
}
for (int i = 0; i < 18; ++i) {
totalMappedNum[i] = 0;
}
}
memoryBarrierShared();
barrier();
int prio1, dis1, prio1Adj;
ivec4 vA1, vA2, vA3;
int prio2, dis2, prio2Adj;
ivec4 vB1, vB2, vB3;
int prio3, dis3, prio3Adj;
ivec4 vC1, vC2, vC3;
int prio4, dis4, prio4Adj;
ivec4 vD1, vD2, vD3;
get_face(localId, minfo, cameraYaw, cameraPitch, centerX, centerY, zoom, prio1, dis1, vA1, vA2, vA3);
get_face(localId + 1, minfo, cameraYaw, cameraPitch, centerX, centerY, zoom, prio2, dis2, vB1, vB2, vB3);
get_face(localId + 2, minfo, cameraYaw, cameraPitch, centerX, centerY, zoom, prio3, dis3, vC1, vC2, vC3);
get_face(localId + 3, minfo, cameraYaw, cameraPitch, centerX, centerY, zoom, prio4, dis4, vD1, vD2, vD3);
memoryBarrierShared();
barrier();
int idx1 = map_face_priority(localId, minfo, prio1, dis1, prio1Adj);
int idx2 = map_face_priority(localId + 1, minfo, prio2, dis2, prio2Adj);
int idx3 = map_face_priority(localId + 2, minfo, prio3, dis3, prio3Adj);
int idx4 = map_face_priority(localId + 3, minfo, prio4, dis4, prio4Adj);
memoryBarrierShared();
barrier();
insert_dfs(localId , minfo, prio1Adj, dis1, idx1);
insert_dfs(localId + 1, minfo, prio2Adj, dis2, idx2);
insert_dfs(localId + 2, minfo, prio3Adj, dis3, idx3);
insert_dfs(localId + 3, minfo, prio4Adj, dis4, idx4);
memoryBarrierShared();
barrier();
sort_and_insert(localId , minfo, prio1Adj, dis1, vA1, vA2, vA3);
sort_and_insert(localId + 1, minfo, prio2Adj, dis2, vB1, vB2, vB3);
sort_and_insert(localId + 2, minfo, prio3Adj, dis3, vC1, vC2, vC3);
sort_and_insert(localId + 3, minfo, prio4Adj, dis4, vD1, vD2, vD3);
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#version 430 core
#define PI 3.1415926535897932384626433832795f
#define UNIT PI / 1024.0f
layout(std140) uniform uniforms {
int cameraYaw;
int cameraPitch;
int centerX;
int centerY;
int zoom;
};
shared int totalNum[12]; // number of faces with a given priority
shared int totalDistance[12]; // sum of distances to faces of a given priority
shared int totalMappedNum[18]; // number of faces with a given adjusted priority
shared int min10; // minimum distance to a face of priority 10
shared int dfs[512]; // packed face id and distance
struct modelinfo {
int offset; // offset into buffer
int uvOffset; // offset into uv buffer
int length; // length in faces
int idx; // write idx in target buffer
int flags; // radius, orientation
int x; // scene position x
int y; // scene position y
int z; // scene position z
};
layout(std430, binding = 0) readonly buffer modelbuffer_in {
modelinfo ol[];
};
layout(std430, binding = 1) readonly buffer vertexbuffer_in {
ivec4 vb[];
};
layout(std430, binding = 2) readonly buffer tempvertexbuffer_in {
ivec4 tempvb[];
};
layout(std430, binding = 3) writeonly buffer vertex_out {
ivec4 vout[];
};
layout(std430, binding = 4) writeonly buffer uv_out {
vec4 uvout[];
};
layout(std430, binding = 5) readonly buffer uvbuffer_in {
vec4 uv[];
};
layout(std430, binding = 6) readonly buffer tempuvbuffer_in {
vec4 tempuv[];
};
layout(local_size_x = 512) in;
#include common.glsl
#include priority_render.glsl
void main() {
uint groupId = gl_WorkGroupID.x;
uint localId = gl_LocalInvocationID.x;
modelinfo minfo = ol[groupId];
if (localId == 0) {
min10 = 1600;
for (int i = 0; i < 12; ++i) {
totalNum[i] = 0;
totalDistance[i] = 0;
}
for (int i = 0; i < 18; ++i) {
totalMappedNum[i] = 0;
}
}
memoryBarrierShared();
barrier();
int prio1, dis1, prio1Adj;
ivec4 vA1, vA2, vA3;
get_face(localId, minfo, cameraYaw, cameraPitch, centerX, centerY, zoom, prio1, dis1, vA1, vA2, vA3);
memoryBarrierShared();
barrier();
int idx1 = map_face_priority(localId, minfo, prio1, dis1, prio1Adj);
memoryBarrierShared();
barrier();
insert_dfs(localId, minfo, prio1Adj, dis1, idx1);
memoryBarrierShared();
barrier();
sort_and_insert(localId, minfo, prio1Adj, dis1, vA1, vA2, vA3);
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#version 400
uniform sampler2DArray textures;
uniform vec2 textureOffsets[64];
uniform float brightness;
in vec4 Color;
in vec4 fUv;
out vec4 FragColor;
void main() {
float n = fUv.x;
float u = fUv.y;
float v = fUv.z;
if (u > 0.0f && v > 0.0f) {
int textureIdx = int(n);
vec2 uv = vec2(u - 1, v - 1);
vec2 animatedUv = uv + textureOffsets[textureIdx];
vec4 textureColor = texture(textures, vec3(animatedUv, n));
vec4 textureColorBrightness = pow(textureColor, vec4(brightness, brightness, brightness, 1.0f));
FragColor = textureColorBrightness * Color;
} else {
FragColor = Color;
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#version 330
uniform sampler2D tex;
in vec2 TexCoord;
out vec4 FragColor;
void main() {
vec4 c = texture(tex, TexCoord);
FragColor = c;
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#version 400
#define PI 3.1415926535897932384626433832795f
#define UNIT PI / 1024.0f
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
layout(std140) uniform Uniforms {
int cameraYaw;
int cameraPitch;
int centerX;
int centerY;
int zoom;
};
uniform mat4 projectionMatrix;
in ivec3 vPosition[];
in vec4 vColor[];
in vec4 vUv[];
out vec4 Color;
out vec4 fUv;
#include to_screen.glsl
void main() {
ivec3 screenA = toScreen(vPosition[0], cameraYaw, cameraPitch, centerX, centerY, zoom);
ivec3 screenB = toScreen(vPosition[1], cameraYaw, cameraPitch, centerX, centerY, zoom);
ivec3 screenC = toScreen(vPosition[2], cameraYaw, cameraPitch, centerX, centerY, zoom);
if (-screenA.z < 50 || -screenB.z < 50 || -screenC.z < 50) {
// the client does not draw a triangle if any vertex distance is <50
return;
}
vec4 tmp = vec4(screenA.xyz, 1.0);
Color = vColor[0];
fUv = vUv[0];
gl_Position = projectionMatrix * tmp;
EmitVertex();
tmp = vec4(screenB.xyz, 1.0);
Color = vColor[1];
fUv = vUv[1];
gl_Position = projectionMatrix * tmp;
EmitVertex();
tmp = vec4(screenC.xyz, 1.0);
Color = vColor[2];
fUv = vUv[2];
gl_Position = projectionMatrix * tmp;
EmitVertex();
EndPrimitive();
}

View File

@@ -0,0 +1,205 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
void get_face(uint localId, modelinfo minfo, int cameraYaw, int cameraPitch, int centerX, int centerY, int zoom,
out int prio, out int dis, out ivec4 o1, out ivec4 o2, out ivec4 o3) {
int offset = minfo.offset;
int length = minfo.length;
int flags = minfo.flags;
int radius = (flags & 0x7fffffff) >> 12;
int orientation = flags & 0x7ff;
ivec4 pos = ivec4(minfo.x, minfo.y, minfo.z, 0);
uint ssboOffset;
if (localId < length) {
ssboOffset = localId;
} else {
ssboOffset = 0;
}
ivec4 thisA;
ivec4 thisB;
ivec4 thisC;
// Grab triangle vertices from the correct buffer
if (flags < 0) {
thisA = vb[offset + ssboOffset * 3 ];
thisB = vb[offset + ssboOffset * 3 + 1];
thisC = vb[offset + ssboOffset * 3 + 2];
} else {
thisA = tempvb[offset + ssboOffset * 3 ];
thisB = tempvb[offset + ssboOffset * 3 + 1];
thisC = tempvb[offset + ssboOffset * 3 + 2];
}
ivec4 thisrvA;
ivec4 thisrvB;
ivec4 thisrvC;
int thisPriority, thisDistance;
if (localId < length) {
// rotate for model orientation
thisrvA = rotate(thisA, orientation);
thisrvB = rotate(thisB, orientation);
thisrvC = rotate(thisC, orientation);
// calculate distance to face
thisPriority = (thisA.w >> 16) & 0xff; // all vertices on the face have the same priority
if (radius == 0) {
thisDistance = 0;
} else {
thisDistance = face_distance(thisrvA, thisrvB, thisrvC, cameraYaw, cameraPitch) + radius;
}
// if the face is not culled, it is calculated into priority distance averages
if (face_visible(thisrvA, thisrvB, thisrvC, pos, cameraYaw, cameraPitch, centerX, centerY, zoom)) {
atomicAdd(totalNum[thisPriority], 1);
atomicAdd(totalDistance[thisPriority], thisDistance);
// calculate minimum distance to any face of priority 10 for positioning the 11 faces later
if (thisPriority == 10) {
atomicMin(min10, thisDistance);
}
}
o1 = thisrvA;
o2 = thisrvB;
o3 = thisrvC;
prio = thisPriority;
dis = thisDistance;
} else {
prio = 0;
dis = 0;
}
}
int map_face_priority(uint localId, modelinfo minfo, int thisPriority, int thisDistance, out int prio) {
int length = minfo.length;
// Compute average distances for 0/2, 3/4, and 6/8
int adjPrio;
int prioIdx;
if (localId < length) {
int avg1 = 0;
int avg2 = 0;
int avg3 = 0;
if (totalNum[1] > 0 || totalNum[2] > 0) {
avg1 = (totalDistance[1] + totalDistance[2]) / (totalNum[1] + totalNum[2]);
}
if (totalNum[3] > 0 || totalNum[4] > 0) {
avg2 = (totalDistance[3] + totalDistance[4]) / (totalNum[3] + totalNum[4]);
}
if (totalNum[6] > 0 || totalNum[8] > 0) {
avg3 = (totalDistance[6] + totalDistance[8]) / (totalNum[6] + totalNum[8]);
}
int _min10 = min10;
adjPrio = priority_map(thisPriority, thisDistance, _min10, avg1, avg2, avg3);
int prioIdx = atomicAdd(totalMappedNum[adjPrio], 1);
prio = adjPrio;
return prioIdx;
}
return 0;
}
void insert_dfs(uint localId, modelinfo minfo, int adjPrio, int distance, int prioIdx) {
int length = minfo.length;
if (localId < length) {
// calculate base offset into dfs based on number of faces with a lower priority
int baseOff = count_prio_offset(adjPrio);
// store into face array offset array by unique index
dfs[baseOff + prioIdx] = (int(localId) << 16) | distance;
}
}
void sort_and_insert(uint localId, modelinfo minfo, int thisPriority, int thisDistance, ivec4 thisrvA, ivec4 thisrvB, ivec4 thisrvC) {
/* compute face distance */
int length = minfo.length;
int outOffset = minfo.idx;
int uvOffset = minfo.uvOffset;
int flags = minfo.flags;
ivec4 pos = ivec4(minfo.x, minfo.y, minfo.z, 0);
int start, end, myOffset;
if (localId < length) {
const int priorityOffset = count_prio_offset(thisPriority);
const int numOfPriority = totalMappedNum[thisPriority];
start = priorityOffset; // index of first face with this priority
end = priorityOffset + numOfPriority; // index of last face with this priority
myOffset = priorityOffset;
} else {
start = end = myOffset = 0;
}
if (localId < length) {
// we only have to order faces against others of the same priority
// calculate position this face will be in
for (int i = start; i < end; ++i) {
int d1 = dfs[i];
int theirId = d1 >> 16;
int theirDistance = d1 & 0xffff;
// the closest faces draw last, so have the highest index
// if two faces have the same distance, the one with the
// higher id draws last
if ((theirDistance > thisDistance)
|| (theirDistance == thisDistance && theirId < localId)) {
++myOffset;
}
}
// position vertices in scene and write to out buffer
vout[outOffset + myOffset * 3] = pos + thisrvA;
vout[outOffset + myOffset * 3 + 1] = pos + thisrvB;
vout[outOffset + myOffset * 3 + 2] = pos + thisrvC;
if (uvOffset < 0) {
uvout[outOffset + myOffset * 3] = vec4(0, 0, 0, 0);
uvout[outOffset + myOffset * 3 + 1] = vec4(0, 0, 0, 0);
uvout[outOffset + myOffset * 3 + 2] = vec4(0, 0, 0, 0);
} else if (flags >= 0) {
uvout[outOffset + myOffset * 3] = tempuv[uvOffset + localId * 3];
uvout[outOffset + myOffset * 3 + 1] = tempuv[uvOffset + localId * 3 + 1];
uvout[outOffset + myOffset * 3 + 2] = tempuv[uvOffset + localId * 3 + 2];
} else {
uvout[outOffset + myOffset * 3] = uv[uvOffset + localId * 3];
uvout[outOffset + myOffset * 3 + 1] = uv[uvOffset + localId * 3 + 1];
uvout[outOffset + myOffset * 3 + 2] = uv[uvOffset + localId * 3 + 2];
}
}
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Convert a vertex to screen space
*/
ivec3 toScreen(ivec3 vertex, int cameraYaw, int cameraPitch, int centerX, int centerY, int zoom) {
int yawSin = int(65536.0f * sin(cameraYaw * UNIT));
int yawCos = int(65536.0f * cos(cameraYaw * UNIT));
int pitchSin = int(65536.0f * sin(cameraPitch * UNIT));
int pitchCos = int(65536.0f * cos(cameraPitch * UNIT));
int rotatedX = ((vertex.z * yawSin) + (vertex.x * yawCos)) >> 16;
int rotatedZ = ((vertex.z * yawCos) - (vertex.x * yawSin)) >> 16;
int var13 = ((vertex.y * pitchCos) - (rotatedZ * pitchSin)) >> 16;
int var12 = ((vertex.y * pitchSin) + (rotatedZ * pitchCos)) >> 16;
int x = rotatedX * zoom / var12 + centerX;
int y = var13 * zoom / var12 + centerY;
int z = -var12; // in OpenGL depth is negative
return ivec3(x, y, z);
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#version 400
layout (location = 0) in ivec4 VertexPosition;
layout (location = 1) in vec4 uv;
uniform float brightness;
out ivec3 vPosition;
out vec4 vColor;
out vec4 vUv;
vec3 hslToRgb(int hsl) {
int var5 = hsl/128;
float var6 = float(var5 >> 3) / 64.0f + 0.0078125f;
float var8 = float(var5 & 7) / 8.0f + 0.0625f;
int var10 = hsl % 128;
float var11 = float(var10) / 128.0f;
float var13 = var11;
float var15 = var11;
float var17 = var11;
if(var8 != 0.0f) {
float var19;
if(var11 < 0.5f) {
var19 = var11 * (1.0f + var8);
} else {
var19 = var11 + var8 - var11 * var8;
}
float var21 = 2.0f * var11 - var19;
float var23 = var6 + 0.3333333333333333f;
if(var23 > 1.0f) {
var23 -= 1.f;
}
float var27 = var6 - 0.3333333333333333f;
if(var27 < 0.0f) {
var27 += 1.f;
}
if(6.0f * var23 < 1.0f) {
var13 = var21 + (var19 - var21) * 6.0f * var23;
} else if(2.0f * var23 < 1.0f) {
var13 = var19;
} else if(3.0f * var23 < 2.0f) {
var13 = var21 + (var19 - var21) * (0.6666666666666666f - var23) * 6.0f;
} else {
var13 = var21;
}
if(6.0f * var6 < 1.0f) {
var15 = var21 + (var19 - var21) * 6.0f * var6;
} else if(2.0f * var6 < 1.0f) {
var15 = var19;
} else if(3.0f * var6 < 2.0f) {
var15 = var21 + (var19 - var21) * (0.6666666666666666f - var6) * 6.0f;
} else {
var15 = var21;
}
if(6.0f * var27 < 1.0f) {
var17 = var21 + (var19 - var21) * 6.0f * var27;
} else if(2.0f * var27 < 1.0f) {
var17 = var19;
} else if(3.0f * var27 < 2.0f) {
var17 = var21 + (var19 - var21) * (0.6666666666666666f - var27) * 6.0f;
} else {
var17 = var21;
}
}
vec3 rgb = vec3(
pow(var13, brightness),
pow(var15, brightness),
pow(var17, brightness)
);
// I don't think we actually need this
if (rgb == vec3(0, 0, 0)) {
rgb = vec3(0, 0, 1/255.f);
}
return rgb;
}
void main()
{
ivec3 vertex = VertexPosition.xyz;
int ahsl = VertexPosition.w;
int hsl = ahsl & 0xffff;
float a = float(ahsl >> 24 & 0xff) / 255.f;
vec3 rgb = hslToRgb(hsl);
vPosition = vertex;
vColor = vec4(rgb, 1.f - a);
vUv = uv;
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright (c) 2018, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#version 330
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
TexCoord = aTexCoord;
}