diff --git a/Makefile b/Makefile index b85a7cb..652c1e2 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ include base.mk include common.mk include headers.mk +OPT = -O2 MAKEFILE_PATH := $(patsubst %/,%,$(dir $(abspath $(firstword $(MAKEFILE_LIST))))) CFLAGS += -I$(MAKEFILE_PATH) LIB ?= $(MAKEFILE_PATH) diff --git a/example/example.mk b/example/example.mk index 05f3985..05728ed 100644 --- a/example/example.mk +++ b/example/example.mk @@ -216,7 +216,8 @@ WIFFLE_SCREEN_SPACE_OBJ = \ holly/region_array.o \ holly/background.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: $(START_OBJ) $(WIFFLE_SCREEN_SPACE_OBJ) diff --git a/example/wiffle_screen_space.cpp b/example/wiffle_screen_space.cpp index c6264eb..bbc6664 100644 --- a/example/wiffle_screen_space.cpp +++ b/example/wiffle_screen_space.cpp @@ -20,6 +20,8 @@ #include "geometry/wiffle.hpp" #include "math/vec4.hpp" +void convolve(uint32_t * in, uint32_t * out); + constexpr float half_degree = 0.01745329f / 2; #define MODEL wiffle @@ -170,6 +172,9 @@ void transform(ta_parameter_writer& parameter, uint32_t ta_parameter_buf[((32 * 8192) + 32) / 4] __attribute__((aligned(32))); +uint32_t inbuf[640 * 480]; +uint32_t outbuf[640 * 480]; + void main() { video_output::set_mode_vga(); @@ -270,13 +275,17 @@ void main() bytes_per_pixel); 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)); holly.FB_R_SOF1 = texture_memory_alloc.framebuffer[0].start; while (spg_status::vsync(holly.SPG_STATUS)); - /* holly.FB_R_CTRL = fb_r_ctrl::vclk_div::pclk_vclk_1 | fb_r_ctrl::fb_depth::_0888_rgb_32bit | fb_r_ctrl::fb_enable; @@ -284,9 +293,7 @@ void main() holly.FB_R_SIZE = fb_r_size::fb_modulus(1) | fb_r_size::fb_y_size(480 - 3) | fb_r_size::fb_x_size((640 * 32) / 32 - 1); - */ - //theta += half_degree; - break; + theta += half_degree; } } diff --git a/holly/texture_memory_alloc3.hpp b/holly/texture_memory_alloc3.hpp index 7479140..fcd1c92 100644 --- a/holly/texture_memory_alloc3.hpp +++ b/holly/texture_memory_alloc3.hpp @@ -30,8 +30,10 @@ constexpr texture_memory_alloc texture_memory_alloc = { .background = {{0x07'ffe0, 0x08'0000}, {0x47'ffe0, 0x48'0000}}, .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 - .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 - .texture = {0x37'1800, 0x80'0000} + //.texture = {0x37'1800, 0x80'0000} + .texture = {0x57'1800, 0x80'0000} }; diff --git a/sobel.cpp b/sobel.cpp new file mode 100644 index 0000000..b15995f --- /dev/null +++ b/sobel.cpp @@ -0,0 +1,102 @@ +#include + +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; + } + } +}