Skip to content

Commit

Permalink
gpu: remove radius from compute shaders
Browse files Browse the repository at this point in the history
Since we are not using the distances to index arrays, we can just store
negative distances, making the radius unnecessary.
  • Loading branch information
Adam- committed Dec 26, 2023
1 parent 14bc97d commit c28cdb4
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ else if (offsetModel.getSceneId() == sceneId)
buffer.put(uvOffset);
buffer.put(tc);
buffer.put(targetBufferOffset);
buffer.put(FLAG_SCENE_BUFFER | (hillskew ? (1 << 26) : 0) | (plane << 24) | (model.getRadius() << 12) | orientation);
buffer.put(FLAG_SCENE_BUFFER | (hillskew ? (1 << 26) : 0) | (plane << 24) | orientation);
buffer.put(x + client.getCameraX2()).put(y + client.getCameraY2()).put(z + client.getCameraZ2());

targetBufferOffset += tc * 3;
Expand Down Expand Up @@ -1812,7 +1812,7 @@ else if (offsetModel.getSceneId() == sceneId)
buffer.put(hasUv ? tempUvOffset : -1);
buffer.put(len / 3);
buffer.put(targetBufferOffset);
buffer.put((model.getRadius() << 12) | orientation);
buffer.put(orientation);
buffer.put(x + client.getCameraX2()).put(y + client.getCameraY2()).put(z + client.getCameraZ2());

tempOffset += len;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ struct shared_data {
int totalDistance[12]; // sum of distances to faces of a given priority
int totalMappedNum[18]; // number of faces with a given adjusted priority
int min10; // minimum distance to a face of priority 10
uint renderPris[0]; // packed distance and face id
int renderPris[0]; // priority for face draw order
};

struct modelinfo {
int offset; // offset into vertex buffer
int toffset; // offset into texture buffer
int size; // length in faces
int idx; // write idx in target buffer
int flags; // buffer, hillskew, plane, radius, orientation
int flags; // buffer, hillskew, plane, orientation
int x; // scene position x
int y; // scene position y
int z; // scene position z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ 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 uint renderPris[THREAD_COUNT * FACES_PER_THREAD]; // packed distance and face id
shared int min10; // minimum distance to a face of priority 10
shared int renderPris[THREAD_COUNT * FACES_PER_THREAD]; // priority for face draw order

#include "comp_common.glsl"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ struct modelinfo {
int toffset; // offset into texture buffer
int size; // length in faces
int idx; // write idx in target buffer
int flags; // buffer, hillskew, plane, radius, orientation
int flags; // buffer, hillskew, plane, orientation
int x; // scene position x
int y; // scene position y
int z; // scene position z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ void get_face(__local struct shared_data *shared, __constant struct uniform *uni
}

if (localId < size) {
int radius = (flags >> 12) & 0xfff;
int orientation = flags & 0x7ff;

// rotate for model orientation
Expand All @@ -129,12 +128,7 @@ void get_face(__local struct shared_data *shared, __constant struct uniform *uni

// calculate distance to face
int thisPriority = (thisA.w >> 16) & 0xff; // all vertices on the face have the same priority
int thisDistance;
if (radius == 0) {
thisDistance = 0;
} else {
thisDistance = face_distance(thisrvA, thisrvB, thisrvC, cameraYaw, cameraPitch) + radius;
}
int thisDistance = face_distance(thisrvA, thisrvB, thisrvC, cameraYaw, cameraPitch);

*o1 = thisrvA;
*o2 = thisrvB;
Expand Down Expand Up @@ -206,15 +200,15 @@ void insert_face(__local struct shared_data *shared, uint localId, struct modeli
if (localId < size) {
// calculate base offset into renderPris based on number of faces with a lower priority
int baseOff = count_prio_offset(shared, adjPrio);
// the furthest faces draw first, and have the highest value
// the furthest faces draw first, and have the highest priority.
// if two faces have the same distance, the one with the
// lower id draws first
shared->renderPris[baseOff + prioIdx] = ((uint)(distance << 16)) | (~localId & 0xffffu);
// lower id draws first.
shared->renderPris[baseOff + prioIdx] = distance << 16 | (int)(~localId & 0xffffu);
}
}

int tile_height(read_only image3d_t tileHeightImage, int z, int x, int y) {
#define ESCENE_OFFSET 40 // (184-104)/2
#define ESCENE_OFFSET 40 // (184-104)/2
const sampler_t tileHeightSampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE;
int4 coord = (int4)(x + ESCENE_OFFSET, y + ESCENE_OFFSET, z, 0);
return read_imagei(tileHeightImage, tileHeightSampler, coord).x << 3;
Expand Down Expand Up @@ -250,7 +244,7 @@ void sort_and_insert(__local struct shared_data *shared, __constant struct unifo
const int numOfPriority = shared->totalMappedNum[thisPriority];
const int start = priorityOffset; // index of first face with this priority
const int end = priorityOffset + numOfPriority; // index of last face with this priority
const uint renderPriority = ((uint)(thisDistance << 16)) | (~localId & 0xffffu);
const int renderPriority = thisDistance << 16 | (int)(~localId & 0xffffu);
int myOffset = priorityOffset;

// calculate position this face will be in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ void get_face(uint localId, modelinfo minfo, float cameraYaw, float cameraPitch,
}

if (localId < size) {
int radius = (flags >> 12) & 0xfff;
int orientation = flags & 0x7ff;

// rotate for model orientation
Expand All @@ -129,12 +128,7 @@ void get_face(uint localId, modelinfo minfo, float cameraYaw, float cameraPitch,

// calculate distance to face
int thisPriority = (thisA.w >> 16) & 0xff; // all vertices on the face have the same priority
int thisDistance;
if (radius == 0) {
thisDistance = 0;
} else {
thisDistance = face_distance(thisrvA, thisrvB, thisrvC, cameraYaw, cameraPitch) + radius;
}
int thisDistance = face_distance(thisrvA, thisrvB, thisrvC, cameraYaw, cameraPitch);

o1 = thisrvA;
o2 = thisrvB;
Expand Down Expand Up @@ -205,15 +199,15 @@ void insert_face(uint localId, modelinfo minfo, int adjPrio, int distance, int p
if (localId < size) {
// calculate base offset into renderPris based on number of faces with a lower priority
int baseOff = count_prio_offset(adjPrio);
// the furthest faces draw first, and have the highest value
// the furthest faces draw first, and have the highest priority.
// if two faces have the same distance, the one with the
// lower id draws first
renderPris[baseOff + prioIdx] = uint(distance << 16) | (~localId & 0xffffu);
// lower id draws first.
renderPris[baseOff + prioIdx] = distance << 16 | int(~localId & 0xffffu);
}
}

int tile_height(int z, int x, int y) {
#define ESCENE_OFFSET 40 // (184-104)/2
#define ESCENE_OFFSET 40 // (184-104)/2
return texelFetch(tileHeightSampler, ivec3(x + ESCENE_OFFSET, y + ESCENE_OFFSET, z), 0).r << 3;
}

Expand Down Expand Up @@ -245,7 +239,7 @@ void sort_and_insert(uint localId, modelinfo minfo, int thisPriority, int thisDi
const int numOfPriority = totalMappedNum[thisPriority];
const int start = priorityOffset; // index of first face with this priority
const int end = priorityOffset + numOfPriority; // index of last face with this priority
const uint renderPriority = uint(thisDistance << 16) | (~localId & 0xffffu);
const int renderPriority = thisDistance << 16 | int(~localId & 0xffffu);
int myOffset = priorityOffset;

// calculate position this face will be in
Expand Down

0 comments on commit c28cdb4

Please sign in to comment.