Skip to content

Commit

Permalink
Try to make the ComputeShader sample work with a buffer-image
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Feb 21, 2025
1 parent 64eaf3f commit a00cb77
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
32 changes: 18 additions & 14 deletions ComputeShader/Shaders/shaders.kong
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ const constants: {
mvp: float3x3;
};

//#[set(everything)]
//const comp_texture: tex2d;

//#[set(everything)]
//const comp_sampler: sampler;

#[set(everything), set(compute)]
const comp_texture: float4[];

fun pix(input: vertex_out): float4 {
//var color: float4 = sample(comp_texture, comp_sampler, input.tex);
var color: float4 = comp_texture[input.tex.x * 256 + input.tex.y * 256 * 256];
return float4(color.r, color.g, color.b, 1.0);
}

fun pos(input: vertex_in): vertex_out {
var output: vertex_out;

Expand All @@ -27,17 +42,6 @@ fun pos(input: vertex_in): vertex_out {
return output;
}

#[set(everything)]
const comp_texture: tex2d;

#[set(everything)]
const comp_sampler: sampler;

fun pix(input: vertex_out): float4 {
var color: float4 = sample(comp_texture, comp_sampler, input.tex);
return float4(color.r, color.g, color.b, 1.0);
}

#[pipe]
struct pipeline {
vertex = pos;
Expand All @@ -49,13 +53,13 @@ const compute_constants: {
roll: float;
};

#[set(compute), write]
const dest_texture: tex2d;
//#[set(compute), write]
//const dest_texture: tex2d;

#[compute, threads(16, 16, 1)]
fun comp(): void {
var store_pos: uint2 = dispatch_thread_id().xy;
var local_coef: float = length(float2(int2(group_thread_id().xy) - 8) / 8.0);
var global_coef: float = sin(float(group_id().x + group_id().y) * 0.1 + compute_constants.roll) * 0.5;
dest_texture[store_pos] = float4(1.0 - global_coef * local_coef, 0.0, 0.0, 1.0);
comp_texture[store_pos.x * store_pos.y * 256] = float4(1.0 - global_coef * local_coef, 0.0, 0.0, 1.0);
}
22 changes: 22 additions & 0 deletions ComputeShader/Sources/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
#include "../../screenshot.h"
#endif

#define USE_BUFFER_IMAGE

static kope_g5_device device;
static kope_g5_command_list list;
static vertex_in_buffer vertices;
static kope_g5_buffer indices;
static kope_g5_buffer constants;
static kope_g5_buffer compute_constants;
#ifdef USE_BUFFER_IMAGE
static kope_g5_buffer buffer_texture;
#else
static kope_g5_texture texture;
static kope_g5_sampler sampler;
#endif
static everything_set everything;
static compute_set compute;
static kope_g5_buffer image_buffer;
Expand Down Expand Up @@ -87,6 +93,13 @@ int kickstart(int argc, char **argv) {

kong_init(&device);

#ifdef USE_BUFFER_IMAGE
const kope_g5_buffer_parameters buffer_texture_parameters = {
.size = 256 * 256 * 4,
.usage_flags = KOPE_G5_BUFFER_USAGE_READ_WRITE,
};
kope_g5_device_create_buffer(&device, &buffer_texture_parameters, &buffer_texture);
#else
kope_g5_texture_parameters texture_parameters;
texture_parameters.width = 256;
texture_parameters.height = 256;
Expand All @@ -110,6 +123,7 @@ int kickstart(int argc, char **argv) {
sampler_parameters.compare = KOPE_G5_COMPARE_FUNCTION_ALWAYS;
sampler_parameters.max_anisotropy = 1;
kope_g5_device_create_sampler(&device, &sampler_parameters, &sampler);
#endif

kope_g5_device_create_command_list(&device, KOPE_G5_COMMAND_LIST_TYPE_GRAPHICS, &list);

Expand Down Expand Up @@ -151,11 +165,15 @@ int kickstart(int argc, char **argv) {
{
everything_parameters parameters = {0};
parameters.constants = &constants;
#ifdef USE_BUFFER_IMAGE
parameters.comp_texture = &buffer_texture;
#else
parameters.comp_texture.texture = &texture;
parameters.comp_texture.base_mip_level = 0;
parameters.comp_texture.mip_level_count = 1;
parameters.comp_texture.array_layer_count = 1;
parameters.comp_sampler = &sampler;
#endif
kong_create_everything_set(&device, &parameters, &everything);
}

Expand All @@ -164,10 +182,14 @@ int kickstart(int argc, char **argv) {
{
compute_parameters parameters = {0};
parameters.compute_constants = &compute_constants;
#ifdef USE_BUFFER_IMAGE
parameters.comp_texture = &buffer_texture;
#else
parameters.dest_texture.texture = &texture;
parameters.dest_texture.base_mip_level = 0;
parameters.dest_texture.mip_level_count = 1;
parameters.dest_texture.array_layer_count = 1;
#endif
kong_create_compute_set(&device, &parameters, &compute);
}

Expand Down

0 comments on commit a00cb77

Please sign in to comment.