From a17fa47054b4746a9fec58e04b15330c8a507aed Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Fri, 13 Jan 2023 22:12:08 +0100 Subject: [PATCH 1/2] Definition of a 2D Starfield Shader --- CMakeLists.txt | 1 + src/MyAzurShaders.h | 12 +++++- src/main.cpp | 26 +++++++----- src/starfieldshader.cpp | 90 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 11 deletions(-) create mode 100644 src/starfieldshader.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8d53632..8e2dea1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ set(SOURCES src/starfield.cpp src/pixelshader.cpp src/pixellistshader.cpp + src/starfieldshader.cpp # ... ) set(ASSETS_cg diff --git a/src/MyAzurShaders.h b/src/MyAzurShaders.h index 9557aeb..b872f9f 100644 --- a/src/MyAzurShaders.h +++ b/src/MyAzurShaders.h @@ -1,6 +1,16 @@ +#ifndef MYAZURSHADERS_H +#define MYAZURSHADERS_H + + #include "starfield.h" #include void azrp_pixel(int x1, int y1, int color); -void azrp_pixellist(std::vector const &list, int fragnum ); \ No newline at end of file +void azrp_pixellist(std::vector const &list, int fragnum ); + + +void azrp_starfield( void ); +void azrp_starfield_init( void ); + +#endif //MYAZURSHADERS_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 026489e..6680971 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,10 +48,10 @@ uint8_t texttodraw=1; -void Create_Starfield( void ) +/*void Create_Starfield( void ) { MyStarField = new Starfield(); -} +}*/ void Create_Explosion( void ) { @@ -73,6 +73,7 @@ void Create_Explosion( void ) static void update( float dt ) { // all update stuff depending on time will be done here + for(unsigned int i=0; iUpdate( dt ); @@ -85,7 +86,8 @@ static void update( float dt ) } } - MyStarField->Update( dt ); + //MyStarField->Update( dt ); + } @@ -110,6 +112,8 @@ static void get_inputs( void ) if(keydown(KEY_F3)) {texttodraw=2;} if(keydown(KEY_F4)) {texttodraw=3;} + if(keydown(KEY_F6)) {azrp_starfield_init();} + } @@ -129,9 +133,9 @@ int main(void) azrp_shader_image_rgb16_configure(); azrp_shader_image_p8_configure(); azrp_shader_image_p4_configure(); + azrp_shader_triangle_configure(); - - Create_Starfield(); + azrp_starfield_init(); usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL }; @@ -189,8 +193,9 @@ int main(void) for(auto& p : MyParticles) p->Render(); - - MyStarField->Render(); + + + azrp_starfield(); azrp_update(); } @@ -225,11 +230,12 @@ int main(void) if (textoutput && usb_is_open()) { char texttosend[1024]; - for(unsigned int i=0; iMyStars.size(); i++) + + /*for(unsigned int i=0; iMyStars[i]->x, (int) MyStarField->MyStars[i]->y, MyStarField->MyStars[i]->size, MyStarField->MyStars[i]->color ); + sprintf( texttosend, "Star %d : x=%d : y=%d : f=%d : s=%d : c=%d", i, datastar[i].x, datastar[i].y, datastar[i].s, datastar[i].frag, datastar[i].c ); usb_fxlink_text(texttosend, 0); - } + }*/ textoutput = false; } diff --git a/src/starfieldshader.cpp b/src/starfieldshader.cpp new file mode 100644 index 0000000..63b0fd2 --- /dev/null +++ b/src/starfieldshader.cpp @@ -0,0 +1,90 @@ +#include +#include "MyAzurShaders.h" +#include +#include + +#include +#include +#include + +uint8_t AZRP_SHADER_STARFIELD = -1; + + + +#define NBPARTS 100 + +struct particle +{ + int x,y,s,c,frag,off; +}; + +particle datastar[NBPARTS]; + + +__attribute__((constructor)) +static void register_shader(void) +{ + extern azrp_shader_t azrp_shader_starfield; + AZRP_SHADER_STARFIELD = azrp_register_shader(azrp_shader_starfield); +} + + +void azrp_shader_starfield_configure(void) +{ + azrp_set_uniforms(AZRP_SHADER_STARFIELD, (void *)azrp_width); +} + + +struct command { + uint8_t shader_id; + uint8_t current_frag; + particle *data; +}; + + +void azrp_starfield_init( void ) +{ + srand(rtc_ticks()); + + for(int i=0; i> 4) + 1; + + azrp_queue_command(&cmd, sizeof cmd, fragmin, fragcount); + prof_leave(azrp_perf_cmdgen); +} + + +void azrp_shader_starfield( void *uniforms, void *comnd, void *fragment ) +{ + struct command *cmd = (struct command *) comnd; + uint16_t *frag = (uint16_t *) fragment; + particle *data = cmd->data; + + for(int i=0; icurrent_frag = data[i].frag) + { + frag[azrp_width * data[i].off + data[i].x] = data[i].c; + } + + cmd->current_frag++; +} \ No newline at end of file From 3516a285dde8621b4c8ef9520567508304f2ad4b Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Sat, 14 Jan 2023 12:52:36 +0100 Subject: [PATCH 2/2] Working Starfield shader with static Table --- src/starfieldshader.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/starfieldshader.cpp b/src/starfieldshader.cpp index 63b0fd2..6f17810 100644 --- a/src/starfieldshader.cpp +++ b/src/starfieldshader.cpp @@ -66,6 +66,14 @@ void azrp_starfield( void ) cmd.current_frag = 0; cmd.data = datastar; + for(int i=0; i> 4) + 1; @@ -81,7 +89,7 @@ void azrp_shader_starfield( void *uniforms, void *comnd, void *fragment ) particle *data = cmd->data; for(int i=0; icurrent_frag = data[i].frag) + if (cmd->current_frag == data[i].frag) { frag[azrp_width * data[i].off + data[i].x] = data[i].c; }