2 * Glide64 - Glide video plugin for Nintendo 64 emulators.
3 * Copyright (c) 2002 Dave2001
4 * Copyright (c) 2003-2009 Sergey 'Gonetz' Lipski
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "../Glide64/winlnxdefs.h"
29 #include "../Glide64/rdp.h"
33 #define Z_MAX (65536.0f)
34 #define VERTEX_SIZE sizeof(VERTEX) //Size of vertex struct
37 //#include "ae_bridge.h"
38 static float polygonOffsetFactor;
39 static float polygonOffsetUnits;
54 static int fog_ext_off;
55 static int fog_ext_en;
61 #define VERTEX_BUFFER_SIZE 1500 //Max amount of vertices to buffer, this seems large enough.
62 static VERTEX vertex_buffer[VERTEX_BUFFER_SIZE];
63 static int vertex_buffer_count = 0;
64 static GLenum vertex_draw_mode;
65 static bool vertex_buffer_enabled = false;
74 if(vertex_buffer_count)
76 glDrawArrays(vertex_draw_mode,0,vertex_buffer_count);
77 vertex_buffer_count = 0;
84 vertex_buffer_count = 0;
88 //Buffer vertices instead of glDrawArrays(...)
89 void vbo_buffer(GLenum mode,GLint first,GLsizei count,void* pointers)
91 if((count != 3 && mode != GL_TRIANGLES) || vertex_buffer_count + count > VERTEX_BUFFER_SIZE)
96 memcpy(&vertex_buffer[vertex_buffer_count],pointers,count * VERTEX_SIZE);
97 vertex_buffer_count += count;
99 if(count == 3 || mode == GL_TRIANGLES)
101 vertex_draw_mode = GL_TRIANGLES;
105 vertex_draw_mode = mode;
106 vbo_draw(); //Triangle fans and strips can't be joined as easily, just draw them straight away.
114 if(vertex_buffer_enabled)
117 vertex_buffer_enabled = true;
118 glEnableVertexAttribArray(POSITION_ATTR);
119 glVertexAttribPointer(POSITION_ATTR, 4, GL_FLOAT, false, VERTEX_SIZE, &vertex_buffer[0].x); //Position
121 glEnableVertexAttribArray(COLOUR_ATTR);
122 glVertexAttribPointer(COLOUR_ATTR, 4, GL_UNSIGNED_BYTE, true, VERTEX_SIZE, &vertex_buffer[0].b); //Colour
124 glEnableVertexAttribArray(TEXCOORD_0_ATTR);
125 glVertexAttribPointer(TEXCOORD_0_ATTR, 2, GL_FLOAT, false, VERTEX_SIZE, &vertex_buffer[0].coord[2]); //Tex0
127 glEnableVertexAttribArray(TEXCOORD_1_ATTR);
128 glVertexAttribPointer(TEXCOORD_1_ATTR, 2, GL_FLOAT, false, VERTEX_SIZE, &vertex_buffer[0].coord[0]); //Tex1
130 glEnableVertexAttribArray(FOG_ATTR);
131 glVertexAttribPointer(FOG_ATTR, 1, GL_FLOAT, false, VERTEX_SIZE, &vertex_buffer[0].f); //Fog
137 vertex_buffer_enabled = false;
141 inline float ZCALC(const float & z, const float & q) {
142 float res = z_en ? ((z) / Z_MAX) / (q) : 1.0f;
147 #define zclamp (1.0f-1.0f/zscale)
148 static inline void zclamp_glVertex4f(float a, float b, float c, float d)
150 if (c<zclamp) c = zclamp;
153 #define glVertex4f(a,b,c,d) zclamp_glVertex4f(a,b,c,d)
157 static inline float ytex(int tmu, float y) {
159 return invtex[tmu] - y;
166 xy_en = q_en = pargb_en = st0_en = st1_en = z_en = 0;
168 inverted_culling = 0;
170 glDisable(GL_CULL_FACE);
171 glDisable(GL_DEPTH_TEST);
176 FX_ENTRY void FX_CALL
177 grCoordinateSpace( GrCoordinateSpaceMode_t mode )
179 LOG("grCoordinateSpace(%d)\r\n", mode);
182 case GR_WINDOW_COORDS:
185 display_warning("unknwown coordinate space : %x", mode);
189 FX_ENTRY void FX_CALL
190 grVertexLayout(FxU32 param, FxI32 offset, FxU32 mode)
192 LOG("grVertexLayout(%d,%d,%d)\r\n", param, offset, mode);
207 case GR_PARAM_FOG_EXT:
209 fog_ext_off = offset;
224 display_warning("unknown grVertexLayout parameter : %x", param);
228 FX_ENTRY void FX_CALL
229 grCullMode( GrCullMode_t mode )
231 LOG("grCullMode(%d)\r\n", mode);
232 static int oldmode = -1, oldinv = -1;
234 if (inverted_culling == oldinv && oldmode == mode)
237 oldinv = inverted_culling;
240 case GR_CULL_DISABLE:
241 glDisable(GL_CULL_FACE);
243 case GR_CULL_NEGATIVE:
244 if (!inverted_culling)
245 glCullFace(GL_FRONT);
248 glEnable(GL_CULL_FACE);
250 case GR_CULL_POSITIVE:
251 if (!inverted_culling)
254 glCullFace(GL_FRONT);
255 glEnable(GL_CULL_FACE);
258 display_warning("unknown cull mode : %x", mode);
264 FX_ENTRY void FX_CALL
265 grDepthBufferMode( GrDepthBufferMode_t mode )
267 LOG("grDepthBufferMode(%d)\r\n", mode);
270 case GR_DEPTHBUFFER_DISABLE:
271 glDisable(GL_DEPTH_TEST);
274 case GR_DEPTHBUFFER_WBUFFER:
275 case GR_DEPTHBUFFER_WBUFFER_COMPARE_TO_BIAS:
276 glEnable(GL_DEPTH_TEST);
279 case GR_DEPTHBUFFER_ZBUFFER:
280 case GR_DEPTHBUFFER_ZBUFFER_COMPARE_TO_BIAS:
281 glEnable(GL_DEPTH_TEST);
285 display_warning("unknown depth buffer mode : %x", mode);
289 FX_ENTRY void FX_CALL
290 grDepthBufferFunction( GrCmpFnc_t function )
292 LOG("grDepthBufferFunction(%d)\r\n", function);
297 glDepthFunc(GL_LEQUAL);
299 glDepthFunc(GL_GEQUAL);
303 glDepthFunc(GL_GEQUAL);
305 glDepthFunc(GL_LEQUAL);
309 glDepthFunc(GL_GREATER);
311 glDepthFunc(GL_LESS);
314 glDepthFunc(GL_ALWAYS);
317 glDepthFunc(GL_EQUAL);
321 glDepthFunc(GL_LESS);
323 glDepthFunc(GL_GREATER);
326 glDepthFunc(GL_NEVER);
328 case GR_CMP_NOTEQUAL:
329 glDepthFunc(GL_NOTEQUAL);
333 display_warning("unknown depth buffer function : %x", function);
337 FX_ENTRY void FX_CALL
338 grDepthMask( FxBool mask )
340 LOG("grDepthMask(%d)\r\n", mask);
344 float biasFactor = 0;
345 void FindBestDepthBias()
348 /* int hardwareType = Android_JNI_GetHardwareType();
349 Android_JNI_GetPolygonOffset(hardwareType, 1, &polygonOffsetFactor, &polygonOffsetUnits);*/
350 // glPolygonOffset(0.2f, 0.2f);
351 polygonOffsetFactor=0.2f;
352 polygonOffsetUnits=0.2f;
354 float f, bestz = 0.25f;
356 if (biasFactor) return;
357 biasFactor = 64.0f; // default value
358 glPushAttrib(GL_ALL_ATTRIB_BITS);
359 glEnable(GL_DEPTH_TEST);
360 glDepthFunc(GL_ALWAYS);
361 glEnable(GL_POLYGON_OFFSET_FILL);
362 glDrawBuffer(GL_BACK);
363 glReadBuffer(GL_BACK);
365 glDisable(GL_ALPHA_TEST);
366 glColor4ub(255,255,255,255);
367 glDepthMask(GL_TRUE);
368 for (x=0, f=1.0f; f<=65536.0f; x+=4, f*=2.0f) {
370 glPolygonOffset(0, f);
371 glBegin(GL_TRIANGLE_STRIP);
372 glVertex3f(float(x+4 - widtho)/(width/2), float(0 - heighto)/(height/2), 0.5);
373 glVertex3f(float(x - widtho)/(width/2), float(0 - heighto)/(height/2), 0.5);
374 glVertex3f(float(x+4 - widtho)/(width/2), float(4 - heighto)/(height/2), 0.5);
375 glVertex3f(float(x - widtho)/(width/2), float(4 - heighto)/(height/2), 0.5);
377 glReadPixels(x+2, 2+viewport_offset, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
380 if (z > 0.01f) continue;
385 //printf("f %g z %g\n", f, z);
387 //printf(" --> bias factor %g\n", biasFactor);
392 FX_ENTRY void FX_CALL
393 grDepthBiasLevel( FxI32 level )
395 LOG("grDepthBiasLevel(%d)\r\n", level);
399 glPolygonOffset(polygonOffsetFactor, polygonOffsetUnits);
401 glPolygonOffset(1.0f, -(float)level*polygonOffsetUnits);
403 glPolygonOffset(0, (float)level*3.0f);*/
406 glPolygonOffset(1.0f, -(float)level*zscale/255.0f);
408 glPolygonOffset(0, (float)level*biasFactor);
410 glEnable(GL_POLYGON_OFFSET_FILL);
414 glPolygonOffset(0,0);
415 glDisable(GL_POLYGON_OFFSET_FILL);
421 FX_ENTRY void FX_CALL
422 grDrawTriangle( const void *a, const void *b, const void *c )
424 LOG("grDrawTriangle()\r\n\t");
426 if(nvidia_viewport_hack && !render_to_texture)
428 glViewport(0, viewport_offset, viewport_width, viewport_height);
429 nvidia_viewport_hack = 0;
434 if(need_to_compile) compile_shader();
436 if(vertex_buffer_count + 3 > VERTEX_BUFFER_SIZE)
440 vertex_draw_mode = GL_TRIANGLES;
441 memcpy(&vertex_buffer[vertex_buffer_count],a,VERTEX_SIZE);
442 memcpy(&vertex_buffer[vertex_buffer_count+1],b,VERTEX_SIZE);
443 memcpy(&vertex_buffer[vertex_buffer_count+2],c,VERTEX_SIZE);
444 vertex_buffer_count += 3;
447 FX_ENTRY void FX_CALL
448 grDrawPoint( const void *pt )
451 float *x = (float*)pt + xy_off/sizeof(float);
452 float *y = (float*)pt + xy_off/sizeof(float) + 1;
453 float *z = (float*)pt + z_off/sizeof(float);
454 float *q = (float*)pt + q_off/sizeof(float);
455 unsigned char *pargb = (unsigned char*)pt + pargb_off;
456 float *s0 = (float*)pt + st0_off/sizeof(float);
457 float *t0 = (float*)pt + st0_off/sizeof(float) + 1;
458 float *s1 = (float*)pt + st1_off/sizeof(float);
459 float *t1 = (float*)pt + st1_off/sizeof(float) + 1;
460 float *fog = (float*)pt + fog_ext_off/sizeof(float);
461 LOG("grDrawPoint()\r\n");
463 if(nvidia_viewport_hack && !render_to_texture)
465 glViewport(0, viewport_offset, viewport_width, viewport_height);
466 nvidia_viewport_hack = 0;
471 if(need_to_compile) compile_shader();
475 if (nbTextureUnits > 2)
478 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, *s0 / *q / (float)tex1_width,
479 ytex(0, *t0 / *q / (float)tex1_height));
481 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, *s1 / *q / (float)tex0_width,
482 ytex(1, *t1 / *q / (float)tex0_height));
487 glTexCoord2f(*s0 / *q / (float)tex0_width,
488 ytex(0, *t0 / *q / (float)tex0_height));
491 glColor4f(pargb[2]/255.0f, pargb[1]/255.0f, pargb[0]/255.0f, pargb[3]/255.0f);
492 if (fog_enabled && fog_coord_support)
494 if(!fog_ext_en || fog_enabled != 2)
495 glSecondaryColor3f((1.0f / *q) / 255.0f, 0.0f, 0.0f);
497 glSecondaryColor3f((1.0f / *fog) / 255.0f, 0.0f, 0.0f);
499 glVertex4f((*x - (float)widtho) / (float)(width/2) / *q,
500 -(*y - (float)heighto) / (float)(height/2) / *q, ZCALC(*z ,*q), 1.0f / *q);
506 FX_ENTRY void FX_CALL
507 grDrawLine( const void *a, const void *b )
510 float *a_x = (float*)a + xy_off/sizeof(float);
511 float *a_y = (float*)a + xy_off/sizeof(float) + 1;
512 float *a_z = (float*)a + z_off/sizeof(float);
513 float *a_q = (float*)a + q_off/sizeof(float);
514 unsigned char *a_pargb = (unsigned char*)a + pargb_off;
515 float *a_s0 = (float*)a + st0_off/sizeof(float);
516 float *a_t0 = (float*)a + st0_off/sizeof(float) + 1;
517 float *a_s1 = (float*)a + st1_off/sizeof(float);
518 float *a_t1 = (float*)a + st1_off/sizeof(float) + 1;
519 float *a_fog = (float*)a + fog_ext_off/sizeof(float);
521 float *b_x = (float*)b + xy_off/sizeof(float);
522 float *b_y = (float*)b + xy_off/sizeof(float) + 1;
523 float *b_z = (float*)b + z_off/sizeof(float);
524 float *b_q = (float*)b + q_off/sizeof(float);
525 unsigned char *b_pargb = (unsigned char*)b + pargb_off;
526 float *b_s0 = (float*)b + st0_off/sizeof(float);
527 float *b_t0 = (float*)b + st0_off/sizeof(float) + 1;
528 float *b_s1 = (float*)b + st1_off/sizeof(float);
529 float *b_t1 = (float*)b + st1_off/sizeof(float) + 1;
530 float *b_fog = (float*)b + fog_ext_off/sizeof(float);
531 LOG("grDrawLine()\r\n");
533 if(nvidia_viewport_hack && !render_to_texture)
535 glViewport(0, viewport_offset, viewport_width, viewport_height);
536 nvidia_viewport_hack = 0;
541 if(need_to_compile) compile_shader();
545 if (nbTextureUnits > 2)
548 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, *a_s0 / *a_q / (float)tex1_width, ytex(0, *a_t0 / *a_q / (float)tex1_height));
550 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, *a_s1 / *a_q / (float)tex0_width, ytex(1, *a_t1 / *a_q / (float)tex0_height));
555 glTexCoord2f(*a_s0 / *a_q / (float)tex0_width, ytex(0, *a_t0 / *a_q / (float)tex0_height));
558 glColor4f(a_pargb[2]/255.0f, a_pargb[1]/255.0f, a_pargb[0]/255.0f, a_pargb[3]/255.0f);
559 if (fog_enabled && fog_coord_support)
561 if(!fog_ext_en || fog_enabled != 2)
562 glSecondaryColor3f((1.0f / *a_q) / 255.0f, 0.0f, 0.0f);
564 glSecondaryColor3f((1.0f / *a_fog) / 255.0f, 0.0f, 0.0f);
566 glVertex4f((*a_x - (float)widtho) / (float)(width/2) / *a_q,
567 -(*a_y - (float)heighto) / (float)(height/2) / *a_q, ZCALC(*a_z, *a_q), 1.0f / *a_q);
569 if (nbTextureUnits > 2)
572 glMultiTexCoord2fARB(GL_TEXTURE1_ARB, *b_s0 / *b_q / (float)tex1_width,
573 ytex(0, *b_t0 / *b_q / (float)tex1_height));
575 glMultiTexCoord2fARB(GL_TEXTURE0_ARB, *b_s1 / *b_q / (float)tex0_width,
576 ytex(1, *b_t1 / *b_q / (float)tex0_height));
581 glTexCoord2f(*b_s0 / *b_q / (float)tex0_width,
582 ytex(0, *b_t0 / *b_q / (float)tex0_height));
585 glColor4f(b_pargb[2]/255.0f, b_pargb[1]/255.0f, b_pargb[0]/255.0f, b_pargb[3]/255.0f);
586 if (fog_enabled && fog_coord_support)
588 if(!fog_ext_en || fog_enabled != 2)
589 glSecondaryColor3f((1.0f / *b_q) / 255.0f, 0.0f, 0.0f);
591 glSecondaryColor3f((1.0f / *b_fog) / 255.0f, 0.0f, 0.0f);
593 glVertex4f((*b_x - (float)widtho) / (float)(width/2) / *b_q,
594 -(*b_y - (float)heighto) / (float)(height/2) / *b_q, ZCALC(*b_z, *b_q), 1.0f / *b_q);
600 FX_ENTRY void FX_CALL
601 grDrawVertexArray(FxU32 mode, FxU32 Count, void *pointers2)
603 void **pointers = (void**)pointers2;
604 LOG("grDrawVertexArray(%d,%d)\r\n", mode, Count);
606 if(nvidia_viewport_hack && !render_to_texture)
608 glViewport(0, viewport_offset, viewport_width, viewport_height);
609 nvidia_viewport_hack = 0;
614 if(need_to_compile) compile_shader();
616 if(mode != GR_TRIANGLE_FAN)
618 display_warning("grDrawVertexArray : unknown mode : %x", mode);
622 vbo_buffer(GL_TRIANGLE_FAN,0,Count,pointers[0]);
625 FX_ENTRY void FX_CALL
626 grDrawVertexArrayContiguous(FxU32 mode, FxU32 Count, void *pointers, FxU32 stride)
628 LOG("grDrawVertexArrayContiguous(%d,%d,%d)\r\n", mode, Count, stride);
630 if(nvidia_viewport_hack && !render_to_texture)
632 glViewport(0, viewport_offset, viewport_width, viewport_height);
633 nvidia_viewport_hack = 0;
638 LOGINFO("Incompatible stride\n");
643 if(need_to_compile) compile_shader();
649 case GR_TRIANGLE_STRIP:
650 vbo_buffer(GL_TRIANGLE_STRIP,0,Count,pointers);
652 case GR_TRIANGLE_FAN:
653 vbo_buffer(GL_TRIANGLE_FAN,0,Count,pointers);
656 display_warning("grDrawVertexArrayContiguous : unknown mode : %x", mode);