Changeset 708 for soft/giet_vm/applications/raycast
- Timestamp:
- Oct 1, 2015, 4:09:25 PM (9 years ago)
- Location:
- soft/giet_vm/applications/raycast
- Files:
-
- 1 added
- 2 deleted
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/applications/raycast/Makefile
r673 r708 1 2 CC = mipsel-unknown-elf-gcc 3 AS = mipsel-unknown-elf-as 4 LD = mipsel-unknown-elf-ld 5 DU = mipsel-unknown-elf-objdump 6 AR = mipsel-unknown-elf-ar 1 7 2 8 APP_NAME = raycast 3 9 4 OBJS = ctrl.o disp.o game.o main.o10 OBJS = disp.o game.o raycast.o 5 11 6 12 LIBS = -L../../build/libs -luser -lmath -
soft/giet_vm/applications/raycast/disp.c
r692 r708 6 6 #include <math.h> 7 7 #include <hard_config.h> 8 #include <user_ sqt_lock.h>8 #include <user_lock.h> 9 9 10 10 #define FIELD_OF_VIEW (70.f * M_PI / 180.f) // Camera field of view … … 13 13 #define FLOOR_COLOR (0x33) // dark gray 14 14 15 // Globals 16 17 static unsigned char* buf[2]; // framebuffer 18 static void * sts[2]; // for fbf_cma 19 static unsigned int cur_buf; // current framebuffer 20 static volatile unsigned int slice_x; // slice index (shared) 21 static sqt_lock_t slice_x_lock; // slice index lock 22 static volatile unsigned int slice_cnt; // slice count (shared) 23 24 // Textures indexed by block number 25 static unsigned char *g_tex[] = 26 { 27 NULL, // 0 28 NULL, // rock 29 NULL, // door 30 NULL, // handle 31 NULL, // wood 32 }; 33 15 16 //////////////////////////// 17 // Extern variables 18 //////////////////////////// 19 20 extern unsigned char* g_tex[5]; 21 extern unsigned char* buf[2]; 22 extern void* sts[2]; 23 extern unsigned int cur_buf; 24 extern unsigned int slice_x; 25 extern unsigned int slice_count; 26 extern sqt_lock_t slice_get_lock; 27 extern sqt_lock_t slice_done_lock; 28 extern Game game; 29 30 ////////////////////// 34 31 // Local functions 35 32 //////////////////////// 33 34 ///////////////////////////////////////////////////////////////////////// 36 35 static void dispDrawColumnTex(int x, int y0, int y1, unsigned char *line) 37 36 { … … 47 46 } 48 47 48 /////////////////////////////////////////////////////////////////////////// 49 49 static void dispDrawColumnSolid(int x, int y0, int y1, unsigned char color) 50 50 { … … 57 57 } 58 58 59 static void dispDrawSlice(Game *game, int x, int height, int type, int tx) 59 //////////////////////////////////////////////////////////////// 60 static void dispDrawSlice( int x, int height, int type, int tx ) 60 61 { 61 62 // Ceiling … … 90 91 } 91 92 92 static float dispRaycast(Game *game, int *type, float *tx, float angle) 93 { 94 float x = game->player.x; 95 float y = game->player.y; 93 /////////////////////////////////////////////////////////////////////// 94 static float dispRaycast( int *type, float *tx, float angle ) 95 { 96 float x = game.player.x; 97 float y = game.player.y; 96 98 97 99 // Camera is inside a block. … … 162 164 } 163 165 166 //////////////////////////////////////////////////////////////// 164 167 static void dispTranspose(unsigned char *buf, unsigned int size) 165 168 { … … 179 182 } 180 183 181 static unsigned char *dispLoadTexture(char *path) 184 //////////////////////// 185 // Exported functions 186 //////////////////////// 187 188 ////////////////////////////////////////// 189 unsigned char* dispLoadTexture(char *path) 182 190 { 183 191 int fd; … … 186 194 tex = malloc(TEX_SIZE * TEX_SIZE); 187 195 fd = giet_fat_open(path, O_RDONLY); 188 if (fd < 0) { 196 if (fd < 0) 197 { 189 198 free(tex); 190 199 return NULL; … … 196 205 dispTranspose(tex, TEX_SIZE); 197 206 198 giet_tty_printf("[RAYCAST] loaded tex %s\n", path);199 200 207 return tex; 201 208 } 202 209 203 // Exported functions 204 205 void dispInit() 206 { 207 unsigned int w, h, p; 208 209 // Initialize lock 210 giet_procs_number(&w, &h, &p); 211 sqt_lock_init(&slice_x_lock, w, h, p); 212 213 // Allocate framebuffer 214 buf[0] = malloc(FBUF_X_SIZE * FBUF_Y_SIZE); 215 buf[1] = malloc(FBUF_X_SIZE * FBUF_Y_SIZE); 216 sts[0] = malloc(64); 217 sts[1] = malloc(64); 218 219 // Initialize framebuffer 220 giet_fbf_cma_alloc(); 221 giet_fbf_cma_init_buf(buf[0], buf[1], sts[0], sts[1]); 222 giet_fbf_cma_start(FBUF_X_SIZE * FBUF_Y_SIZE); 223 224 // Load textures 225 g_tex[1] = dispLoadTexture("misc/rock_32.raw"); 226 g_tex[2] = dispLoadTexture("misc/door_32.raw"); 227 g_tex[3] = dispLoadTexture("misc/handle_32.raw"); 228 g_tex[4] = dispLoadTexture("misc/wood_32.raw"); 229 230 cur_buf = 0; 231 slice_cnt = 0; 232 slice_x = FBUF_X_SIZE; 233 } 234 235 int dispRenderSlice(Game *game) 236 { 237 unsigned int x; 210 /////////////////////////////////////////////////// 211 unsigned int dispRenderSlice( unsigned int* slice ) 212 { 213 // return 0 when there is no more slice to do 214 238 215 int type; 239 216 float angle, dist, tx; 240 241 sqt_lock_acquire(&slice_x_lock); 242 243 if (slice_x == FBUF_X_SIZE) { 244 // No more work to do for this frame 245 sqt_lock_release(&slice_x_lock); 217 unsigned int x; 218 219 // get a slice index 220 sqt_lock_acquire( &slice_get_lock ); 221 222 if (slice_x >= FBUF_X_SIZE) // No more work to do for this frame 223 { 224 sqt_lock_release( &slice_get_lock ); 246 225 return 0; 247 226 } 248 else { 249 // Keep slice coordinate 250 x = slice_x++; 251 } 252 253 sqt_lock_release(&slice_x_lock); 254 255 angle = game->player.dir - FIELD_OF_VIEW / 2.f + 227 else // Keep slice coordinate 228 { 229 x = slice_x; 230 slice_x = x + 1; 231 sqt_lock_release( &slice_get_lock ); 232 *slice = x; 233 } 234 235 // Cast a ray to get wall distance 236 angle = game.player.dir - FIELD_OF_VIEW / 2.f + 256 237 x * FIELD_OF_VIEW / FBUF_X_SIZE; 257 238 258 // Cast a ray to get wall distance 259 dist = dispRaycast(game, &type, &tx, angle); 260 261 // Perspective correction 262 dist *= cos(game->player.dir - angle); 239 dist = dispRaycast(&type, &tx, angle); 240 241 dist *= cos(game.player.dir - angle); 263 242 264 243 // Draw ceiling, wall and floor 265 dispDrawSlice( game,x, FBUF_Y_SIZE / dist, type, tx * TEX_SIZE);244 dispDrawSlice(x, FBUF_Y_SIZE / dist, type, tx * TEX_SIZE); 266 245 267 246 // Signal this slice is done 268 atomic_increment((unsigned int*)&slice_cnt, 1); 247 248 sqt_lock_acquire( &slice_done_lock ); 249 slice_count++; 250 sqt_lock_release( &slice_done_lock ); 269 251 270 252 return 1; 271 } 272 273 void dispRender(Game *game) 274 { 275 int start = giet_proctime(); 276 277 // Start rendering 278 slice_cnt = 0; 279 slice_x = 0; 280 281 // Render slices 282 while (dispRenderSlice(game)); 283 284 // Wait for completion 285 while (slice_cnt != FBUF_X_SIZE); 286 287 // Flip framebuffer 288 giet_fbf_cma_display(cur_buf); 289 cur_buf = !cur_buf; 290 giet_tty_printf("[RAYCAST] flip (took %d cycles)\n", giet_proctime() - start); 291 } 292 253 } // end dispRenderSlice() 254 255 -
soft/giet_vm/applications/raycast/disp.h
r683 r708 4 4 #include "game.h" 5 5 6 void dispInit(); 7 int dispRenderSlice(Game *game); 8 void dispRender(Game *game); 6 unsigned char* dispLoadTexture( char* path ); 7 unsigned int dispRenderSlice( unsigned int* slice ); 9 8 10 9 #endif // __DISP_H -
soft/giet_vm/applications/raycast/game.c
r683 r708 3 3 #include "ctrl.h" 4 4 #include <math.h> 5 6 // Globals 5 #include <stdio.h> 6 7 ////////////////////////////// 8 // Game Maps 9 ////////////////////////////// 7 10 8 11 static Map map[] = … … 70 73 }; 71 74 72 static Game game = 73 { 74 .mapId = 0 75 }; 76 77 static bool g_exit; 78 75 //////////////////////////// 76 // extern variables 77 //////////////////////////// 78 79 extern Game game; 80 81 ////////////////////// 79 82 // Local functions 80 83 ////////////////////// 84 85 //////////////////////////////////// 81 86 static void gameOnBlockHit(int type) 82 87 { 83 g_exit = true; 84 } 85 88 giet_tty_printf("\n[RAYCAST] Fatal collision, killed...\n"); 89 game.exit = 1; 90 } 91 92 /////////////////////////////////////////////// 86 93 static void gameCollision(float opx, float opy) 87 94 { 88 static bool collided_x = false;89 static bool collided_y = false;90 91 float px = game.player.x;92 float py = game.player.y;93 int fpx = floor(px);94 int fpy = floor(py);95 bool colliding_x = false;96 bool colliding_y = false;97 int collide_type_x = 0;98 int collide_type_y = 0;99 int type;95 static unsigned int collided_x = 0; 96 static unsigned int collided_y = 0; 97 98 float px = game.player.x; 99 float py = game.player.y; 100 int fpx = floor(px); 101 int fpy = floor(py); 102 unsigned int colliding_x = 0; 103 unsigned int colliding_y = 0; 104 int collide_type_x = 0; 105 int collide_type_y = 0; 106 int type; 100 107 101 108 // Check for x axis collisions 102 if ((type = gameLocate(floor(px + COLLIDE_GAP), fpy))) { 103 colliding_x = true, collide_type_x = type; 109 if ((type = gameLocate(floor(px + COLLIDE_GAP), fpy))) 110 { 111 colliding_x = 1, collide_type_x = type; 104 112 game.player.x = fpx - COLLIDE_GAP + 1; 105 113 } 106 else if ((type = gameLocate(floor(px - COLLIDE_GAP), fpy))) { 107 colliding_x = true, collide_type_x = type; 114 else if ((type = gameLocate(floor(px - COLLIDE_GAP), fpy))) 115 { 116 colliding_x = 1, collide_type_x = type; 108 117 game.player.x = fpx + COLLIDE_GAP; 109 118 } 110 119 111 120 // Check for y axis collisions 112 if ((type = gameLocate(fpx, floor(py + COLLIDE_GAP)))) { 113 colliding_y = true, collide_type_y = type; 121 if ((type = gameLocate(fpx, floor(py + COLLIDE_GAP)))) 122 { 123 colliding_y = 1, collide_type_y = type; 114 124 game.player.y = fpy - COLLIDE_GAP + 1; 115 125 } 116 else if ((type = gameLocate(fpx, floor(py - COLLIDE_GAP)))) { 117 colliding_y = true, collide_type_y = type; 126 else if ((type = gameLocate(fpx, floor(py - COLLIDE_GAP)))) 127 { 128 colliding_y = 1, collide_type_y = type; 118 129 game.player.y = fpy + COLLIDE_GAP; 119 130 } 120 131 121 132 // Check if we're inside a wall 122 if ((type = gameLocate(fpx, fpy))) { 123 colliding_x = true, collide_type_x = type; 124 colliding_y = true, collide_type_y = type; 125 game.player.x = opx, game.player.y = opy; 133 if ((type = gameLocate(fpx, fpy))) 134 { 135 colliding_x = 1 , collide_type_x = type; 136 colliding_y = 1 , collide_type_y = type; 137 game.player.x = opx , game.player.y = opy; 126 138 } 127 139 … … 136 148 } 137 149 138 static void gameLogic() 139 { 140 float opx = game.player.x; 141 float opy = game.player.y; 142 143 ctrlLogic(&game); 144 gameCollision(opx, opy); 145 } 146 147 static void gameInitMap() 148 { 149 game.map = &map[game.mapId]; 150 game.player.x = game.map->startX; 151 game.player.y = game.map->startY; 150 ///////////////////////// 151 // Exported functions 152 ///////////////////////// 153 154 /////////////// 155 void gameInit() 156 { 157 game.mapId = 0; 158 game.map = &map[0]; 159 game.player.x = game.map->startX; 160 game.player.y = game.map->startY; 152 161 game.player.dir = game.map->startDir; 153 game.timeLeft = TIME_TOTAL; 154 } 155 156 // Exported functions 157 162 game.timeLeft = TIME_TOTAL; 163 game.exit = 0; 164 } 165 166 ////////////////// 167 void gameControl() 168 { 169 // Only six commands are accepted: 170 // - key Q : quit game 171 // - key UP : forward move 172 // - key DOWN : backward move 173 // - key RIGHT : right move 174 // - key LEFT : left move 175 // - any other key : continue 176 // several moves can be made before continue 177 178 char c; 179 unsigned int state = 0; 180 unsigned int done = 0; 181 182 // display prompt 183 giet_tty_printf("\n[RAYCAST] > " ); 184 185 // get one command 186 while ( done == 0 ) 187 { 188 // get one character 189 giet_tty_getc( &c ); 190 191 if (state == 0) // first character 192 { 193 if (c == 0x1B) // ESCAPE : possible player move 194 { 195 state = 1; 196 } 197 else if (c == 0x71) // quit game 198 { 199 game.exit = 1; 200 done = 1; 201 giet_tty_printf("QUIT\n"); 202 } 203 else // continue 204 { 205 done = 1; 206 giet_tty_printf("\n"); 207 } 208 } 209 else if (state == 1) // previous character was ESCAPE 210 { 211 if (c == 0x5B) // BRAKET : possible player move 212 { 213 state = 2; 214 } 215 else // continue 216 { 217 done = 1; 218 giet_tty_printf("\n"); 219 } 220 } 221 else // previous characters were ESCAPE,BRACKET 222 { 223 if (c == 0x41) // UP arrow <=> forward move 224 { 225 game.player.x += PLAYER_MOVE * cos(game.player.dir); 226 game.player.y += PLAYER_MOVE * sin(game.player.dir); 227 giet_tty_printf("GO "); 228 state = 0; 229 } 230 else if (c == 0x42) // DOWN arrow <=> backward move 231 { 232 game.player.x -= PLAYER_MOVE * cos(game.player.dir); 233 game.player.y -= PLAYER_MOVE * sin(game.player.dir); 234 giet_tty_printf("BACK "); 235 state = 0; 236 } 237 else if (c == 0x43) // RIGHT arrow <=> turn right 238 { 239 game.player.dir += PLAYER_ROT; 240 giet_tty_printf("RIGHT "); 241 state = 0; 242 } 243 else if (c == 0x44) // LEFT arrow <=> turn left 244 { 245 game.player.dir -= PLAYER_ROT; 246 giet_tty_printf("LEFT "); 247 state = 0; 248 } 249 else // continue 250 { 251 done = 1; 252 giet_tty_printf("\n"); 253 } 254 } 255 } // end while 256 257 // check new position 258 int hit = gameLocate( game.player.x , game.player.y ); 259 if ( hit ) gameOnBlockHit( hit ); 260 } 261 262 //////////////////////////// 158 263 int gameLocate(int x, int y) 159 264 { 160 265 if ((x < 0 || x >= game.map->w) || 161 (y < 0 || y >= game.map->h)) { 266 (y < 0 || y >= game.map->h)) 267 { 162 268 // Outside the map bounds 163 269 return 1; … … 167 273 } 168 274 169 void gameRun() 170 { 171 gameInitMap(); 172 173 g_exit = false; 174 175 // Game loop 176 while (!g_exit) { 177 gameLogic(); 178 dispRender(&game); 179 } 180 181 if (game.timeLeft == 0) { 182 // Time's up! 183 game.mapId = 0; 184 } 185 else { 186 // Go to next map 187 game.mapId++; 188 } 189 } 190 275 /////////////// 191 276 void gameTick() 192 277 { 193 278 game.timeLeft--; 194 279 195 if (game.timeLeft == 0) { 196 g_exit = true; 197 } 198 } 199 200 Game *gameInstance() 201 { 202 return &game; 203 } 280 if (game.timeLeft == 0) 281 { 282 game.exit = 1; 283 } 284 } 285 -
soft/giet_vm/applications/raycast/game.h
r683 r708 2 2 #define __GAME_H 3 3 4 #include <stdint.h>5 #include <stdbool.h>6 4 7 5 #define M_PI (3.14159f) … … 11 9 #define TIME_TOTAL (30) 12 10 13 typedef struct 11 typedef struct 14 12 { 15 13 float x; … … 20 18 typedef struct 21 19 { 22 uint8_ttile[10][10];23 uint8_tw;24 uint8_th;25 float startX;26 float startY;27 float startDir;20 char tile[10][10]; 21 char w; 22 char h; 23 float startX; 24 float startY; 25 float startDir; 28 26 } Map; 29 27 30 28 typedef struct 31 { 32 Player player; 33 Map *map; 34 int mapId; 35 int timeLeft; 29 { 30 Player player; 31 Map *map; 32 int mapId; 33 int timeLeft; 34 int exit; // exit requested 36 35 } Game; 37 36 38 int gameLocate(int x, int y); 39 void gameRun(); 37 38 void gameInit(); 39 int gameLocate(int x, int y); 40 void gameControl(); 40 41 void gameTick(); 41 Game *gameInstance();42 42 43 43 #endif // __GAME_H -
soft/giet_vm/applications/raycast/raycast.py
r683 r708 10 10 # This file describes the mapping of the multi-threaded "raycast" application 11 11 # on a multi-clusters, multi-processors architecture. 12 # The mapping of t asks on processors is the following:13 # - one "main" t askon (0,0,0)14 # - one "render" t askper processor but (0,0,0)12 # The mapping of threads on processors is the following: 13 # - one "main" thread on (0,0,0) 14 # - one "render" thread per processor but (0,0,0) 15 15 # The mapping of virtual segments is the following: 16 16 # - There is one shared data vseg in cluster[0][0] … … 89 89 mapping.addVseg( vspace, 'raycast_heap_%d_%d' %(x,y), base , size, 90 90 'C_WU', vtype = 'HEAP', x = x, y = y, pseg = 'RAM', 91 local = False )91 local = False, big = True ) 92 92 93 # distributed t asks / one taskper processor93 # distributed threads / one thread per processor 94 94 for x in xrange (x_size): 95 95 for y in xrange (y_size): … … 98 98 for p in xrange( nprocs ): 99 99 trdid = (((x * y_size) + y) * nprocs) + p 100 if ( x == 0 and y == 0 and p == 0 ): # main t ask101 task_index= 1102 task_name = 'main_%d_%d_%d' %(x,y,p)103 else: # render t ask104 task_index= 0105 task_name = 'render_%d_%d_%d' % (x,y,p)100 if ( x == 0 and y == 0 and p == 0 ): # main thread 101 start_id = 1 102 is_main = True 103 else: # render thread 104 start_id = 0 105 is_main = False 106 106 107 mapping.addTask( vspace, task_name, trdid, x, y, p, 108 'raycast_stack_%d_%d_%d' % (x,y,p), 109 'raycast_heap_%d_%d' % (x,y), 110 task_index ) 107 mapping.addThread( vspace, 108 'raycast_%d_%d_%d' % (x,y,p), 109 is_main, 110 x, y, p, 111 'raycast_stack_%d_%d_%d' % (x,y,p), 112 'raycast_heap_%d_%d' % (x,y), 113 start_id ) 111 114 112 115 # extend mapping name
Note: See TracChangeset
for help on using the changeset viewer.