babies first gpu code

This commit is contained in:
Andrew Cohn 2025-11-29 12:29:08 -08:00
parent 189f87a872
commit 8885e6a7c3
9 changed files with 265 additions and 117 deletions

View File

@ -1,9 +1,8 @@
# Makefile
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra -O2 $(shell pkg-config --cflags glfw3) CFLAGS = -Wall -Wextra -O2 $(shell pkg-config --cflags glfw3)
LDFLAGS = $(shell pkg-config --libs glfw3) -ldl LDFLAGS = $(shell pkg-config --libs glfw3) -ldl
SRC = main.c glad.c SRC = glad.c gl_utils.c main.c
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)
BIN = fallingCand BIN = fallingCand

Binary file not shown.

152
gl_utils.c Normal file
View File

@ -0,0 +1,152 @@
// gl_utils.c
#include "gl_utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
void glfw_error_callback(int error, const char* description) {
fprintf(stderr, "GLFW error %d: %s\n", error, description);
}
char* load_text_file(const char* path) {
FILE* f = fopen(path, "rb");
if (!f) {
fprintf(stderr, "Failed to open '%s': %s\n", path, strerror(errno));
return NULL;
}
if (fseek(f, 0, SEEK_END) != 0) {
fprintf(stderr, "fseek failed on '%s'\n", path);
fclose(f);
return NULL;
}
long size = ftell(f);
if (size < 0) {
fprintf(stderr, "ftell failed on '%s'\n", path);
fclose(f);
return NULL;
}
rewind(f);
char* buffer = malloc((size_t)size + 1);
if (!buffer) {
fprintf(stderr, "Out of memory reading '%s'\n", path);
fclose(f);
return NULL;
}
size_t read = fread(buffer, 1, (size_t)size, f);
fclose(f);
if (read != (size_t)size) {
fprintf(stderr, "Short read on '%s'\n", path);
free(buffer);
return NULL;
}
buffer[size] = '\0';
return buffer;
}
GLuint compile_shader(GLenum type, const char* source, const char* debugName) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
GLint success = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char log[1024];
glGetShaderInfoLog(shader, sizeof(log), NULL, log);
fprintf(stderr, "Shader compile failed (%s):\n%s\n", debugName ? debugName : "unknown", log);
glDeleteShader(shader);
exit(EXIT_FAILURE);
}
return shader;
}
GLuint create_program_from_files(const char* vsPath, const char* fsPath) {
char* vsSource = load_text_file(vsPath);
if (!vsSource) {
fprintf(stderr, "Could not load vertex shader from '%s'\n", vsPath);
exit(EXIT_FAILURE);
}
char* fsSource = load_text_file(fsPath);
if (!fsSource) {
fprintf(stderr, "Could not load fragment shader from '%s'\n", fsPath);
free(vsSource);
exit(EXIT_FAILURE);
}
GLuint vs = compile_shader(GL_VERTEX_SHADER, vsSource, vsPath);
GLuint fs = compile_shader(GL_FRAGMENT_SHADER, fsSource, fsPath);
free(vsSource);
free(fsSource);
GLuint prog = glCreateProgram();
glAttachShader(prog, vs);
glAttachShader(prog, fs);
glLinkProgram(prog);
glDeleteShader(vs);
glDeleteShader(fs);
GLint linked = 0;
glGetProgramiv(prog, GL_LINK_STATUS, &linked);
if (!linked) {
char log[1024];
glGetProgramInfoLog(prog, sizeof(log), NULL, log);
fprintf(stderr, "Program link failed:\n%s\n", log);
glDeleteProgram(prog);
exit(EXIT_FAILURE);
}
return prog;
}
GLFWwindow* init_glfw_glad(const char* title, int width, int height) {
glfwSetErrorCallback(glfw_error_callback);
// Prefer Wayland if this GLFW build supports it
if (glfwPlatformSupported(GLFW_PLATFORM_WAYLAND)) {
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_WAYLAND);
fprintf(stderr, "[info] Requesting Wayland platform via glfwInitHint\n");
} else {
fprintf(stderr, "[warn] Wayland platform not supported by this GLFW build, using default\n");
}
if (!glfwInit()) {
fprintf(stderr, "[fatal] glfwInit failed\n");
return NULL;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
GLFWwindow* window = glfwCreateWindow(width, height, title, NULL, NULL);
if (!window) {
fprintf(stderr, "[fatal] glfwCreateWindow failed\n");
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
fprintf(stderr, "[fatal] Failed to initialize GLAD\n");
glfwDestroyWindow(window);
glfwTerminate();
return NULL;
}
return window;
}

27
gl_utils.h Normal file
View File

@ -0,0 +1,27 @@
// gl_utils.h
#ifndef GL_UTILS_H
#define GL_UTILS_H
#include "glad/glad.h"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
// Error callback for GLFW
void glfw_error_callback(int error, const char* description);
// Read an entire text file into a null-terminated buffer.
// Caller must free() the returned pointer.
char* load_text_file(const char* path);
// Compile a single shader from source.
GLuint compile_shader(GLenum type, const char* source, const char* debugName);
// Compile & link a vertex+fragment program from files.
GLuint create_program_from_files(const char* vsPath, const char* fsPath);
// Minimal GLFW+GLAD init: sets error callback, (optionally) hints Wayland,
// calls glfwInit, creates window, makes context current, loads GLAD.
// Returns the created window or NULL on fatal error.
GLFWwindow* init_glfw_glad(const char* title, int width, int height);
#endif // GL_UTILS_H

169
main.c
View File

@ -1,142 +1,81 @@
// main.c // main.c
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h>
#include "glad/glad.h" #include "gl_utils.h"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
static void glfw_error_callback(int error, const char* description) {
fprintf(stderr, "GLFW error %d: %s\n", error, description);
}
static GLuint create_compute_program(const char* source) {
GLint success = 0;
GLchar log[1024];
GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
glShaderSource(shader, 1, &source, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, sizeof(log), NULL, log);
fprintf(stderr, "Compute shader compilation failed:\n%s\n", log);
exit(EXIT_FAILURE);
}
GLuint program = glCreateProgram();
glAttachShader(program, shader);
glLinkProgram(program);
glDeleteShader(shader);
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(program, sizeof(log), NULL, log);
fprintf(stderr, "Program link failed:\n%s\n", log);
exit(EXIT_FAILURE);
}
return program;
}
int main(void) { int main(void) {
// --- Init GLFW & create GL context --- int winW = 800;
glfwSetErrorCallback(glfw_error_callback); int winH = 600;
if (!glfwInit()) { GLFWwindow* window = init_glfw_glad("Falling Sand - Fullscreen Quad", winW, winH);
fprintf(stderr, "Failed to init GLFW\n");
return EXIT_FAILURE;
}
// Request OpenGL 4.3 core (needed for compute shaders)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// We don't actually *need* to show the window
GLFWwindow* window = glfwCreateWindow(640, 480, "GPU Hello", NULL, NULL);
if (!window) { if (!window) {
fprintf(stderr, "Failed to create GLFW window\n");
glfwTerminate();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
glfwMakeContextCurrent(window); printf("Renderer: %s\n", (const char*)glGetString(GL_RENDERER));
printf("OpenGL: %s\n", (const char*)glGetString(GL_VERSION));
printf("GLSL: %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
// --- Load GL symbols via GLAD --- // --- Create fullscreen triangle geometry (VAO + VBO) ---
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { float vertices[] = {
fprintf(stderr, "Failed to initialize GLAD\n"); // x, y
glfwDestroyWindow(window); -1.0f, -1.0f,
glfwTerminate(); 3.0f, -1.0f,
return EXIT_FAILURE; -1.0f, 3.0f
} };
printf("OpenGL version: %s\n", glGetString(GL_VERSION)); GLuint vao = 0, vbo = 0;
printf("GLSL version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
// --- Prepare data buffer on GPU (SSBO) --- glBindVertexArray(vao);
const GLuint N = 16;
GLuint initial[N];
for (GLuint i = 0; i < N; ++i) {
initial[i] = i;
}
GLuint ssbo = 0; glBindBuffer(GL_ARRAY_BUFFER, vbo);
glGenBuffers(1, &ssbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(initial), initial, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssbo); // binding = 0 in shader
// --- Compute shader source: data[i] *= 2 --- // layout(location = 0) in vec2 aPos;
const char* compute_src = glEnableVertexAttribArray(0);
"#version 430 core\n" glVertexAttribPointer(
"layout(local_size_x = 16) in;\n"
"layout(std430, binding = 0) buffer Data {\n"
" uint data[];\n"
"};\n"
"void main() {\n"
" uint idx = gl_GlobalInvocationID.x;\n"
" // N == 16 and local_size_x == 16, so we know idx < 16\n"
" data[idx] *= 2u;\n"
"}\n";
GLuint compute_program = create_compute_program(compute_src);
// --- Run the compute shader ---
glUseProgram(compute_program);
// One workgroup of 16 threads (matches local_size_x)
glDispatchCompute(1, 1, 1);
// Make sure writes to the SSBO are visible to the CPU
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
// --- Read back the result ---
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
GLuint* ptr = (GLuint*)glMapBufferRange(
GL_SHADER_STORAGE_BUFFER,
0, 0,
sizeof(initial), 2,
GL_MAP_READ_BIT GL_FLOAT,
GL_FALSE,
2 * sizeof(float),
(void*)0
); );
if (!ptr) { glBindVertexArray(0);
fprintf(stderr, "Failed to map SSBO\n");
} else { // --- Create shader program ---
printf("GPU computed values:\n"); GLuint program = create_program_from_files(
for (GLuint i = 0; i < N; ++i) { "shaders/fullscreen.vert",
printf(" %2u -> %2u\n", i, ptr[i]); "shaders/gradient.frag"
} );
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
glUseProgram(program);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// --- Main loop ---
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
} }
// --- Cleanup --- // --- Cleanup ---
glDeleteProgram(compute_program); glDeleteProgram(program);
glDeleteBuffers(1, &ssbo); glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

BIN
sand_hello Executable file

Binary file not shown.

12
shaders/double.comp Normal file
View File

@ -0,0 +1,12 @@
#version 460 core
layout(local_size_x = 16) in;
layout(std430, binding = 0) buffer Data {
uint data[];
};
void main() {
uint idx = gl_GlobalInvocationID.x;
data[idx] *= 2u;
}

8
shaders/fullscreen.vert Normal file
View File

@ -0,0 +1,8 @@
#version 430 core
layout(location = 0) in vec2 aPos;
void main()
{
gl_Position = vec4(aPos, 0.0, 1.0);
}

11
shaders/gradient.frag Normal file
View File

@ -0,0 +1,11 @@
#version 430 core
out vec4 FragColor;
void main()
{
// Normalized coordinates in [0,1]
vec2 uv = gl_FragCoord.xy / vec2(800.0, 600.0); // well fix this later with a uniform
vec3 color = vec3(uv.x, uv.y, 0.2);
FragColor = vec4(color, 1.0);
}