Rework OIT

This commit is contained in:
shylie 2025-09-12 17:37:29 -04:00
parent 4496625807
commit 1dcb779637
2 changed files with 125 additions and 84 deletions

View File

@ -7,7 +7,7 @@
namespace
{
constexpr int SIZE = 256;
constexpr int SIZE = 512;
double octaves(SimplexNoise1234& simplex, double x, double y, int layers, double persistence, double frequency)
{
@ -30,8 +30,7 @@ double octaves(SimplexNoise1234& simplex, double x, double y, int layers, double
uint8_t data[SIZE * SIZE];
double split_point = 0;
double direction = 0.5;
double split_point = 0.5;
double ease_f(double n)
{
@ -66,64 +65,112 @@ uint32_t color(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
return (r << 0) | (g << 8) | (b << 16) | (a << 24);
}
uint32_t color(double r, double g, double b, double a)
{
if (r < 0) { r = 0; }
if (r > 1) { r = 1; }
if (g < 0) { g = 0; }
if (g > 1) { g = 1; }
if (b < 0) { b = 0; }
if (b > 1) { b = 1; }
if (a < 0) { a = 0; }
if (a > 1) { a = 1; }
uint8_t r8 = r * 255.0;
uint8_t g8 = g * 255.0;
uint8_t b8 = b * 255.0;
uint8_t a8 = a * 255.0;
return color(r8, g8, b8, a8);
}
constexpr double ALPHA = 0.7;
const sprstk_palette* water_palette()
{
static sprstk_palette pal;
for (int i = 0; i < 32; i++)
{
pal.colors[i] = color(i / 32.0, i / 16.0, i / 8.0, ALPHA);
}
return &pal;
}
const sprstk_palette* sand_palette()
{
static sprstk_palette pal;
for (int i = 0; i < 32; i++)
{
pal.colors[i] = color(i / 20.0 + 0.3, i / 20.0 + 0.2, i / 20.0 + 0.1, ALPHA);
}
return &pal;
}
const sprstk_palette* land_palette()
{
static sprstk_palette pal;
for (int i = 0; i < 32; i++)
{
if (i <= 19)
{
pal.colors[i] = color(i / 24.0 + 0.1, i / 36.0 + 0.05, i / 48.0, ALPHA);
}
else
{
pal.colors[i] = color(i / 40.0 - 0.1, i / 40.0 + 0.1, 0.1, ALPHA);
}
}
return &pal;
}
const sprstk_palette* stone_palette()
{
static sprstk_palette pal;
for (int i = 0; i < 32; i++)
{
const double offset = (i < 28 ? -0.2 : 0.2);
pal.colors[i] = color(i / 35.2 + offset, i / 32.0 + offset, i / 29.0 + offset, ALPHA);
}
return &pal;
}
const sprstk_palette* shadow_palette()
{
static sprstk_palette pal;
for (int i = 0; i < 32; i++)
{
pal.colors[i] = color(0, 0, 0, 0.3);
}
return &pal;
}
int pick_pal(uint8_t height)
{
if (height > 20) { return 0; }
if (height > 8) { return 1; }
return 2;
if (height < 8) { return 0; } // water
if (height < 13) { return 1; } // sand
if (height < 24) { return 2; } // land
return 3;
}
void init(sprstk* instance, void* userdata)
{
sprstk_palette pal = {};
sprstk_set_palette(instance, 0, water_palette());
sprstk_set_palette(instance, 1, sand_palette());
sprstk_set_palette(instance, 2, land_palette());
sprstk_set_palette(instance, 3, stone_palette());
sprstk_set_palette(instance, 4, shadow_palette());
const uint8_t ALPHA = 0xAA;
for (int i = 0; i < 28; i++)
{
uint8_t val = 0x55 / (16.2f - i / 2.0f) + 0x11;
pal.colors[i] = color(val, val, val, ALPHA);
}
for (int i = 28; i < 32; i++)
{
uint8_t val = 0x50 / (32.0f - i) + 0xA0;
pal.colors[i] = color(val, val, val, ALPHA);
}
sprstk_set_palette(instance, 0, &pal);
for (int i = 0; i < 16; i++)
{
pal.colors[i] = color(0x70 / (5.0f - i / 4.0f), 0x35 / (5.0f - i / 4.0f), 0, ALPHA);
}
for (int i = 16; i < 32; i++)
{
pal.colors[i] = color(0x05 / (8.5f - i / 2.5f) + 0x15, 0x40 / (8.5f - i / 2.5f) + 0x0, 0, ALPHA);
}
sprstk_set_palette(instance, 1, &pal);
for (int i = 0; i < 8; i++)
{
pal.colors[i] = color(0x20 / (8 - i) + 0x10, 0x40 / (8 - i) + 0x20, 0xB0 / (8 - i) + 0x40, ALPHA);
}
sprstk_set_palette(instance, 2, &pal);
sprstk_set_scale(instance, 1.0f);
}
void update(sprstk* instance, float dt, float* userdata)
{
*userdata += dt / 2;
sprstk_set_angle(instance, *userdata);
double direction_modifier = 1 - 1.9 * fabs(0.5 - split_point);
split_point += direction_modifier * direction * dt;
if (split_point < 0) { direction *= -1; split_point = 0; }
if (split_point > 1) { direction *= -1; split_point = 1; }
sprstk_set_scale(instance, 0.5);
SimplexNoise1234 simplex;
for (int i = 0; i < SIZE; i++)
@ -143,13 +190,25 @@ void update(sprstk* instance, float dt, float* userdata)
sprstk_tile_info info = {};
info.position = { i - SIZE / 2, j - SIZE / 2 };
info.layers = data[i + SIZE * j];
info.layer_modifiers = { 0, 24, 4 / 255.0f };
info.layer_modifiers = { 0, 15 };
info.palette_index = pick_pal(data[i + SIZE * j]);
info.layer_order_offset = 1;
sprstk_put(instance, info);
info.palette_index = 4;
info.layer_modifiers = { 0.5, 15.5 };
info.layer_order_offset = 0;
sprstk_put(instance, info);
}
}
}
void update(sprstk* instance, float dt, float* userdata)
{
*userdata += dt / 2;
sprstk_set_angle(instance, *userdata);
}
}
int main()

View File

@ -10,7 +10,7 @@
#include <cmath>
#define OIT_LAYERS 32
#define OIT_LAYERS 64
#define _STRINGIFY(x) #x
#define STRINGIFY(x) _STRINGIFY(x)
@ -123,17 +123,18 @@ void main()
vec2 offset_per_layer = vec2(x_offset_per_layer, y_offset_per_layer);
float scale_per_layer = bitfieldExtract(t_info.packed_data[1], 16, 8) / 255.0f;
float total_scale = scale + scale_per_layer * gl_LocalInvocationID.x;
float layer_scale = 1 + scale_per_layer * gl_LocalInvocationID.x;
for (uint i = 4 * gl_LocalInvocationID.x; i < 4 * gl_LocalInvocationID.x + 4; i++)
{
vec4 position = vec4(rotation_matrix * positions[i % 4], float(4 * gl_LocalInvocationID.x + layer_order_offset) / 128, 1);
vec4 position = vec4(rotation_matrix * positions[i % 4], float(2 * gl_LocalInvocationID.x + layer_order_offset) / 128, 1);
position.xy /= screen_size_and_pixel_scale.xy;
position.xy *= total_scale;
position.xy *= scale;
position.xy += offset_per_layer * vec2(gl_LocalInvocationID.x * scale / screen_size_and_pixel_scale.xy);
position.xy *= layer_scale;
gl_MeshVerticesNV[i].gl_Position = position;
v_out[i].layer = 4 * gl_LocalInvocationID.x + layer_order_offset;
v_out[i].layer = 2 * gl_LocalInvocationID.x + layer_order_offset;
v_out[i].color = unpackUnorm4x8(c);
v_out[i].texcoord = texcoords[i % 4];
}
@ -169,12 +170,9 @@ void main()
const vec4 texture_color = texture(TextureAtlas, fragIn.texcoord / vec2(textureSize(TextureAtlas, 0)));
const uint color = packUnorm4x8(fragIn.color * texture_color);
const uint position = imageAtomicAdd(AZNextBuffer, ivec2(gl_FragCoord.xy), 1);
imageAtomicMax(AZNextBuffer, ivec2(gl_FragCoord.xy), fragIn.layer + 1);
if (position < )" STRINGIFY(OIT_LAYERS) R"()
{
imageStore(AZBuffer, ivec3(gl_FragCoord.xy, position), uvec4(fragIn.layer, color, 0, 0));
}
imageStore(AZBuffer, ivec3(gl_FragCoord.xy, fragIn.layer), uvec4(fragIn.layer, color, 0, 0));
FragColor = vec4(0);
}
@ -201,29 +199,13 @@ layout (binding = 1, r32ui) uniform restrict readonly uimage2D AZNextBuffer;
void main()
{
uvec2 data[ )" STRINGIFY(OIT_LAYERS) R"(];
const uint layer_count = imageLoad(AZNextBuffer, ivec2(gl_FragCoord.xy)).x;
for (uint i = 0; i < layer_count; i++)
{
data[i] = imageLoad(AZBuffer, ivec3(gl_FragCoord.xy, i)).xy;
}
for (int i = 0; i < layer_count; i++)
{
for (int j = i; j > 0 && data[j - 1].x > data[j].x; j--)
{
const uvec2 temp = data[j];
data[j] = data[j - 1];
data[j - 1] = temp;
}
}
vec3 color = vec3(0);
for (int i = 0; i < layer_count; i++)
{
vec4 temp_color = unpackUnorm4x8(data[i].y);
uvec4 data = imageLoad(AZBuffer, ivec3(gl_FragCoord.xy, i));
vec4 temp_color = unpackUnorm4x8(data.y);
color = vec3(temp_color.rgb * temp_color.a + color.rgb * (1 - temp_color.a));
}
@ -487,7 +469,7 @@ private:
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
sdl.window = SDL_CreateWindow("sprstk", 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
sdl.window = SDL_CreateWindow("sprstk", 1280, 960, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!sdl.window)
{
throw application_error("Failed to create window");