comparison render_sdl.c @ 1977:f3cca4b3f17a

Allow use of NPOT textures as a config option. Useful for some mobile GPUs
author Michael Pavone <pavone@retrodev.com>
date Sat, 09 May 2020 21:15:33 -0700
parents 3701517d852c
children 33d5b9b77aef
comparison
equal deleted inserted replaced
1976:3dd9c68472fb 1977:f3cca4b3f17a
271 } 271 }
272 } 272 }
273 } 273 }
274 274
275 #ifndef DISABLE_OPENGL 275 #ifndef DISABLE_OPENGL
276 static GLuint textures[3], buffers[2], vshader, fshader, program, un_textures[2], un_width, un_height, at_pos; 276 static GLuint textures[3], buffers[2], vshader, fshader, program, un_textures[2], un_width, un_height, un_texsize, at_pos;
277 static int tex_width, tex_height;
277 278
278 static GLfloat vertex_data_default[] = { 279 static GLfloat vertex_data_default[] = {
279 -1.0f, -1.0f, 280 -1.0f, -1.0f,
280 1.0f, -1.0f, 281 1.0f, -1.0f,
281 -1.0f, 1.0f, 282 -1.0f, 1.0f,
377 { 378 {
378 tern_val def = {.ptrval = "linear"}; 379 tern_val def = {.ptrval = "linear"};
379 char *scaling = tern_find_path_default(config, "video\0scaling\0", def, TVAL_PTR).ptrval; 380 char *scaling = tern_find_path_default(config, "video\0scaling\0", def, TVAL_PTR).ptrval;
380 GLint filter = strcmp(scaling, "linear") ? GL_NEAREST : GL_LINEAR; 381 GLint filter = strcmp(scaling, "linear") ? GL_NEAREST : GL_LINEAR;
381 glGenTextures(3, textures); 382 glGenTextures(3, textures);
383 def.ptrval = "off";
384 char *npot_textures = tern_find_path_default(config, "video\0npot_textures\0", def, TVAL_PTR).ptrval;
385 if (!strcmp(npot_textures, "on")) {
386 tex_width = LINEBUF_SIZE;
387 tex_height = 294; //PAL height with full borders
388 } else {
389 tex_width = tex_height = 512;
390 }
391 printf("Using %dx%d textures\n", tex_width, tex_height);
382 for (int i = 0; i < 3; i++) 392 for (int i = 0; i < 3; i++)
383 { 393 {
384 glBindTexture(GL_TEXTURE_2D, textures[i]); 394 glBindTexture(GL_TEXTURE_2D, textures[i]);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); 395 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
386 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); 396 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 397 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
388 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 398 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
389 if (i < 2) { 399 if (i < 2) {
390 //TODO: Fixme for PAL + invalid display mode 400 //TODO: Fixme for PAL + invalid display mode
391 glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT, 512, 512, 0, SRC_FORMAT, GL_UNSIGNED_BYTE, texture_buf); 401 glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT, tex_width, tex_height, 0, SRC_FORMAT, GL_UNSIGNED_BYTE, texture_buf);
392 } else { 402 } else {
393 uint32_t blank = 255 << 24; 403 uint32_t blank = 255 << 24;
394 glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT, 1, 1, 0, SRC_FORMAT, GL_UNSIGNED_BYTE, &blank); 404 glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT, 1, 1, 0, SRC_FORMAT, GL_UNSIGNED_BYTE, &blank);
395 } 405 }
396 } 406 }
415 } 425 }
416 un_textures[0] = glGetUniformLocation(program, "textures[0]"); 426 un_textures[0] = glGetUniformLocation(program, "textures[0]");
417 un_textures[1] = glGetUniformLocation(program, "textures[1]"); 427 un_textures[1] = glGetUniformLocation(program, "textures[1]");
418 un_width = glGetUniformLocation(program, "width"); 428 un_width = glGetUniformLocation(program, "width");
419 un_height = glGetUniformLocation(program, "height"); 429 un_height = glGetUniformLocation(program, "height");
430 un_texsize = glGetUniformLocation(program, "texsize");
420 at_pos = glGetAttribLocation(program, "pos"); 431 at_pos = glGetAttribLocation(program, "pos");
421 } 432 }
422 433
423 static void gl_teardown() 434 static void gl_teardown()
424 { 435 {
1375 uint32_t *buffer; 1386 uint32_t *buffer;
1376 SDL_LockMutex(free_buffer_mutex); 1387 SDL_LockMutex(free_buffer_mutex);
1377 if (num_buffers) { 1388 if (num_buffers) {
1378 buffer = frame_buffers[--num_buffers]; 1389 buffer = frame_buffers[--num_buffers];
1379 } else { 1390 } else {
1380 buffer = calloc(512*512, sizeof(uint32_t)); 1391 buffer = calloc(tex_width*(tex_height + 1), sizeof(uint32_t));
1381 } 1392 }
1382 SDL_UnlockMutex(free_buffer_mutex); 1393 SDL_UnlockMutex(free_buffer_mutex);
1383 locked_pixels = buffer; 1394 locked_pixels = buffer;
1384 return buffer; 1395 return buffer;
1385 } 1396 }
1731 glBindTexture(GL_TEXTURE_2D, textures[interlaced ? 1 : scanlines ? 2 : 0]); 1742 glBindTexture(GL_TEXTURE_2D, textures[interlaced ? 1 : scanlines ? 2 : 0]);
1732 glUniform1i(un_textures[1], 1); 1743 glUniform1i(un_textures[1], 1);
1733 1744
1734 glUniform1f(un_width, render_emulated_width()); 1745 glUniform1f(un_width, render_emulated_width());
1735 glUniform1f(un_height, last_height); 1746 glUniform1f(un_height, last_height);
1747 glUniform2f(un_texsize, tex_width, tex_height);
1736 1748
1737 glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); 1749 glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
1738 glVertexAttribPointer(at_pos, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat[2]), (void *)0); 1750 glVertexAttribPointer(at_pos, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat[2]), (void *)0);
1739 glEnableVertexAttribArray(at_pos); 1751 glEnableVertexAttribArray(at_pos);
1740 1752