ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | LICENSE

Commit: 007bc1f0a5f2677504c14d4e651eb1f1cb967b94
Parent: abd9e92b9fc0964561f1dffc30964431c29bbe0c
Author: Randy Palamar
Date:   Wed, 26 Jun 2024 14:05:04 -0600

simplify fragment shader mip map view

Diffstat:
Mbeamformer.c | 31+++++++++++++------------------
Mmain.c | 2--
Mshaders/render.glsl | 13++++++-------
Mutil.h | 1-
4 files changed, 19 insertions(+), 28 deletions(-)

diff --git a/beamformer.c b/beamformer.c @@ -100,24 +100,24 @@ do_beamformer(BeamformerCtx *ctx, Arena arena, s8 rf_data) u32 fontsize = 32; u32 fontspace = 1; - static v2 fs[2]; - if (fs[0].x == 0) { - fs[0] = (v2){ .rl = MeasureTextEx(ctx->font, txt[0], fontsize, fontspace) }; - fs[1] = (v2){ .rl = MeasureTextEx(ctx->font, txt[1], fontsize, fontspace) }; + static v2 fts[2]; + if (fts[0].x == 0) { + fts[0] = (v2){ .rl = MeasureTextEx(ctx->font, txt[0], fontsize, fontspace) }; + fts[1] = (v2){ .rl = MeasureTextEx(ctx->font, txt[1], fontsize, fontspace) }; } pos.x += 130 * dt * scale.x; pos.y += 120 * dt * scale.y; - if (pos.x > (ws.w - fs[txt_idx].x) || pos.x < 0) { + if (pos.x > (ws.w - fts[txt_idx].x) || pos.x < 0) { txt_idx = !txt_idx; - CLAMP(pos.x, 0, ws.w - fs[txt_idx].x); + CLAMP(pos.x, 0, ws.w - fts[txt_idx].x); scale.x *= -1.0; } - if (pos.y > (ws.h - fs[txt_idx].y) || pos.y < 0) { + if (pos.y > (ws.h - fts[txt_idx].y) || pos.y < 0) { txt_idx = !txt_idx; - CLAMP(pos.y, 0, ws.h - fs[txt_idx].y); + CLAMP(pos.y, 0, ws.h - fts[txt_idx].y); scale.y *= -1.0; } @@ -144,16 +144,11 @@ do_beamformer(BeamformerCtx *ctx, Arena arena, s8 rf_data) BeginTextureMode(ctx->fsctx.output); ClearBackground(ctx->bg); BeginShaderMode(ctx->fsctx.shader); - glUseProgram(ctx->fsctx.shader.id); - u32 otu = ctx->out_texture_unit; - glBindImageTexture(otu + 1, ctx->out_texture, ctx->out_texture_mips - 1, - GL_FALSE, 0, GL_READ_ONLY, GL_RG32F); - glBindImageTexture(otu, ctx->out_texture, 0, - GL_FALSE, 0, GL_READ_ONLY, GL_RG32F); - glUniform1i(ctx->fsctx.out_data_tex_id, otu); - glUniform1i(ctx->fsctx.mip_view_tex_id, otu + 1); - glUniform1f(ctx->fsctx.db_cutoff_id, ctx->fsctx.db); - DrawTexture(ctx->fsctx.output.texture, 0, 0, WHITE); + FragmentShaderCtx *fs = &ctx->fsctx; + glUseProgram(fs->shader.id); + glUniform1i(fs->out_data_tex_id, ctx->out_texture_unit); + glUniform1f(fs->db_cutoff_id, fs->db); + DrawTexture(fs->output.texture, 0, 0, WHITE); EndShaderMode(); EndTextureMode(); diff --git a/main.c b/main.c @@ -164,7 +164,6 @@ init_fragment_shader_ctx(FragmentShaderCtx *ctx, uv3 out_data_dim) ctx->shader = LoadShader(NULL, "shaders/render.glsl"); ctx->output = LoadRenderTexture(out_data_dim.w, out_data_dim.h); ctx->out_data_tex_id = glGetUniformLocation(ctx->shader.id, "u_out_data_tex"); - ctx->mip_view_tex_id = glGetUniformLocation(ctx->shader.id, "u_mip_view_tex"); ctx->db_cutoff_id = glGetUniformLocation(ctx->shader.id, "u_db_cutoff"); ctx->db = -50.0f; } @@ -197,7 +196,6 @@ reload_shaders(BeamformerCtx *ctx, Arena a) UnloadShader(ctx->fsctx.shader); ctx->fsctx.shader = updated_fs; ctx->fsctx.out_data_tex_id = GetShaderLocation(updated_fs, "u_out_data_tex"); - ctx->fsctx.mip_view_tex_id = GetShaderLocation(updated_fs, "u_mip_view_tex"); ctx->fsctx.db_cutoff_id = GetShaderLocation(updated_fs, "u_db_cutoff"); } } diff --git a/shaders/render.glsl b/shaders/render.glsl @@ -1,12 +1,11 @@ /* See LICENSE for license details. */ -#version 430 +#version 430 core in vec2 fragTexCoord; out vec4 v_out_colour; -layout(rg32f, location = 1) uniform image3D u_out_data_tex; -layout(rg32f, location = 2) uniform image3D u_mip_view_tex; -layout(location = 3) uniform float u_db_cutoff = -60; +layout(location = 1) uniform sampler3D u_out_data_tex; +layout(location = 2) uniform float u_db_cutoff = -60; /* input: h [0,360] | s,v [0, 1] * * output: rgb [0,1] */ @@ -19,11 +18,11 @@ vec3 hsv2rgb(vec3 hsv) void main() { - ivec3 out_data_dim = imageSize(u_out_data_tex); + ivec3 out_data_dim = textureSize(u_out_data_tex, 0); ivec2 coord = ivec2(fragTexCoord * out_data_dim.xy); - vec2 min_max = imageLoad(u_mip_view_tex, ivec3(0, 0, 0)).xy; + vec2 min_max = texelFetch(u_out_data_tex, ivec3(0), textureQueryLevels(u_out_data_tex) - 1).xy; - float smp = imageLoad(u_out_data_tex, ivec3(coord.x, coord.y, 0)).x; + float smp = texelFetch(u_out_data_tex, ivec3(coord.x, coord.y, 0), 0).x; float absmax = max(abs(min_max.y), abs(min_max.x)); smp = 20 * log(abs(smp) / absmax); diff --git a/util.h b/util.h @@ -91,7 +91,6 @@ typedef struct { Shader shader; RenderTexture2D output; i32 out_data_tex_id; - i32 mip_view_tex_id; i32 db_cutoff_id; f32 db; } FragmentShaderCtx;