comparison render_sdl.c @ 426:add9e2f5c0e3

Make VDP render in native pixel format of the renderer for a modest performance gain and to make it easier to use OpenGL for rendering
author Mike Pavone <pavone@retrodev.com>
date Sun, 30 Jun 2013 11:45:58 -0700
parents d0cacb4ade0b
children 18cde14e8c10 7696d824489d
comparison
equal deleted inserted replaced
425:8b3ae850d1c4 426:add9e2f5c0e3
7 SDL_Surface *screen; 7 SDL_Surface *screen;
8 uint8_t render_dbg = 0; 8 uint8_t render_dbg = 0;
9 uint8_t debug_pal = 0; 9 uint8_t debug_pal = 0;
10 10
11 uint32_t last_frame = 0; 11 uint32_t last_frame = 0;
12
13 int32_t color_map[1 << 12];
14 uint8_t levels[] = {0, 27, 49, 71, 87, 103, 119, 130, 146, 157, 174, 190, 206, 228, 255};
15 12
16 uint32_t min_delay; 13 uint32_t min_delay;
17 uint32_t frame_delay = 1000/60; 14 uint32_t frame_delay = 1000/60;
18 15
19 int16_t * current_psg = NULL; 16 int16_t * current_psg = NULL;
74 } 71 }
75 72
76 SDL_Joystick * joysticks[MAX_JOYSTICKS]; 73 SDL_Joystick * joysticks[MAX_JOYSTICKS];
77 int num_joysticks; 74 int num_joysticks;
78 75
76 uint32_t render_map_color(uint8_t r, uint8_t g, uint8_t b)
77 {
78 return SDL_MapRGB(screen->format, r, g, b);
79 }
80
81 uint8_t render_depth()
82 {
83 return screen->format->BytesPerPixel * 8;
84 }
85
79 void render_init(int width, int height, char * title, uint32_t fps) 86 void render_init(int width, int height, char * title, uint32_t fps)
80 { 87 {
81 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) { 88 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
82 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); 89 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
83 exit(1); 90 exit(1);
88 screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_ANYFORMAT); 95 screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_ANYFORMAT);
89 if (!screen) { 96 if (!screen) {
90 fprintf(stderr, "Unable to get SDL surface: %s\n", SDL_GetError()); 97 fprintf(stderr, "Unable to get SDL surface: %s\n", SDL_GetError());
91 exit(1); 98 exit(1);
92 } 99 }
93 if (screen->format->BytesPerPixel < 2) { 100 if (screen->format->BytesPerPixel != 2 && screen->format->BytesPerPixel != 4) {
94 fprintf(stderr, "BlastEm requires at least a 16-bit surface, SDL returned a %d-bit surface\n", screen->format->BytesPerPixel * 8); 101 fprintf(stderr, "BlastEm requires a 16-bit or 32-bit surface, SDL returned a %d-bit surface\n", screen->format->BytesPerPixel * 8);
95 exit(1); 102 exit(1);
96 } 103 }
97 SDL_WM_SetCaption(title, title); 104 SDL_WM_SetCaption(title, title);
98 uint8_t b,g,r;
99 for (uint16_t color = 0; color < (1 << 12); color++) {
100 if (color & FBUF_SHADOW) {
101 b = levels[(color >> 9) & 0x7];
102 g = levels[(color >> 5) & 0x7];
103 r = levels[(color >> 1) & 0x7];
104 } else if(color & FBUF_HILIGHT) {
105 b = levels[((color >> 9) & 0x7) + 7];
106 g = levels[((color >> 5) & 0x7) + 7];
107 r = levels[((color >> 1) & 0x7) + 7];
108 } else {
109 b = levels[(color >> 8) & 0xE];
110 g = levels[(color >> 4) & 0xE];
111 r = levels[color & 0xE];
112 }
113 color_map[color] = SDL_MapRGB(screen->format, r, g, b);
114 }
115 min_delay = 0; 105 min_delay = 0;
116 for (int i = 0; i < 100; i++) { 106 for (int i = 0; i < 100; i++) {
117 uint32_t start = SDL_GetTicks(); 107 uint32_t start = SDL_GetTicks();
118 SDL_Delay(1); 108 SDL_Delay(1);
119 uint32_t delay = SDL_GetTicks()-start; 109 uint32_t delay = SDL_GetTicks()-start;
161 } 151 }
162 } 152 }
163 SDL_JoystickEventState(SDL_ENABLE); 153 SDL_JoystickEventState(SDL_ENABLE);
164 } 154 }
165 155
166 uint16_t blankbuf[320*240]; 156 uint32_t blankbuf[320*240];
167 157
168 void render_context(vdp_context * context) 158 void render_context(vdp_context * context)
169 { 159 {
170 uint8_t *buf_8;
171 uint16_t *buf_16; 160 uint16_t *buf_16;
172 uint32_t *buf_32; 161 uint32_t *buf_32;
173 uint8_t b,g,r; 162 uint8_t b,g,r;
174 last_frame = SDL_GetTicks(); 163 last_frame = SDL_GetTicks();
175 if (SDL_MUSTLOCK(screen)) { 164 if (SDL_MUSTLOCK(screen)) {
183 repeat_x = repeat_y; 172 repeat_x = repeat_y;
184 } else { 173 } else {
185 repeat_y = repeat_x; 174 repeat_y = repeat_x;
186 } 175 }
187 int othermask = repeat_y >> 1; 176 int othermask = repeat_y >> 1;
188 uint16_t *otherbuf = (context->regs[REG_MODE_4] & BIT_INTERLACE) ? context->evenbuf : blankbuf; 177
189 switch (screen->format->BytesPerPixel) { 178 if (screen->format->BytesPerPixel == 2) {
190 case 2: 179 uint16_t *otherbuf = (context->regs[REG_MODE_4] & BIT_INTERLACE) ? context->evenbuf : (uint16_t *)blankbuf;
191 buf_16 = (uint16_t *)screen->pixels; 180 uint16_t * oddbuf = context->oddbuf;
192 for (int y = 0; y < 240; y++) { 181 buf_16 = (uint16_t *)screen->pixels;
193 for (int i = 0; i < repeat_y; i++,buf_16 += screen->pitch/2) { 182 for (int y = 0; y < 240; y++) {
183 for (int i = 0; i < repeat_y; i++,buf_16 += screen->pitch/2) {
194 uint16_t *line = buf_16; 184 uint16_t *line = buf_16;
195 uint16_t *src_line = (i & othermask ? otherbuf : context->oddbuf) + y * 320; 185 uint16_t *src_line = (i & othermask ? otherbuf : oddbuf) + y * 320;
196 for (int x = 0; x < 320; x++) { 186 for (int x = 0; x < 320; x++) {
197 uint16_t color = color_map[*(src_line++) & 0xFFF]; 187 uint16_t color = *(src_line++);
198 for (int j = 0; j < repeat_x; j++) { 188 for (int j = 0; j < repeat_x; j++) {
199 *(line++) = color; 189 *(line++) = color;
200 } 190 }
201 } 191 }
202 } 192 }
203 } 193 }
204 break; 194 } else {
205 case 3: 195 uint32_t *otherbuf = (context->regs[REG_MODE_4] & BIT_INTERLACE) ? context->evenbuf : (uint32_t *)blankbuf;
206 buf_8 = (uint8_t *)screen->pixels; 196 uint32_t * oddbuf = context->oddbuf;
207 for (int y = 0; y < 240; y++) { 197 buf_32 = (uint32_t *)screen->pixels;
208 for (int i = 0; i < repeat_y; i++,buf_8 += screen->pitch) { 198 for (int y = 0; y < 240; y++) {
209 uint8_t *line = buf_8; 199 for (int i = 0; i < repeat_y; i++,buf_32 += screen->pitch/4) {
200 uint32_t *line = buf_32;
201 uint32_t *src_line = (i & othermask ? otherbuf : oddbuf) + y * 320;
210 for (int x = 0; x < 320; x++) { 202 for (int x = 0; x < 320; x++) {
211 uint16_t gen_color = context->oddbuf[y * 320 + x]; 203 uint32_t color = *(src_line++);
212 b = ((gen_color >> 8) & 0xE) * 18;
213 g = ((gen_color >> 4) & 0xE) * 18;
214 r = (gen_color& 0xE) * 18;
215 for (int j = 0; j < repeat_x; j++) { 204 for (int j = 0; j < repeat_x; j++) {
216 *(buf_8+screen->format->Rshift/8) = r; 205 *(line++) = color;
217 *(buf_8+screen->format->Gshift/8) = g; 206 }
218 *(buf_8+screen->format->Bshift/8) = b;
219 buf_8 += 3;
220 }
221 } 207 }
222 } 208 }
223 } 209 }
224 break; 210 }
225 case 4:
226 buf_32 = (uint32_t *)screen->pixels;
227
228 for (int y = 0; y < 240; y++) {
229 for (int i = 0; i < repeat_y; i++,buf_32 += screen->pitch/4) {
230 uint32_t *line = buf_32;
231 uint16_t *src_line = (i & othermask ? otherbuf : context->oddbuf) + y * 320;
232 for (int x = 0; x < 320; x++) {
233 uint32_t color;
234 if (!render_dbg) {
235 color = color_map[*(src_line++) & 0xFFF];
236 } else if(render_dbg == 2) {
237 color = color_map[context->cram[(y/30)*8 + x/40]];
238 } else if(render_dbg == 3) {
239 if (x & 1) {
240 color = color_map[context->cram[ (debug_pal << 4) | (context->vdpmem[(x/8)*32 + (y/8)*32*40 + (x%8)/2 + (y%8)*4] & 0xF) ]];
241 } else {
242 color = color_map[context->cram[ (debug_pal << 4) | (context->vdpmem[(x/8)*32 + (y/8)*32*40 + (x%8)/2 + (y%8)*4] >> 4) ]];
243 }
244 }else {
245 uint16_t gen_color = context->oddbuf[y * 320 + x];
246 r = g = b = 0;
247 switch(gen_color & FBUF_SRC_MASK)
248 {
249 case FBUF_SRC_A:
250 g = 127;//plane a = green
251 break;
252 case FBUF_SRC_W:
253 g = 127;//window = cyan
254 b = 127;
255 break;
256 case FBUF_SRC_B:
257 b = 127;//plane b = blue
258 break;
259 case FBUF_SRC_S:
260 r = 127;//sprite = red
261 break;
262 case FBUF_SRC_BG:
263 r = 127;//BG = purple
264 b = 127;
265 }
266 if (gen_color & FBUF_BIT_PRIORITY) {
267 b *= 2;
268 g *= 2;
269 r *= 2;
270 }
271 if (gen_color & FBUF_SHADOW) {
272 b /= 2;
273 g /= 2;
274 r /= 2;
275 } else if(gen_color & FBUF_HILIGHT) {
276 b = b ? b : 64;
277 g = g ? g : 64;
278 r = r ? r : 64;
279 }
280 color = SDL_MapRGB(screen->format, r, g, b);
281 }
282 for (int j = 0; j < repeat_x; j++) {
283 *(line++) = color;
284 }
285 }
286 }
287 }
288 break;
289 }
290 if ( SDL_MUSTLOCK(screen) ) { 211 if ( SDL_MUSTLOCK(screen) ) {
291 SDL_UnlockSurface(screen); 212 SDL_UnlockSurface(screen);
292 } 213 }
293 SDL_UpdateRect(screen, 0, 0, screen->clip_rect.w, screen->clip_rect.h); 214 SDL_UpdateRect(screen, 0, 0, screen->clip_rect.w, screen->clip_rect.h);
294 if (context->regs[REG_MODE_4] & BIT_INTERLACE) 215 if (context->regs[REG_MODE_4] & BIT_INTERLACE)