Files
egutierrez c74fd4ae0d 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

3.4 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, tested, tests, test_file_path, file_path, framework, params, output
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports tested tests test_file_path file_path framework params output
gpu_ssbo function cpp gfx 1.0.0 impure Ssbo ssbo_create(size_t bytes, const void* initial_data, GLenum usage); void ssbo_bind(const Ssbo&, unsigned binding); void ssbo_upload(const Ssbo&, size_t offset, size_t bytes, const void* data); void ssbo_readback(const Ssbo&, size_t offset, size_t bytes, void* out); void ssbo_destroy(Ssbo&) Lifecycle de Shader Storage Buffer Objects (SSBO) para datos arbitrarios CPU<->GPU. create/bind/upload/readback/destroy. Pareja generica de mesh_gpu para computes y Monte Carlo intensivo.
opengl
ssbo
gpu
compute
buffer
gfx
gl_loader_cpp_gfx
false error_go_core
GL/gl.h
GL/glext.h
false
cpp/functions/gfx/gpu_ssbo.cpp opengl
name desc
bytes Tamano del buffer en bytes. Si 0, ssbo_create devuelve un Ssbo vacio sin tocar GL.
name desc
initial_data Puntero a datos iniciales (nullptr = sin inicializar; el shader es responsable del primer write).
name desc
usage GLenum hint: GL_DYNAMIC_DRAW (default, CPU escribe a menudo), GL_STATIC_DRAW, GL_DYNAMIC_COPY (compute<->compute).
name desc
binding Indice del layout(std430, binding=N) que matchea el shader.
name desc
offset Offset en bytes dentro del buffer.
name desc
data Puntero a bytes a subir (upload) o destino donde escribir bytes leidos (readback).
Ssbo con id GL valido (o 0 si bytes=0). El caller mantiene la struct y la pasa a las demas funciones; ssbo_destroy resetea id/bytes a 0.

gpu_ssbo

Wrapper canonico de Shader Storage Buffer Objects para uso con compute shaders. Pensado como base de Monte Carlo / MCMC en GPU: el SSBO es el medio para llevar samples, walkers, contadores e histogramas entre passes y de vuelta a CPU para visualizacion con histogram_cpp_viz, heatmap_cpp_viz, etc.

Ciclo de vida

fn::gfx::Ssbo samples = fn::gfx::ssbo_create(N * sizeof(float));

// Bind antes de cada dispatch que quiera leer/escribir el buffer:
fn::gfx::ssbo_bind(samples, /*binding=*/0);
glDispatchCompute( ... );
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);

// Lectura sincrona a CPU:
std::vector<float> host(N);
fn::gfx::ssbo_readback(samples, 0, N * sizeof(float), host.data());

fn::gfx::ssbo_destroy(samples);

Subir datos iniciales

std::vector<unsigned int> seeds(N);
fn::gfx::seed_walkers_init(0xDEADBEEF, seeds.data(), N);

fn::gfx::Ssbo seed_buf = fn::gfx::ssbo_create(
    N * sizeof(unsigned int),
    seeds.data(),
    GL_STATIC_DRAW
);

O bien crear vacio y rellenar luego:

fn::gfx::Ssbo buf = fn::gfx::ssbo_create(N * sizeof(unsigned int));
fn::gfx::ssbo_upload(buf, 0, N * sizeof(unsigned int), seeds.data());

Notas

  • ssbo_readback bloquea el pipeline. Para readbacks frecuentes considerar PBO o glFenceSync (no implementado aqui — añadirlo cuando haga falta).
  • El caller es responsable de los glMemoryBarrier entre dispatches que leen lo que un dispatch anterior escribio.
  • El buffer se enlaza a GL_SHADER_STORAGE_BUFFER para create/upload/readback y al binding indexado via glBindBufferBase solo en ssbo_bind. Esto evita estado pegajoso entre llamadas.
  • Tamano maximo en GL 4.3: GL_MAX_SHADER_STORAGE_BLOCK_SIZE (en RTX 3070 ~ 2 GB). Para Monte Carlo de 10^7 samples float fp32 son 40 MB — sobrado.