Files
fn_registry/cpp/functions/gfx/gpu_ssbo.cpp
T
egutierrez 07d06d5e7d feat(cpp/gfx): GPU compute primitives for Monte Carlo (G1-G7)
Stack base de compute shaders OpenGL 4.3 para cargas Monte Carlo intensivas
en GPU. Reutiliza el patron de graph_force_layout_gpu (SSBO + compute) y se
integra con el resto del registry sin nuevos simbolos en gl_loader (todo lo
que se necesita ya estaba expuesto).

- gpu_ssbo: lifecycle de Shader Storage Buffer Objects.
- gpu_compute_program: compila compute GLSL 4.3 con preamble inyectable
  (mismo pattern de gl_shader::compile_fragment).
- gpu_dispatch: dispatch_1d/2d/3d con ceil(N/local) automatico + barrier
  helpers (storage, uniform, image, buffer_update, all).
- gpu_rng_glsl: PCG32 GLSL (uniform/normal/below) + SplitMix64 seed walkers
  para sembrar deterministicamente N walkers desde un master seed.
- gpu_histogram_1d: SSBO float[N] -> uint[nbins] via atomicAdd.
- gpu_histogram_2d: SSBO float[2N] xy-interleaved -> uint[nx*ny] +
  to_density helper para alimentar heatmap_cpp_viz.
- gpu_reduce: workgroup-shared sum/min/max/mean (local 256, partials CPU).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-04 11:52:08 +02:00

56 lines
1.6 KiB
C++

#include "gfx/gpu_ssbo.h"
namespace fn::gfx {
Ssbo ssbo_create(std::size_t bytes, const void* initial_data, GLenum usage) {
Ssbo s{};
if (bytes == 0) return s;
GLuint id = 0;
glGenBuffers(1, &id);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, id);
glBufferData(GL_SHADER_STORAGE_BUFFER,
static_cast<GLsizeiptr>(bytes),
initial_data, usage);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
s.id = id;
s.bytes = bytes;
return s;
}
void ssbo_bind(const Ssbo& s, unsigned int binding) {
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, s.id);
}
void ssbo_upload(const Ssbo& s, std::size_t offset,
std::size_t bytes, const void* data) {
if (bytes == 0 || s.id == 0) return;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, s.id);
glBufferSubData(GL_SHADER_STORAGE_BUFFER,
static_cast<GLintptr>(offset),
static_cast<GLsizeiptr>(bytes),
data);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
void ssbo_readback(const Ssbo& s, std::size_t offset,
std::size_t bytes, void* out) {
if (bytes == 0 || s.id == 0) return;
glBindBuffer(GL_SHADER_STORAGE_BUFFER, s.id);
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER,
static_cast<GLintptr>(offset),
static_cast<GLsizeiptr>(bytes),
out);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}
void ssbo_destroy(Ssbo& s) {
if (s.id != 0) {
GLuint id = s.id;
glDeleteBuffers(1, &id);
}
s.id = 0;
s.bytes = 0;
}
} // namespace fn::gfx