Added simple render stub to test shader

This commit is contained in:
2022-05-27 10:18:34 +02:00
parent d9a4e8085b
commit 58441c79aa
2 changed files with 87 additions and 0 deletions

View File

@@ -45,6 +45,8 @@ add_executable(bluebell
src/bluebell.cpp src/bluebell.hpp
src/pre-compiler.cpp src/pre-compiler.hpp src/compiler.cpp src/compiler.hpp src/spv_meta.cpp src/spv_meta.hpp src/runtime.cpp src/runtime.hpp)
add_executable(render_stub
src/render_frame/render_stub.c)
message(STATUS "Include dirs are ${LLVM_INCLUDE_DIRS}")
#target_link_libraries(bluebell PkgConfig::shaderc)

View File

@@ -0,0 +1,85 @@
//
// Created by thequux on 5/27/22.
//
#define _GNU_SOURCE
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdio.h>
#include <err.h>
// render API:
struct Uniforms {
float iResolution[3];
float iTime;
float iTimeDelta;
float iFrame;
float iChannelTime[4];
float iMouse[4];
float iDate[4];
float iSampleRate;
float iChannelResolution[3][4];
} uniforms = {
.iResolution = {32, 32},
.iTime = 0,
.iTimeDelta = 0,
.iFrame = 0,
.iChannelTime = {0,0,0,0},
.iMouse = 0,
.iDate = {0,0,0,0},
.iSampleRate = 0,
.iChannelResolution = {}
};
struct frame_context_t;
void setup_frame(struct frame_context_t* ctx, struct Uniforms *uniforms);
void render_pixel(struct frame_context_t* ctx, float x, float y, float pixel[3]);
unsigned int get_context_size(void);
// program utilities
const char* PPM_HDR = "P6\n32 32\n255\n";
unsigned char frame_buf[32][32][3];
int main(int argc, char** argv) {
unsigned header_len = strlen(PPM_HDR);
float fdelta = 1. / 30.;
struct frame_context_t *ctx = malloc(get_context_size());
for (int frame = 0; frame < 100; frame++) {
uniforms.iFrame = frame;
uniforms.iTimeDelta = fdelta;
uniforms.iTime = fdelta * frame;
setup_frame(ctx, &uniforms);
for (int y = 0; y < 32; y++) {
for (int x = 0; x < 32; x++) {
float pixel[3];
unsigned char *pxl_dst = frame_buf[y][x];
render_pixel(ctx, (x + 0.5), (y + 0.5), pixel);
for (int i = 0; i < 3; i++) {
pixel[i] *= 256;
if (pixel[i] < 0) { pixel[i] = 0; }
if (pixel[i] > 255) { pixel[i] = 255; }
pxl_dst[i] = (unsigned char) pixel[i];
}
}
}
char* fname;
asprintf(&fname, "render/frame%04d.ppm", frame);
int fd = open(fname, O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
err(1, "Failed to open %s", fname);
}
write(fd, PPM_HDR, header_len);
write(fd, frame_buf, 32*32*3);
close(fd);
printf("%s\n", fname);
free(fname);
}
return 0;
}