From afcbbfc10f884ac8bcce592733eec1e650177e2b Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Tue, 10 Jan 2023 12:31:08 +0100 Subject: [PATCH 1/4] Attempt to develop a PixelList shader - WIP --- CMakeLists.txt | 1 + src/MyAzurShaders.h | 4 +- src/main.cpp | 24 ++----- src/pixellistshader.cpp | 50 ++++++++++++++ src/starfield.cpp | 147 ++++++++++++++++++++++++++++++---------- src/starfield.h | 38 ++++++++++- 6 files changed, 207 insertions(+), 57 deletions(-) create mode 100644 src/pixellistshader.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 855c84b..8d53632 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ set(SOURCES src/particles.cpp src/starfield.cpp src/pixelshader.cpp + src/pixellistshader.cpp # ... ) set(ASSETS_cg diff --git a/src/MyAzurShaders.h b/src/MyAzurShaders.h index 0984d65..d092fda 100644 --- a/src/MyAzurShaders.h +++ b/src/MyAzurShaders.h @@ -1,4 +1,6 @@ - +#include "starfield.h" +#include void azrp_pixel(int x1, int y1, int color); +void azrp_pixellist(std::vector list, int fragnum ); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 45bafab..3c601b8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,29 +38,23 @@ bool exitToOS = false; #define X_RESOL (DWIDTH / SCALE_PIXEL) #define Y_RESOL (DHEIGHT / SCALE_PIXEL) -#define BIAS 1 +#define BIAS 0 #define NOBIAS (1-BIAS) std::vector MyParticles; -std::vector MyStars; +Starfield *MyStarField; uint8_t texttodraw=1; void Create_Starfield( void ) { - srand(rtc_ticks()); - - for(int i=0; i<100; i++) - { - Star *s = new Star( ); - MyStars.push_back( s ); - } + MyStarField = new Starfield(); } void Create_Explosion( void ) { - if(MyParticles.size()>=300) return; + if(MyParticles.size()>=150) return; srand(rtc_ticks()); @@ -78,7 +72,7 @@ void Create_Explosion( void ) static void update( float dt ) { // all update stuff depending on time will be done here - for(int i=0; iUpdate( dt ); @@ -90,8 +84,7 @@ static void update( float dt ) } } - for(auto&s : MyStars) - s->Update( dt ); + MyStarField->Update( dt ); } @@ -194,8 +187,7 @@ int main(void) for(auto& p : MyParticles) p->Render(); - for(auto& s : MyStars) - s->Render(); + MyStarField->Render(); azrp_update(); } @@ -233,8 +225,6 @@ int main(void) for(auto& p : MyParticles) delete(p); MyParticles.clear(); - for(auto& s : MyStars) delete(s); - MyStars.clear(); prof_quit(); diff --git a/src/pixellistshader.cpp b/src/pixellistshader.cpp new file mode 100644 index 0000000..d30c5e3 --- /dev/null +++ b/src/pixellistshader.cpp @@ -0,0 +1,50 @@ +#include +#include "MyAzurShaders.h" +#include "starfield.h" +#include + + +uint8_t AZRP_SHADER_PIXELLIST = -1; + + +__attribute__((constructor)) +static void register_shader(void) +{ + extern azrp_shader_t azrp_shader_pixellist; + AZRP_SHADER_PIXELLIST = azrp_register_shader(azrp_shader_pixellist); +} + + +void azrp_shader_pixellist_configure(void) +{ + azrp_set_uniforms(AZRP_SHADER_PIXELLIST, (void *)azrp_width); +} + + +struct command { + uint8_t shader_id; + std::vector data; +}; + + +void azrp_pixellist(std::vector list, int fragnum ) +{ + prof_enter(azrp_perf_cmdgen); + + struct command cmd; + cmd.shader_id = AZRP_SHADER_PIXELLIST; + cmd.data = list; + + azrp_queue_command(&cmd, sizeof cmd, fragnum, 1); + prof_leave(azrp_perf_cmdgen); +} + + +void azrp_shader_pixellist( void *uniforms, void *command, void *fragment ) +{ + struct command *cmd = (struct command *) command; + uint16_t *frag = (uint16_t *) fragment; + + for( auto& pix : cmd->data ) + frag[azrp_width * pix->y + pix->x] = pix->c; +} \ No newline at end of file diff --git a/src/starfield.cpp b/src/starfield.cpp index c29725e..48d689b 100644 --- a/src/starfield.cpp +++ b/src/starfield.cpp @@ -5,6 +5,7 @@ #include #include +#include #include @@ -21,7 +22,12 @@ Star::Star( void ) sy = libnum::num( size ); - color = 0xFFFF; + int colorrandom = rand() % 4; + + if (colorrandom==0) color = 0xFFFF; + else if (colorrandom==1) color = 0xFFE0; + else if (colorrandom==2) color = 0xFB80; + else color = 0xF80D; } Star::~Star() @@ -30,50 +36,117 @@ Star::~Star() } -void Star::Update( float dt ) -{ - libnum::num a = libnum::num( dt / 12000.0f ); - y += sy * a; +void Star::Update( libnum::num dt ) +{ + //libnum::num a = libnum::num( dt / 12000.0f ); + //y += sy * a; + y+= sy * dt; - if (y > 224) + if (y >= 224) { x = rand() % 396; - y = 0; + y = libnum::num( 0 ); } } -void Star::Render( ) + + +Starfield::Starfield( ) { - if (size==1) + srand(rtc_ticks()); + + for(int i=0; i<100; i++) { - azrp_pixel( x, (int) y, color ); - } - else if (size==2) - { - azrp_pixel( x-1, (int) y-1, color ); - azrp_pixel( x-1, (int) y, color ); - azrp_pixel( x, (int) y-1, color ); - azrp_pixel( x, (int) y, color ); - } - else if (size==3) - { - azrp_pixel( x, (int) y-1, color ); - azrp_pixel( x-1, (int) y, color ); - azrp_pixel( x, (int) y, color ); - azrp_pixel( x+1, (int) y, color ); - azrp_pixel( x, (int) y+1, color ); - } - else if (size==4) - { - azrp_pixel( x-1, (int) y-1, color ); - azrp_pixel( x-1, (int) y, color ); - azrp_pixel( x-1, (int) y+1, color ); - azrp_pixel( x, (int) y-1, color ); - azrp_pixel( x, (int) y, color ); - azrp_pixel( x, (int) y+1, color ); - azrp_pixel( x+1, (int) y-1, color ); - azrp_pixel( x+1, (int) y, color ); - azrp_pixel( x+1, (int) y+1, color ); + Star *s = new Star( ); + MyStars.push_back( s ); } } +Starfield::~Starfield( ) +{ + for(auto& s : MyStars) + delete(s); + + for(auto& pf : PixelListPerFragment) + pf.clear(); + + MyStars.clear(); +} + +void Starfield::Update( float dt ) +{ +// for(auto& s : MyStars) +// s->Update( dt ); + + libnum::num a = libnum::num( dt / 120000.f ); + for(auto& s : MyStars) + s->Update( a ); +} + +inline void Starfield::AddPixel( int x, int y, int c ) +{ + uint8_t current_frag = 0; + + // check if the point is in the range screen + if(x >= azrp_width || x < 0 || y >= azrp_height || y < 0) + return; + + current_frag = y >> 4; // each fragment is 16pixel high : so fragment number for the current point is y/16 or y>>4 + Pixel *MyPix = new Pixel( x, (int) y, c ); + PixelListPerFragment[ current_frag ].push_back( MyPix ); // add the pixel to the appropriate list +} + +void Starfield::Render( void ) +{ + for(auto& list : PixelListPerFragment) + { + for(auto& p : list) + delete(p); + list.clear(); + } + + for(auto& s : MyStars) + { + //TODO :The only considered case is for a small star 1x1 pixel + //TODO :Other sizes to be added right after this case +// if (s->size==1) +// {} + AddPixel( s->x, (int) s->y, s->color ); +// } + +/* + else if (s->size==2) + { + AddPixel( s->x, (int) s->y, s->color ); + AddPixel( s->x+1, (int) s->y, s->color ); + AddPixel( s->x, (int) s->y+1, s->color ); + AddPixel( s->x+1, (int) s->y+1, s->color ); + } + else if (s->size==3) + { + AddPixel( s->x+1, (int) s->y, s->color ); + AddPixel( s->x-1, (int) s->y+1, s->color ); + AddPixel( s->x, (int) s->y+1, s->color ); + AddPixel( s->x+1, (int) s->y+1, s->color ); + AddPixel( s->x+1, (int) s->y+2, s->color ); + } + else if (s->size==4) + { + AddPixel( s->x-1, (int) s->y-1, s->color ); + AddPixel( s->x-1, (int) s->y, s->color ); + AddPixel( s->x-1, (int) s->y+1, s->color ); + AddPixel( s->x, (int) s->y-1, s->color ); + AddPixel( s->x, (int) s->y, s->color ); + AddPixel( s->x, (int) s->y+1, s->color ); + AddPixel( s->x+1, (int) s->y-1, s->color ); + AddPixel( s->x+1, (int) s->y, s->color ); + AddPixel( s->x+1, (int) s->y+1, s->color ); + } +*/ + } + + + // call the PixelList shader with the appropriate lists + for(unsigned int i=0; i #include +#include +#include + class Star { public: Star(); ~Star(); - void Update( float dt ); - void Render(); + //void Update( float dt ); + void Update( libnum::num dt ); + uint16_t x; libnum::num y; @@ -19,4 +23,34 @@ class Star uint16_t color; }; +class Pixel +{ + public: + int x,y,c; + Pixel( int _x, int _y, int _c) + { + x = _x; + y = _y; + c = _c; + }; + ~Pixel() {}; +}; + +class Starfield +{ + public: + // the Star collection + std::vector MyStars; + // list of all pixels to be rendered by Azur pixel shader fragment by fragment + std::array,14> PixelListPerFragment; + + Starfield( ); + ~Starfield( ); + void Update( float dt ); + void Render( void ); + + private: + void AddPixel( int x, int y, int c ); +}; + #endif //STARS_H \ No newline at end of file From aa1d14976debc24ae5b840ac4889d6da7e2d7752 Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Tue, 10 Jan 2023 18:54:29 +0100 Subject: [PATCH 2/4] Big bug - Crash in azrp_shader_pixellist( void *, void*, void *) --- src/main.cpp | 21 +++++++++++++++++++-- src/pixellistshader.cpp | 6 +++--- src/starfield.cpp | 37 ++++++++++++++++++++----------------- src/starfield.h | 2 +- 4 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 3c601b8..f2d15d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,13 +32,14 @@ bool screenshot = false; bool record = false; +bool textoutput = false; bool exitToOS = false; #define SCALE_PIXEL 1 #define X_RESOL (DWIDTH / SCALE_PIXEL) #define Y_RESOL (DHEIGHT / SCALE_PIXEL) -#define BIAS 0 +#define BIAS 1 #define NOBIAS (1-BIAS) std::vector MyParticles; @@ -102,6 +103,8 @@ static void get_inputs( void ) if(keydown(KEY_7)) {screenshot = true; }; if(keydown(KEY_8)) {record = !record; }; + if(keydown(KEY_9)) {textoutput = true; }; + if(keydown(KEY_F1)) {texttodraw=0;} if(keydown(KEY_F2)) {texttodraw=1;} if(keydown(KEY_F3)) {texttodraw=2;} @@ -195,6 +198,8 @@ int main(void) prof_leave(perf_render); time_render = prof_time(perf_render); + getkey(); + #if(NOBIAS) dclear(C_BLACK); dprint(1,01, C_WHITE, "Update = %.0f mc secs", (float) time_update ); @@ -218,7 +223,19 @@ int main(void) { usb_fxlink_videocapture(false); } - } + + if (usb_is_open()) + { + char texttosend[1024]; + for(unsigned int i=0; iMyStars.size(); i++) + { + sprintf( texttosend, "Star %d : x=%d : y=%d : s=%d : c=%d", i, MyStarField->MyStars[i]->x, (int) MyStarField->MyStars[i]->y, MyStarField->MyStars[i]->size, MyStarField->MyStars[i]->color ); + usb_fxlink_text(texttosend, 0); + } + + textoutput = false; + } + } while (exitToOS==false); diff --git a/src/pixellistshader.cpp b/src/pixellistshader.cpp index d30c5e3..1e8c462 100644 --- a/src/pixellistshader.cpp +++ b/src/pixellistshader.cpp @@ -40,11 +40,11 @@ void azrp_pixellist(std::vector list, int fragnum ) } -void azrp_shader_pixellist( void *uniforms, void *command, void *fragment ) +void azrp_shader_pixellist( void *uniforms, void *comnd, void *fragment ) { - struct command *cmd = (struct command *) command; + struct command *cmd = (struct command *) comnd; uint16_t *frag = (uint16_t *) fragment; for( auto& pix : cmd->data ) - frag[azrp_width * pix->y + pix->x] = pix->c; + frag[azrp_width * (pix->y & 15) + pix->x] = pix->c; } \ No newline at end of file diff --git a/src/starfield.cpp b/src/starfield.cpp index 48d689b..24ffe5a 100644 --- a/src/starfield.cpp +++ b/src/starfield.cpp @@ -55,7 +55,7 @@ Starfield::Starfield( ) { srand(rtc_ticks()); - for(int i=0; i<100; i++) + for(int i=0; i<10; i++) { Star *s = new Star( ); MyStars.push_back( s ); @@ -67,8 +67,12 @@ Starfield::~Starfield( ) for(auto& s : MyStars) delete(s); - for(auto& pf : PixelListPerFragment) - pf.clear(); + for(auto& list : PixelListPerFragment) + { + for(auto& p : list) + delete(p); + list.clear(); + } MyStars.clear(); } @@ -83,16 +87,14 @@ void Starfield::Update( float dt ) s->Update( a ); } -inline void Starfield::AddPixel( int x, int y, int c ) +void Starfield::AddPixel( int x, int y, int c ) { - uint8_t current_frag = 0; - // check if the point is in the range screen if(x >= azrp_width || x < 0 || y >= azrp_height || y < 0) return; - current_frag = y >> 4; // each fragment is 16pixel high : so fragment number for the current point is y/16 or y>>4 - Pixel *MyPix = new Pixel( x, (int) y, c ); + uint8_t current_frag = y >> 4; // each fragment is 16pixel high : so fragment number for the current point is y/16 or y>>4 + Pixel *MyPix = new Pixel( x, y & 15, c ); // consider the local offset of the point in the current fragment (y & 15) PixelListPerFragment[ current_frag ].push_back( MyPix ); // add the pixel to the appropriate list } @@ -101,7 +103,9 @@ void Starfield::Render( void ) for(auto& list : PixelListPerFragment) { for(auto& p : list) + { delete(p); + } list.clear(); } @@ -109,12 +113,10 @@ void Starfield::Render( void ) { //TODO :The only considered case is for a small star 1x1 pixel //TODO :Other sizes to be added right after this case -// if (s->size==1) -// {} + if (s->size==1) + { AddPixel( s->x, (int) s->y, s->color ); -// } - -/* + } else if (s->size==2) { AddPixel( s->x, (int) s->y, s->color ); @@ -141,12 +143,13 @@ void Starfield::Render( void ) AddPixel( s->x+1, (int) s->y-1, s->color ); AddPixel( s->x+1, (int) s->y, s->color ); AddPixel( s->x+1, (int) s->y+1, s->color ); - } -*/ + } } // call the PixelList shader with the appropriate lists - for(unsigned int i=0; i Date: Tue, 10 Jan 2023 19:06:01 +0100 Subject: [PATCH 3/4] Crash next - Stars correctly created and updated - Rendering is the source --- src/main.cpp | 4 +--- src/pixellistshader.cpp | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f2d15d7..afc1f62 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -198,8 +198,6 @@ int main(void) prof_leave(perf_render); time_render = prof_time(perf_render); - getkey(); - #if(NOBIAS) dclear(C_BLACK); dprint(1,01, C_WHITE, "Update = %.0f mc secs", (float) time_update ); @@ -224,7 +222,7 @@ int main(void) usb_fxlink_videocapture(false); } - if (usb_is_open()) + if (textouput && usb_is_open()) { char texttosend[1024]; for(unsigned int i=0; iMyStars.size(); i++) diff --git a/src/pixellistshader.cpp b/src/pixellistshader.cpp index 1e8c462..db8d59e 100644 --- a/src/pixellistshader.cpp +++ b/src/pixellistshader.cpp @@ -46,5 +46,5 @@ void azrp_shader_pixellist( void *uniforms, void *comnd, void *fragment ) uint16_t *frag = (uint16_t *) fragment; for( auto& pix : cmd->data ) - frag[azrp_width * (pix->y & 15) + pix->x] = pix->c; + frag[azrp_width * pix->y + pix->x] = pix->c; } \ No newline at end of file From 758abed55fc737de5373459c0442a5310e2dea09 Mon Sep 17 00:00:00 2001 From: Slyvtt Date: Tue, 10 Jan 2023 21:53:49 +0100 Subject: [PATCH 4/4] PixelList shader fixed with Lephe's help --- src/MyAzurShaders.h | 2 +- src/main.cpp | 2 +- src/pixellistshader.cpp | 12 ++++++++---- src/starfield.cpp | 9 +++++---- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/MyAzurShaders.h b/src/MyAzurShaders.h index d092fda..9557aeb 100644 --- a/src/MyAzurShaders.h +++ b/src/MyAzurShaders.h @@ -3,4 +3,4 @@ void azrp_pixel(int x1, int y1, int color); -void azrp_pixellist(std::vector list, int fragnum ); \ No newline at end of file +void azrp_pixellist(std::vector const &list, int fragnum ); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index afc1f62..026489e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -222,7 +222,7 @@ int main(void) usb_fxlink_videocapture(false); } - if (textouput && usb_is_open()) + if (textoutput && usb_is_open()) { char texttosend[1024]; for(unsigned int i=0; iMyStars.size(); i++) diff --git a/src/pixellistshader.cpp b/src/pixellistshader.cpp index db8d59e..a0b57a9 100644 --- a/src/pixellistshader.cpp +++ b/src/pixellistshader.cpp @@ -23,17 +23,19 @@ void azrp_shader_pixellist_configure(void) struct command { uint8_t shader_id; - std::vector data; + uint16_t length; + Pixel * const *data; }; -void azrp_pixellist(std::vector list, int fragnum ) +void azrp_pixellist(std::vector const &list, int fragnum ) { prof_enter(azrp_perf_cmdgen); struct command cmd; cmd.shader_id = AZRP_SHADER_PIXELLIST; - cmd.data = list; + cmd.length = list.size(); + cmd.data = list.data(); azrp_queue_command(&cmd, sizeof cmd, fragnum, 1); prof_leave(azrp_perf_cmdgen); @@ -45,6 +47,8 @@ void azrp_shader_pixellist( void *uniforms, void *comnd, void *fragment ) struct command *cmd = (struct command *) comnd; uint16_t *frag = (uint16_t *) fragment; - for( auto& pix : cmd->data ) + for(int i = 0; i < cmd->length; i++) { + Pixel *pix = cmd->data[i]; frag[azrp_width * pix->y + pix->x] = pix->c; + } } \ No newline at end of file diff --git a/src/starfield.cpp b/src/starfield.cpp index 24ffe5a..53fc556 100644 --- a/src/starfield.cpp +++ b/src/starfield.cpp @@ -24,10 +24,13 @@ Star::Star( void ) int colorrandom = rand() % 4; + color = 0xFFFF; +/* if (colorrandom==0) color = 0xFFFF; else if (colorrandom==1) color = 0xFFE0; else if (colorrandom==2) color = 0xFB80; else color = 0xF80D; +*/ } Star::~Star() @@ -55,7 +58,7 @@ Starfield::Starfield( ) { srand(rtc_ticks()); - for(int i=0; i<10; i++) + for(int i=0; i<100; i++) { Star *s = new Star( ); MyStars.push_back( s ); @@ -79,10 +82,8 @@ Starfield::~Starfield( ) void Starfield::Update( float dt ) { -// for(auto& s : MyStars) -// s->Update( dt ); + libnum::num a = libnum::num( dt / 50000.f ); - libnum::num a = libnum::num( dt / 120000.f ); for(auto& s : MyStars) s->Update( a ); }