Skip to content

Commit

Permalink
Generate mipmaps more efficiently
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Nov 5, 2024
1 parent fb985e1 commit 73bf061
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
15 changes: 5 additions & 10 deletions 13_generate_mipmaps/Shaders/shaders.kong
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,17 @@ const mip_constants: {
#[set(mip)]
const mip_source_texture: tex2d;

#[set(mip)]
const mip_source_sampler: sampler;

#[set(mip), write]
const mip_destination_texture: tex2d;

#[compute, threads(8, 8, 1)]
fun comp(): void {
var index: uint2 = dispatch_thread_id().xy;
if (index.x < mip_constants.size.x && index.y < mip_constants.size.y) {
var source_index0: uint2 = index * 2;
var source_index1: uint2 = uint2(source_index0.x + 1, source_index0.y);
var source_index2: uint2 = uint2(source_index0.x, source_index0.y + 1);
var source_index3: uint2 = uint2(source_index0.x + 1, source_index0.y + 1);

mip_destination_texture[index] =
(mip_source_texture[source_index0]
+ mip_source_texture[source_index1]
+ mip_source_texture[source_index2]
+ mip_source_texture[source_index0]) / 4.0;
var color: float4 = sample_lod(mip_source_texture, mip_source_sampler, float2(index.x / float(mip_constants.size.x), index.y / float(mip_constants.size.x)), 0);
mip_destination_texture[index] = color;
}
}
18 changes: 18 additions & 0 deletions 13_generate_mipmaps/Sources/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static fs_vertex_in_buffer vertices_fs;
static kope_g5_buffer indices;
static kope_g5_texture render_target;
static kope_g5_sampler sampler;
static kope_g5_sampler mip_sampler;
static fs_set set;
static mip_set mip_sets[4];

Expand Down Expand Up @@ -195,18 +196,35 @@ int kickstart(int argc, char **argv) {
fsparams.fs_sampler = &sampler;
kong_create_fs_set(&device, &fsparams, &set);

kope_g5_sampler_parameters mip_sampler_params = {0};
mip_sampler_params.address_mode_u = KOPE_G5_ADDRESS_MODE_REPEAT;
mip_sampler_params.address_mode_v = KOPE_G5_ADDRESS_MODE_REPEAT;
mip_sampler_params.address_mode_w = KOPE_G5_ADDRESS_MODE_REPEAT;
mip_sampler_params.mag_filter = KOPE_G5_FILTER_MODE_LINEAR;
mip_sampler_params.min_filter = KOPE_G5_FILTER_MODE_LINEAR;
mip_sampler_params.mipmap_filter = KOPE_G5_MIPMAP_FILTER_MODE_NEAREST;
mip_sampler_params.lod_min_clamp = 0;
mip_sampler_params.lod_max_clamp = 32;
mip_sampler_params.compare = KOPE_G5_COMPARE_FUNCTION_ALWAYS;
mip_sampler_params.max_anisotropy = 1;
kope_g5_device_create_sampler(&device, &mip_sampler_params, &mip_sampler);

for (int i = 0; i < 4; ++i) {
mip_parameters cparams = {0};
cparams.mip_source_texture.texture = &render_target;
cparams.mip_source_texture.base_mip_level = i;
cparams.mip_source_texture.mip_level_count = 1;
cparams.mip_source_texture.base_array_layer = 0;
cparams.mip_source_texture.array_layer_count = 1;

cparams.mip_source_sampler = &mip_sampler;

cparams.mip_destination_texture.texture = &render_target;
cparams.mip_destination_texture.base_mip_level = i + 1;
cparams.mip_destination_texture.mip_level_count = 1;
cparams.mip_destination_texture.base_array_layer = 0;
cparams.mip_destination_texture.array_layer_count = 1;

kong_create_mip_set(&device, &cparams, &mip_sets[i]);
}

Expand Down
Binary file modified 13_generate_mipmaps/reference.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 73bf061

Please sign in to comment.