refactor project structure
This commit is contained in:
parent
1090cdbd82
commit
ed8fa364eb
23
Makefile
23
Makefile
@ -71,6 +71,7 @@ as_obj_binary = $(subst -,_,$(subst .,_,$(subst /,_,$(subst .h,,$(call makefile_
|
||||
as_obj_binary_p = _binary_$(call as_obj_binary,$(1))
|
||||
|
||||
define BUILD_BINARY_H
|
||||
@mkdir -p $(dir $@)
|
||||
@echo gen $(call makefile_relative,$@)
|
||||
@echo '#pragma once' > $@
|
||||
@echo '' >> $@
|
||||
@ -127,15 +128,29 @@ clean:
|
||||
%.o: %.c
|
||||
$(CC) $(CSTD) $(ARCH) $(CFLAGS) $(CXXFLAGS) $(OPT) $(DEBUG) $(DEPFLAGS) -MF ${<}.d -c $< -o $@
|
||||
|
||||
SHADERS = $(shell find src/ -type f -name '*.glsl')
|
||||
SHADER_OBJS = $(patsubst %,%.o,$(SHADERS))
|
||||
SHADER_HEADERS = $(subst src/,include/,$(patsubst %,%.h,$(SHADERS)))
|
||||
|
||||
shaders: $(SHADER_HEADERS)
|
||||
|
||||
INPUTS = $(shell find puzzle/ -type f -name input)
|
||||
INPUT_OBJS = $(patsubst %,%.o,$(INPUTS))
|
||||
|
||||
SOLUTIONS = $(shell find src/solution/2025/ -type f -name '*.c')
|
||||
SOLUTION_OBJS = $(patsubst %.c,%.o,$(SOLUTIONS))
|
||||
|
||||
MAIN_OBJS = \
|
||||
src/main.o \
|
||||
src/glad.o \
|
||||
src/opengl.o \
|
||||
$(patsubst %.glsl,%.glsl.o,$(wildcard src/shader/*.glsl)) \
|
||||
$(patsubst %,%.o,$(shell find puzzle/ -type f -name input)) \
|
||||
src/input.o \
|
||||
$(SHADER_OBJS) \
|
||||
$(INPUT_OBJS) \
|
||||
$(SOLUTION_OBJS) \
|
||||
$(GLFW)
|
||||
|
||||
main: $(MAIN_OBJS)
|
||||
main: $(MAIN_OBJS) | shaders
|
||||
$(CXX) $^ $(CXXFLAGS) $(ARCH) -o $@ $(LDFLAGS)
|
||||
|
||||
#-include $(shell find -type f -name 'src/*.d')
|
||||
@ -143,7 +158,7 @@ main: $(MAIN_OBJS)
|
||||
.SUFFIXES:
|
||||
.INTERMEDIATE:
|
||||
.SECONDARY:
|
||||
.PHONY: all clean
|
||||
.PHONY: all clean shaders
|
||||
|
||||
%: RCS/%,v
|
||||
%: RCS/%
|
||||
|
||||
11
include/input.h
Normal file
11
include/input.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
unsigned int rectangularize_input(const char * buf, int size, int * width_out);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_shader_day1_fs_glsl_start __asm("_binary_src_shader_day1_fs_glsl_start");
|
||||
extern uint32_t _binary_src_shader_day1_fs_glsl_end __asm("_binary_src_shader_day1_fs_glsl_end");
|
||||
extern uint32_t _binary_src_shader_day1_fs_glsl_size __asm("_binary_src_shader_day1_fs_glsl_size");
|
||||
|
||||
#define src_shader_day1_fs_glsl_start ((const char *)&_binary_src_shader_day1_fs_glsl_start)
|
||||
#define src_shader_day1_fs_glsl_end ((const char *)&_binary_src_shader_day1_fs_glsl_end)
|
||||
#define src_shader_day1_fs_glsl_size (src_shader_day1_fs_glsl_end - src_shader_day1_fs_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_shader_day1_vs_glsl_start __asm("_binary_src_shader_day1_vs_glsl_start");
|
||||
extern uint32_t _binary_src_shader_day1_vs_glsl_end __asm("_binary_src_shader_day1_vs_glsl_end");
|
||||
extern uint32_t _binary_src_shader_day1_vs_glsl_size __asm("_binary_src_shader_day1_vs_glsl_size");
|
||||
|
||||
#define src_shader_day1_vs_glsl_start ((const char *)&_binary_src_shader_day1_vs_glsl_start)
|
||||
#define src_shader_day1_vs_glsl_end ((const char *)&_binary_src_shader_day1_vs_glsl_end)
|
||||
#define src_shader_day1_vs_glsl_size (src_shader_day1_vs_glsl_end - src_shader_day1_vs_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_shader_test_fs_glsl_start __asm("_binary_src_shader_test_fs_glsl_start");
|
||||
extern uint32_t _binary_src_shader_test_fs_glsl_end __asm("_binary_src_shader_test_fs_glsl_end");
|
||||
extern uint32_t _binary_src_shader_test_fs_glsl_size __asm("_binary_src_shader_test_fs_glsl_size");
|
||||
|
||||
#define src_shader_test_fs_glsl_start ((const char *)&_binary_src_shader_test_fs_glsl_start)
|
||||
#define src_shader_test_fs_glsl_end ((const char *)&_binary_src_shader_test_fs_glsl_end)
|
||||
#define src_shader_test_fs_glsl_size (src_shader_test_fs_glsl_end - src_shader_test_fs_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_shader_test_vs_glsl_start __asm("_binary_src_shader_test_vs_glsl_start");
|
||||
extern uint32_t _binary_src_shader_test_vs_glsl_end __asm("_binary_src_shader_test_vs_glsl_end");
|
||||
extern uint32_t _binary_src_shader_test_vs_glsl_size __asm("_binary_src_shader_test_vs_glsl_size");
|
||||
|
||||
#define src_shader_test_vs_glsl_start ((const char *)&_binary_src_shader_test_vs_glsl_start)
|
||||
#define src_shader_test_vs_glsl_end ((const char *)&_binary_src_shader_test_vs_glsl_end)
|
||||
#define src_shader_test_vs_glsl_size (src_shader_test_vs_glsl_end - src_shader_test_vs_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
19
include/solution/2025/01/solution.fs.glsl.h
Normal file
19
include/solution/2025/01/solution.fs.glsl.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_solution_2025_01_solution_fs_glsl_start __asm("_binary_src_solution_2025_01_solution_fs_glsl_start");
|
||||
extern uint32_t _binary_src_solution_2025_01_solution_fs_glsl_end __asm("_binary_src_solution_2025_01_solution_fs_glsl_end");
|
||||
extern uint32_t _binary_src_solution_2025_01_solution_fs_glsl_size __asm("_binary_src_solution_2025_01_solution_fs_glsl_size");
|
||||
|
||||
#define src_solution_2025_01_solution_fs_glsl_start ((const char *)&_binary_src_solution_2025_01_solution_fs_glsl_start)
|
||||
#define src_solution_2025_01_solution_fs_glsl_end ((const char *)&_binary_src_solution_2025_01_solution_fs_glsl_end)
|
||||
#define src_solution_2025_01_solution_fs_glsl_size (src_solution_2025_01_solution_fs_glsl_end - src_solution_2025_01_solution_fs_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
19
include/solution/2025/01/solution.vs.glsl.h
Normal file
19
include/solution/2025/01/solution.vs.glsl.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_solution_2025_01_solution_vs_glsl_start __asm("_binary_src_solution_2025_01_solution_vs_glsl_start");
|
||||
extern uint32_t _binary_src_solution_2025_01_solution_vs_glsl_end __asm("_binary_src_solution_2025_01_solution_vs_glsl_end");
|
||||
extern uint32_t _binary_src_solution_2025_01_solution_vs_glsl_size __asm("_binary_src_solution_2025_01_solution_vs_glsl_size");
|
||||
|
||||
#define src_solution_2025_01_solution_vs_glsl_start ((const char *)&_binary_src_solution_2025_01_solution_vs_glsl_start)
|
||||
#define src_solution_2025_01_solution_vs_glsl_end ((const char *)&_binary_src_solution_2025_01_solution_vs_glsl_end)
|
||||
#define src_solution_2025_01_solution_vs_glsl_size (src_solution_2025_01_solution_vs_glsl_end - src_solution_2025_01_solution_vs_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
19
include/solution/2025/03/parse_input.fs.glsl.h
Normal file
19
include/solution/2025/03/parse_input.fs.glsl.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint32_t _binary_src_solution_2025_03_parse_input_fs_glsl_start __asm("_binary_src_solution_2025_03_parse_input_fs_glsl_start");
|
||||
extern uint32_t _binary_src_solution_2025_03_parse_input_fs_glsl_end __asm("_binary_src_solution_2025_03_parse_input_fs_glsl_end");
|
||||
extern uint32_t _binary_src_solution_2025_03_parse_input_fs_glsl_size __asm("_binary_src_solution_2025_03_parse_input_fs_glsl_size");
|
||||
|
||||
#define src_solution_2025_03_parse_input_fs_glsl_start ((const char *)&_binary_src_solution_2025_03_parse_input_fs_glsl_start)
|
||||
#define src_solution_2025_03_parse_input_fs_glsl_end ((const char *)&_binary_src_solution_2025_03_parse_input_fs_glsl_end)
|
||||
#define src_solution_2025_03_parse_input_fs_glsl_size (src_solution_2025_03_parse_input_fs_glsl_end - src_solution_2025_03_parse_input_fs_glsl_start)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
31
src/input.c
Normal file
31
src/input.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "glad.h"
|
||||
#include "opengl.h"
|
||||
|
||||
static inline int next_power_of_two(double n)
|
||||
{
|
||||
return pow(2, ceil(log2(n)));
|
||||
}
|
||||
|
||||
uint rectangularize_input(const char * buf, int size, int * width_out)
|
||||
{
|
||||
int width = next_power_of_two(sqrt(size));
|
||||
|
||||
void * tmp = malloc(width * width);
|
||||
memcpy(tmp, buf, size);
|
||||
|
||||
uint texture = make_texture(buf,
|
||||
GL_R8, // internalformat
|
||||
width,
|
||||
width,
|
||||
GL_RED,
|
||||
GL_UNSIGNED_BYTE);
|
||||
*width_out = width;
|
||||
|
||||
free(tmp);
|
||||
|
||||
return texture;
|
||||
}
|
||||
142
src/main.c
142
src/main.c
@ -2,21 +2,14 @@
|
||||
#include <unistd.h>
|
||||
#include <math.h>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "glad.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "opengl.h"
|
||||
|
||||
#include "shader/test.vs.glsl.h"
|
||||
#include "shader/test.fs.glsl.h"
|
||||
|
||||
#include "shader/day1.vs.glsl.h"
|
||||
#include "shader/day1.fs.glsl.h"
|
||||
|
||||
#include "puzzle/2025/01/input.h"
|
||||
//#include "shader/test.vs.glsl.h"
|
||||
//#include "shader/test.fs.glsl.h"
|
||||
|
||||
int vp_width = 800;
|
||||
int vp_height = 600;
|
||||
@ -34,30 +27,7 @@ const float triangle_array[] = {
|
||||
1, 1
|
||||
};
|
||||
|
||||
static inline int next_power_of_two(double n)
|
||||
{
|
||||
return pow(2, ceil(log2(n)));
|
||||
}
|
||||
|
||||
uint rectangularize_input(const char * buf, int size, int * width_out)
|
||||
{
|
||||
int width = next_power_of_two(sqrt(size));
|
||||
|
||||
void * tmp = malloc(width * width);
|
||||
memcpy(tmp, buf, size);
|
||||
|
||||
uint texture = make_texture(buf,
|
||||
GL_R8, // internalformat
|
||||
width,
|
||||
width,
|
||||
GL_RED,
|
||||
GL_UNSIGNED_BYTE);
|
||||
*width_out = width;
|
||||
|
||||
free(tmp);
|
||||
|
||||
return texture;
|
||||
}
|
||||
extern void solution_2025_01(unsigned int vertex_array);
|
||||
|
||||
int main()
|
||||
{
|
||||
@ -85,48 +55,6 @@ int main()
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
|
||||
const int buf_width = 4;
|
||||
const int buf_height = 4;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// textures
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
int input_width;
|
||||
uint texture_input = rectangularize_input(puzzle_2025_01_input_start,
|
||||
puzzle_2025_01_input_size,
|
||||
&input_width);
|
||||
int input_height = input_width;
|
||||
int input_length = puzzle_2025_01_input_size;
|
||||
|
||||
uint texture_framebuffer = make_texture(NULL,
|
||||
GL_RGBA32F,
|
||||
buf_width,
|
||||
buf_height,
|
||||
GL_RGBA,
|
||||
GL_UNSIGNED_BYTE);
|
||||
|
||||
uint framebuffer = make_framebuffer(&texture_framebuffer, 1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// shaders
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
uint program_test = compile_shader(src_shader_test_vs_glsl_start,
|
||||
src_shader_test_vs_glsl_size,
|
||||
src_shader_test_fs_glsl_start,
|
||||
src_shader_test_fs_glsl_size);
|
||||
uint program_test__tex_sampler = glGetUniformLocation(program_test, "tex_sampler");
|
||||
uint program_test__tex_size = glGetUniformLocation(program_test, "tex_size");
|
||||
|
||||
uint program_day1 = compile_shader(src_shader_day1_vs_glsl_start,
|
||||
src_shader_day1_vs_glsl_size,
|
||||
src_shader_day1_fs_glsl_start,
|
||||
src_shader_day1_fs_glsl_size);
|
||||
uint program_day1__tex_sampler = glGetUniformLocation(program_day1, "tex_sampler");
|
||||
uint program_day1__tex_size = glGetUniformLocation(program_day1, "tex_size");
|
||||
uint program_day1__input_length = glGetUniformLocation(program_day1, "input_length");
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// buffers
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
@ -147,8 +75,8 @@ int main()
|
||||
(void*)0
|
||||
);
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindVertexArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glBindVertexArray(0);
|
||||
|
||||
int max_texture_size;
|
||||
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size);
|
||||
@ -158,70 +86,10 @@ int main()
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
//
|
||||
solution_2025_01(vertex_array);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
||||
glViewport(0, 0, buf_width, buf_height);
|
||||
|
||||
glClearColor(0.78, 0.56, 0.34, 0.12);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_input);
|
||||
uint draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
|
||||
glDrawBuffers(1, draw_buffers);
|
||||
/*
|
||||
glUseProgram(program_test);
|
||||
glUniform1i(program_test__tex_sampler, 0);
|
||||
glUniform4f(program_test__tex_size,
|
||||
input_width, input_height,
|
||||
0.5f / input_width,
|
||||
0.5f / input_height);
|
||||
*/
|
||||
glUseProgram(program_day1);
|
||||
glUniform1i(program_day1__tex_sampler, 0);
|
||||
glUniform4f(program_day1__tex_size,
|
||||
input_width, input_height,
|
||||
0.5f / input_width,
|
||||
0.5f / input_height);
|
||||
glUniform1f(program_day1__input_length, input_length);
|
||||
glBindVertexArray(vertex_array);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
float out[4 * buf_width * buf_height] = {};
|
||||
glReadPixels(0, 0,
|
||||
buf_width, buf_height,
|
||||
GL_RGBA,
|
||||
GL_FLOAT,
|
||||
out);
|
||||
|
||||
for (int y = 0; y < buf_height; y++) {
|
||||
for (int x = 0; x < buf_width; x++) {
|
||||
printf("[% 2.3f % 2.3f % 2.3f % 2.3f ] ",
|
||||
out[(y * buf_width + x) * 4 + 0],
|
||||
out[(y * buf_width + x) * 4 + 1],
|
||||
out[(y * buf_width + x) * 4 + 2],
|
||||
out[(y * buf_width + x) * 4 + 3]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
break;
|
||||
|
||||
/*
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
glViewport(0, 0, vp_width, vp_height);
|
||||
|
||||
glClearColor(0.1, 0.2, 0.0, 0.4);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(program_test);
|
||||
glBindVertexArray(vertex_array);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
*/
|
||||
|
||||
//glFlush();
|
||||
//glFinish();
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
out vec4 fragment_color;
|
||||
|
||||
uniform sampler2D tex_sampler;
|
||||
uniform vec4 tex_size;
|
||||
|
||||
in vec2 f_position;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 p = texture(tex_sampler, f_position);
|
||||
|
||||
float sum = 0;
|
||||
for (float y = 0; y < tex_size.y; y++) {
|
||||
for (float x = 0; x < tex_size.x; x++) {
|
||||
vec2 coord = vec2((x * 2 + 1) * tex_size.z,
|
||||
(y * 2 + 1) * tex_size.w);
|
||||
|
||||
sum += texture(tex_sampler, coord).x * 255;
|
||||
}
|
||||
}
|
||||
|
||||
fragment_color = vec4(p.x * 255, sum, f_position);
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
#version 330 core
|
||||
|
||||
layout (location = 0) in vec2 position;
|
||||
|
||||
out vec2 f_position;
|
||||
|
||||
void main()
|
||||
{
|
||||
f_position = position * 0.5 + 0.5;
|
||||
|
||||
gl_Position = vec4(position, 0, 1);
|
||||
}
|
||||
105
src/solution/2025/01/solution.c
Normal file
105
src/solution/2025/01/solution.c
Normal file
@ -0,0 +1,105 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "glad.h"
|
||||
#include "input.h"
|
||||
#include "opengl.h"
|
||||
|
||||
#include "puzzle/2025/01/input.h"
|
||||
#include "solution/2025/01/solution.vs.glsl.h"
|
||||
#include "solution/2025/01/solution.fs.glsl.h"
|
||||
|
||||
const int output_width = 1;
|
||||
const int output_height = 1;
|
||||
|
||||
void solution_2025_01(unsigned int vertex_array)
|
||||
{
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// textures
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
const char * input_start = puzzle_2025_01_input_start;
|
||||
const int input_length = puzzle_2025_01_input_size;
|
||||
int input_width;
|
||||
uint texture_input = rectangularize_input(input_start,
|
||||
input_length,
|
||||
&input_width);
|
||||
int input_height = input_width;
|
||||
|
||||
uint texture_output = make_texture(NULL,
|
||||
GL_RGBA32F,
|
||||
output_width,
|
||||
output_height,
|
||||
GL_RGBA,
|
||||
GL_UNSIGNED_BYTE);
|
||||
|
||||
uint framebuffer_output = make_framebuffer(&texture_output, 1);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// shaders
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
uint program = compile_shader(src_solution_2025_01_solution_vs_glsl_start,
|
||||
src_solution_2025_01_solution_vs_glsl_size,
|
||||
src_solution_2025_01_solution_fs_glsl_start,
|
||||
src_solution_2025_01_solution_fs_glsl_size);
|
||||
uint program__input_sampler = glGetUniformLocation(program, "input_sampler");
|
||||
uint program__input_dim = glGetUniformLocation(program, "input_dim");
|
||||
uint program__input_length = glGetUniformLocation(program, "input_length");
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// shader uniforms
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
glUseProgram(program);
|
||||
glUniform1i(program__input_sampler, 0);
|
||||
glUniform4f(program__input_dim,
|
||||
input_width,
|
||||
input_height,
|
||||
0.5f / input_width,
|
||||
0.5f / input_height);
|
||||
glUniform1f(program__input_length, input_length);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// runner
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_output);
|
||||
glViewport(0, 0, output_width, output_height);
|
||||
|
||||
// clear
|
||||
|
||||
glClearColor(0.78, 0.56, 0.34, 0.12);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
// draw
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture_input);
|
||||
const uint draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
|
||||
glDrawBuffers(1, draw_buffers);
|
||||
|
||||
glBindVertexArray(vertex_array);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// output
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
float out[4 * output_width * output_height] = {};
|
||||
glReadPixels(0, 0,
|
||||
output_width, output_height,
|
||||
GL_RGBA,
|
||||
GL_FLOAT,
|
||||
out);
|
||||
|
||||
for (int y = 0; y < output_height; y++) {
|
||||
for (int x = 0; x < output_width; x++) {
|
||||
printf("[% 2.3f % 2.3f % 2.3f % 2.3f ] ",
|
||||
out[(y * output_width + x) * 4 + 0],
|
||||
out[(y * output_width + x) * 4 + 1],
|
||||
out[(y * output_width + x) * 4 + 2],
|
||||
out[(y * output_width + x) * 4 + 3]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
@ -2,17 +2,17 @@
|
||||
|
||||
out vec4 fragment_color;
|
||||
|
||||
uniform sampler2D tex_sampler;
|
||||
uniform vec4 tex_size; // w h halfpx_w halfpx_h
|
||||
uniform sampler2D input_sampler;
|
||||
uniform vec4 input_dim; // w h halfpx_w halfpx_h
|
||||
uniform float input_length;
|
||||
|
||||
vec2 rectangular_position(float ix)
|
||||
{
|
||||
float y = floor(ix * tex_size.z * 2.0);
|
||||
float x = ix - tex_size.x * y;
|
||||
float y = floor(ix * input_dim.z * 2.0);
|
||||
float x = ix - input_dim.x * y;
|
||||
|
||||
return vec2((x * 2.0 + 1) * tex_size.z,
|
||||
(y * 2.0 + 1) * tex_size.w);
|
||||
return vec2((x * 2.0 + 1) * input_dim.z,
|
||||
(y * 2.0 + 1) * input_dim.w);
|
||||
}
|
||||
|
||||
const float ascii_zero = 48.0;
|
||||
@ -21,7 +21,7 @@ const float ascii_l = 76.0;
|
||||
|
||||
float get_input(float ix)
|
||||
{
|
||||
return texture(tex_sampler, rectangular_position(ix)).x * 255.0;
|
||||
return texture(input_sampler, rectangular_position(ix)).x * 255.0;
|
||||
}
|
||||
|
||||
vec2 parse_integer(float ix)
|
||||
34
src/solution/2025/03/parse_input.fs.glsl
Normal file
34
src/solution/2025/03/parse_input.fs.glsl
Normal file
@ -0,0 +1,34 @@
|
||||
#version 330 core
|
||||
|
||||
uniform vec4 in_size; // w h halfpx_w halfpx_h
|
||||
uniform vec4 out_size; // w h halfpx_w halfpx_h
|
||||
|
||||
out vec4 fragment_result;
|
||||
|
||||
in vec2 f_position;
|
||||
|
||||
vec2 halfpixel(float x, float y, vec4 size)
|
||||
{
|
||||
return vec2((x * 2.0 + 1) * size.z,
|
||||
(y * 2.0 + 1) * size.w);
|
||||
}
|
||||
|
||||
vec2 rectangularize(float ix, vec4 size)
|
||||
{
|
||||
float y = floor(ix * size.z * 2.0);
|
||||
float x = ix - size.x * y;
|
||||
|
||||
return halfpixel(x, y, size);
|
||||
}
|
||||
|
||||
float get_input(float ix)
|
||||
{
|
||||
return texture(tex_sampler, rectangularize(ix, in_size)).x * 255.0;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
float x = 0.0;
|
||||
float y = 0.0;
|
||||
float ix = 0.0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user