screen space sobel

This commit is contained in:
Zack Buhman 2025-01-25 03:48:57 -06:00
parent ddd7e1dcb6
commit 65d768bdaf
5 changed files with 120 additions and 7 deletions

View File

@ -4,6 +4,7 @@ include base.mk
include common.mk include common.mk
include headers.mk include headers.mk
OPT = -O2
MAKEFILE_PATH := $(patsubst %/,%,$(dir $(abspath $(firstword $(MAKEFILE_LIST))))) MAKEFILE_PATH := $(patsubst %/,%,$(dir $(abspath $(firstword $(MAKEFILE_LIST)))))
CFLAGS += -I$(MAKEFILE_PATH) CFLAGS += -I$(MAKEFILE_PATH)
LIB ?= $(MAKEFILE_PATH) LIB ?= $(MAKEFILE_PATH)

View File

@ -216,7 +216,8 @@ WIFFLE_SCREEN_SPACE_OBJ = \
holly/region_array.o \ holly/region_array.o \
holly/background.o \ holly/background.o \
holly/ta_fifo_polygon_converter.o \ holly/ta_fifo_polygon_converter.o \
sh7091/serial.o sh7091/serial.o \
sobel.o
example/wiffle_screen_space.elf: LDSCRIPT = $(LIB)/main.lds example/wiffle_screen_space.elf: LDSCRIPT = $(LIB)/main.lds
example/wiffle_screen_space.elf: $(START_OBJ) $(WIFFLE_SCREEN_SPACE_OBJ) example/wiffle_screen_space.elf: $(START_OBJ) $(WIFFLE_SCREEN_SPACE_OBJ)

View File

@ -20,6 +20,8 @@
#include "geometry/wiffle.hpp" #include "geometry/wiffle.hpp"
#include "math/vec4.hpp" #include "math/vec4.hpp"
void convolve(uint32_t * in, uint32_t * out);
constexpr float half_degree = 0.01745329f / 2; constexpr float half_degree = 0.01745329f / 2;
#define MODEL wiffle #define MODEL wiffle
@ -170,6 +172,9 @@ void transform(ta_parameter_writer& parameter,
uint32_t ta_parameter_buf[((32 * 8192) + 32) / 4] uint32_t ta_parameter_buf[((32 * 8192) + 32) / 4]
__attribute__((aligned(32))); __attribute__((aligned(32)));
uint32_t inbuf[640 * 480];
uint32_t outbuf[640 * 480];
void main() void main()
{ {
video_output::set_mode_vga(); video_output::set_mode_vga();
@ -270,13 +275,17 @@ void main()
bytes_per_pixel); bytes_per_pixel);
core_wait_end_of_render_video(); core_wait_end_of_render_video();
uint32_t * in = (uint32_t *)&texture_memory64[texture_memory_alloc.texture.start / 4];
uint32_t * out = (uint32_t *)&texture_memory32[texture_memory_alloc.framebuffer[0].start / 4];
convolve(in, out);
while (!spg_status::vsync(holly.SPG_STATUS)); while (!spg_status::vsync(holly.SPG_STATUS));
holly.FB_R_SOF1 = texture_memory_alloc.framebuffer[0].start; holly.FB_R_SOF1 = texture_memory_alloc.framebuffer[0].start;
while (spg_status::vsync(holly.SPG_STATUS)); while (spg_status::vsync(holly.SPG_STATUS));
/*
holly.FB_R_CTRL = fb_r_ctrl::vclk_div::pclk_vclk_1 holly.FB_R_CTRL = fb_r_ctrl::vclk_div::pclk_vclk_1
| fb_r_ctrl::fb_depth::_0888_rgb_32bit | fb_r_ctrl::fb_depth::_0888_rgb_32bit
| fb_r_ctrl::fb_enable; | fb_r_ctrl::fb_enable;
@ -284,9 +293,7 @@ void main()
holly.FB_R_SIZE = fb_r_size::fb_modulus(1) holly.FB_R_SIZE = fb_r_size::fb_modulus(1)
| fb_r_size::fb_y_size(480 - 3) | fb_r_size::fb_y_size(480 - 3)
| fb_r_size::fb_x_size((640 * 32) / 32 - 1); | fb_r_size::fb_x_size((640 * 32) / 32 - 1);
*/
//theta += half_degree; theta += half_degree;
break;
} }
} }

View File

@ -30,8 +30,10 @@ constexpr texture_memory_alloc texture_memory_alloc = {
.background = {{0x07'ffe0, 0x08'0000}, {0x47'ffe0, 0x48'0000}}, .background = {{0x07'ffe0, 0x08'0000}, {0x47'ffe0, 0x48'0000}},
.object_list = {{0x08'0000, 0x0f'ffe0}, {0x48'0000, 0x4f'ffe0}}, // ~122880 object list pointers .object_list = {{0x08'0000, 0x0f'ffe0}, {0x48'0000, 0x4f'ffe0}}, // ~122880 object list pointers
.region_array = {{0x10'0000, 0x11'0000}, {0x50'0000, 0x51'0000}}, // ~9 render passes .region_array = {{0x10'0000, 0x11'0000}, {0x50'0000, 0x51'0000}}, // ~9 render passes
.framebuffer = {{0x11'0000, 0x1b'8c00}, {0x51'0000, 0x5b'8c00}}, // 720x480*2bpp //.framebuffer = {{0x11'0000, 0x1b'8c00}, {0x51'0000, 0x5b'8c00}}, // 720x480*2
.framebuffer = {{0x11'0000, 0x23'c000}, {0x51'0000, 0x63'c000}}, // 640x480*4
// 64-bit addresses // 64-bit addresses
.texture = {0x37'1800, 0x80'0000} //.texture = {0x37'1800, 0x80'0000}
.texture = {0x57'1800, 0x80'0000}
}; };

102
sobel.cpp Normal file
View File

@ -0,0 +1,102 @@
#include <stdint.h>
int clamp255(float v)
{
int n = (int)v;
if (n < 0)
return 0;
if (n > 255)
return 255;
return n;
}
uint32_t getpx(uint32_t * buf, int x, int y)
{
if (x < 0)
x = 0;
if (y < 0)
y = 0;
if (x >= 640)
x = 640 - 1;
if (y >= 480)
y = 480 - 1;
return buf[y * 640 + x];
}
float multiply(uint32_t * buf, int x, int y, float weight)
{
uint32_t color = getpx(buf, x, y);
int b = color & 0xff;
color >>= 8;
int g = color & 0xff;
color >>= 8;
int r = color & 0xff;
color >>= 8;
int a = color;
float luminance = (float)(r + g + b + a) * 0.25;
return luminance * (float)weight;
}
float kernel(uint32_t * buf, const float * weights, int x, int y)
{
float c = 0;
c += multiply(buf, x - 1, y - 1, weights[0]);
c += multiply(buf, x , y - 1, weights[1]);
c += multiply(buf, x + 1, y - 1, weights[2]);
c += multiply(buf, x - 1, y , weights[3]);
c += multiply(buf, x , y , weights[4]);
c += multiply(buf, x + 1, y , weights[5]);
c += multiply(buf, x - 1, y + 1, weights[6]);
c += multiply(buf, x , y + 1, weights[7]);
c += multiply(buf, x + 1, y + 1, weights[8]);
return c;
}
const float gx[] = {
1, 0, -1,
2, 0, -2,
1, 0, -1,
};
const float gy[] = {
1, 2, 1,
0, 0, 0,
-1, -2, -1,
};
void convolve(uint32_t * in, uint32_t * out)
{
for (int y = 0; y < 480; y++) {
for (int x = 0; x < 640; x++) {
float vx = kernel(in, gx, x, y);
float vy = kernel(in, gy, x, y);
float c = vx * vx + vy * vy;
int d = c > 100.f ? 0 : 1;
uint32_t color = in[y * 640 + x];
int b = color & 0xff;
color >>= 8;
int g = color & 0xff;
color >>= 8;
int r = color & 0xff;
color >>= 8;
int a = color;
uint32_t color_out = 0;
//color_out |= (a * d);
//color_out <<= 8;
color_out |= (r * d);
color_out <<= 8;
color_out |= (g * d);
color_out <<= 8;
color_out |= (b * d);
out[y * 640 + x] = color_out;
}
}
}