2021-03-02 02:13:17 +00:00
|
|
|
// Project64 - A Nintendo 64 emulator
|
|
|
|
// http://www.pj64-emu.com/
|
|
|
|
// Copyright(C) 2001-2021 Project64
|
|
|
|
// Copyright(C) 2003-2009 Sergey 'Gonetz' Lipski
|
|
|
|
// Copyright(C) 2002 Dave2001
|
|
|
|
// GNU/GPLv2 licensed: https://gnu.org/licenses/gpl-2.0.html
|
2015-10-09 04:44:33 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif // _WIN32
|
2015-10-09 04:53:16 +00:00
|
|
|
#include "glitchmain.h"
|
2017-04-26 09:40:23 +00:00
|
|
|
#include <Project64-video/rdp.h>
|
|
|
|
#include <Project64-video/trace.h>
|
2015-10-09 04:44:33 +00:00
|
|
|
|
|
|
|
#define Z_MAX (65536.0f)
|
2017-08-16 22:18:19 +00:00
|
|
|
#define VERTEX_SIZE sizeof(gfxVERTEX) //Size of vertex struct
|
2015-10-09 04:44:33 +00:00
|
|
|
|
|
|
|
int w_buffer_mode;
|
|
|
|
int inverted_culling;
|
2017-08-02 21:48:55 +00:00
|
|
|
gfxCullMode_t culling_mode;
|
2015-10-09 04:44:33 +00:00
|
|
|
|
|
|
|
#define VERTEX_BUFFER_SIZE 1500 //Max amount of vertices to buffer, this seems large enough.
|
2017-08-16 22:18:19 +00:00
|
|
|
static gfxVERTEX vertex_buffer[VERTEX_BUFFER_SIZE];
|
2015-10-09 04:44:33 +00:00
|
|
|
static int vertex_buffer_count = 0;
|
|
|
|
static GLenum vertex_draw_mode;
|
|
|
|
static bool vertex_buffer_enabled = false;
|
|
|
|
|
|
|
|
void vbo_draw()
|
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
if (vertex_buffer_count)
|
|
|
|
{
|
|
|
|
WriteTrace(TraceGlide64, TraceDebug, "vertex_draw_mode: %d vertex_buffer_count: %d", vertex_draw_mode, vertex_buffer_count);
|
|
|
|
glDrawArrays(vertex_draw_mode, 0, vertex_buffer_count);
|
|
|
|
vertex_buffer_count = 0;
|
|
|
|
WriteTrace(TraceGlide64, TraceDebug, "done (glGetError() = %X)", glGetError());
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Buffer vertices instead of glDrawArrays(...)
|
2016-03-10 18:29:41 +00:00
|
|
|
void vbo_buffer(GLenum mode, GLint first, GLsizei count, void* pointers)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
if ((count != 3 && mode != GL_TRIANGLES) || vertex_buffer_count + count > VERTEX_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
vbo_draw();
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
memcpy(&vertex_buffer[vertex_buffer_count], pointers, count * VERTEX_SIZE);
|
|
|
|
vertex_buffer_count += count;
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
if (count == 3 || mode == GL_TRIANGLES)
|
|
|
|
{
|
|
|
|
vertex_draw_mode = GL_TRIANGLES;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vertex_draw_mode = mode;
|
|
|
|
vbo_draw(); //Triangle fans and strips can't be joined as easily, just draw them straight away.
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void vbo_enable()
|
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
if (vertex_buffer_enabled)
|
|
|
|
return;
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
vertex_buffer_enabled = true;
|
|
|
|
glEnableVertexAttribArray(POSITION_ATTR);
|
2016-12-25 22:26:36 +00:00
|
|
|
glVertexAttribPointer(POSITION_ATTR, 4, GL_FLOAT, GL_FALSE, VERTEX_SIZE, &vertex_buffer[0].x); //Position
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
glEnableVertexAttribArray(COLOUR_ATTR);
|
2016-12-25 22:26:36 +00:00
|
|
|
glVertexAttribPointer(COLOUR_ATTR, 4, GL_UNSIGNED_BYTE, GL_TRUE, VERTEX_SIZE, &vertex_buffer[0].b); //Colour
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
glEnableVertexAttribArray(TEXCOORD_0_ATTR);
|
|
|
|
glVertexAttribPointer(TEXCOORD_0_ATTR, 2, GL_FLOAT, false, VERTEX_SIZE, &vertex_buffer[0].coord[2]); //Tex0
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
glEnableVertexAttribArray(TEXCOORD_1_ATTR);
|
|
|
|
glVertexAttribPointer(TEXCOORD_1_ATTR, 2, GL_FLOAT, false, VERTEX_SIZE, &vertex_buffer[0].coord[0]); //Tex1
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
glEnableVertexAttribArray(FOG_ATTR);
|
|
|
|
glVertexAttribPointer(FOG_ATTR, 1, GL_FLOAT, false, VERTEX_SIZE, &vertex_buffer[0].f); //Fog
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void vbo_disable()
|
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
vbo_draw();
|
|
|
|
vertex_buffer_enabled = false;
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline float ZCALC(const float & z, const float & q) {
|
2017-08-11 07:07:23 +00:00
|
|
|
float res = ((z) / Z_MAX) / (q);
|
2016-03-10 18:29:41 +00:00
|
|
|
return res;
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
#define zclamp (1.0f-1.0f/zscale)
|
|
|
|
static inline void zclamp_glVertex4f(float a, float b, float c, float d)
|
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
if (c<zclamp) c = zclamp;
|
|
|
|
glVertex4f(a,b,c,d);
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
#define glVertex4f(a,b,c,d) zclamp_glVertex4f(a,b,c,d)
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline float ytex(int tmu, float y) {
|
2016-03-10 18:29:41 +00:00
|
|
|
if (invtex[tmu])
|
|
|
|
return invtex[tmu] - y;
|
|
|
|
else
|
|
|
|
return y;
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_geometry()
|
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
w_buffer_mode = 0;
|
|
|
|
inverted_culling = 0;
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-02 21:48:55 +00:00
|
|
|
void gfxCullMode(gfxCullMode_t mode)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceDebug, "mode: %d", mode);
|
|
|
|
static int oldmode = -1, oldinv = -1;
|
|
|
|
culling_mode = mode;
|
|
|
|
if (inverted_culling == oldinv && oldmode == mode)
|
|
|
|
return;
|
|
|
|
oldmode = mode;
|
|
|
|
oldinv = inverted_culling;
|
|
|
|
switch (mode)
|
|
|
|
{
|
2017-08-02 21:48:55 +00:00
|
|
|
case GFX_CULL_DISABLE:
|
2016-03-10 18:29:41 +00:00
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
break;
|
2017-08-02 21:48:55 +00:00
|
|
|
case GFX_CULL_NEGATIVE:
|
2016-03-10 18:29:41 +00:00
|
|
|
if (!inverted_culling)
|
|
|
|
glCullFace(GL_FRONT);
|
|
|
|
else
|
|
|
|
glCullFace(GL_BACK);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
break;
|
2017-08-02 21:48:55 +00:00
|
|
|
case GFX_CULL_POSITIVE:
|
2016-03-10 18:29:41 +00:00
|
|
|
if (!inverted_culling)
|
|
|
|
glCullFace(GL_BACK);
|
|
|
|
else
|
|
|
|
glCullFace(GL_FRONT);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WriteTrace(TraceGlitch, TraceWarning, "unknown cull mode : %x", mode);
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Depth buffer
|
|
|
|
|
2017-08-02 22:02:52 +00:00
|
|
|
void gfxDepthBufferMode(gfxDepthBufferMode_t mode)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceDebug, "mode: %d", mode);
|
|
|
|
switch (mode)
|
|
|
|
{
|
2017-08-02 22:02:52 +00:00
|
|
|
case GFX_DEPTHBUFFER_DISABLE:
|
2016-03-10 18:29:41 +00:00
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
w_buffer_mode = 0;
|
|
|
|
return;
|
2017-08-02 22:02:52 +00:00
|
|
|
case GFX_DEPTHBUFFER_WBUFFER:
|
|
|
|
case GFX_DEPTHBUFFER_WBUFFER_COMPARE_TO_BIAS:
|
2016-03-10 18:29:41 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
w_buffer_mode = 1;
|
|
|
|
break;
|
2017-08-02 22:02:52 +00:00
|
|
|
case GFX_DEPTHBUFFER_ZBUFFER:
|
|
|
|
case GFX_DEPTHBUFFER_ZBUFFER_COMPARE_TO_BIAS:
|
2016-03-10 18:29:41 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
w_buffer_mode = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WriteTrace(TraceGlitch, TraceWarning, "unknown depth buffer mode : %x", mode);
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 11:05:29 +00:00
|
|
|
void gfxDepthBufferFunction(gfxCmpFnc_t function)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceDebug, "function: %d", function);
|
|
|
|
switch (function)
|
|
|
|
{
|
2017-07-31 11:05:29 +00:00
|
|
|
case GFX_CMP_GEQUAL:
|
2016-03-10 18:29:41 +00:00
|
|
|
if (w_buffer_mode)
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
else
|
|
|
|
glDepthFunc(GL_GEQUAL);
|
|
|
|
break;
|
2017-07-31 11:05:29 +00:00
|
|
|
case GFX_CMP_LEQUAL:
|
2016-03-10 18:29:41 +00:00
|
|
|
if (w_buffer_mode)
|
|
|
|
glDepthFunc(GL_GEQUAL);
|
|
|
|
else
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
break;
|
2017-07-31 11:05:29 +00:00
|
|
|
case GFX_CMP_LESS:
|
2016-03-10 18:29:41 +00:00
|
|
|
if (w_buffer_mode)
|
|
|
|
glDepthFunc(GL_GREATER);
|
|
|
|
else
|
|
|
|
glDepthFunc(GL_LESS);
|
|
|
|
break;
|
2017-07-31 11:05:29 +00:00
|
|
|
case GFX_CMP_ALWAYS:
|
2016-03-10 18:29:41 +00:00
|
|
|
glDepthFunc(GL_ALWAYS);
|
|
|
|
break;
|
2017-07-31 11:05:29 +00:00
|
|
|
case GFX_CMP_EQUAL:
|
2016-03-10 18:29:41 +00:00
|
|
|
glDepthFunc(GL_EQUAL);
|
|
|
|
break;
|
2017-07-31 11:05:29 +00:00
|
|
|
case GFX_CMP_GREATER:
|
2016-03-10 18:29:41 +00:00
|
|
|
if (w_buffer_mode)
|
|
|
|
glDepthFunc(GL_LESS);
|
|
|
|
else
|
|
|
|
glDepthFunc(GL_GREATER);
|
|
|
|
break;
|
2017-07-31 11:05:29 +00:00
|
|
|
case GFX_CMP_NEVER:
|
2016-03-10 18:29:41 +00:00
|
|
|
glDepthFunc(GL_NEVER);
|
|
|
|
break;
|
2017-07-31 11:05:29 +00:00
|
|
|
case GFX_CMP_NOTEQUAL:
|
2016-03-10 18:29:41 +00:00
|
|
|
glDepthFunc(GL_NOTEQUAL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
WriteTrace(TraceGlitch, TraceWarning, "unknown depth buffer function : %x", function);
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-07-22 23:03:22 +00:00
|
|
|
void gfxDepthMask(bool mask)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceDebug, "mask: %d", mask);
|
|
|
|
glDepthMask(mask);
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
2017-05-24 22:51:36 +00:00
|
|
|
|
2015-10-09 04:44:33 +00:00
|
|
|
float biasFactor = 0;
|
2017-08-08 21:32:10 +00:00
|
|
|
void gfxDepthBiasLevel(int32_t level)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceDebug, "level: %d", level);
|
|
|
|
if (level)
|
|
|
|
{
|
|
|
|
if (w_buffer_mode)
|
|
|
|
{
|
|
|
|
glPolygonOffset(1.0f, -(float)level*zscale / 255.0f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glPolygonOffset(0, (float)level*biasFactor);
|
|
|
|
}
|
|
|
|
glEnable(GL_POLYGON_OFFSET_FILL);
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
else
|
2016-03-10 18:29:41 +00:00
|
|
|
{
|
|
|
|
glPolygonOffset(0, 0);
|
|
|
|
glDisable(GL_POLYGON_OFFSET_FILL);
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw
|
2017-08-16 22:18:19 +00:00
|
|
|
void gfxDrawTriangle(const gfxVERTEX *a, const gfxVERTEX *b, const gfxVERTEX *c)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceDebug, "start");
|
2017-01-04 06:26:25 +00:00
|
|
|
vbo_enable();
|
2016-03-10 18:29:41 +00:00
|
|
|
if (nvidia_viewport_hack && !render_to_texture)
|
|
|
|
{
|
2017-03-13 09:34:51 +00:00
|
|
|
glViewport(0, g_viewport_offset, viewport_width, viewport_height);
|
2016-03-10 18:29:41 +00:00
|
|
|
nvidia_viewport_hack = 0;
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
reloadTexture();
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
if (need_to_compile) compile_shader();
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
if (vertex_buffer_count + 3 > VERTEX_BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
vbo_draw();
|
|
|
|
}
|
|
|
|
vertex_draw_mode = GL_TRIANGLES;
|
|
|
|
memcpy(&vertex_buffer[vertex_buffer_count], a, VERTEX_SIZE);
|
|
|
|
memcpy(&vertex_buffer[vertex_buffer_count + 1], b, VERTEX_SIZE);
|
|
|
|
memcpy(&vertex_buffer[vertex_buffer_count + 2], c, VERTEX_SIZE);
|
|
|
|
vertex_buffer_count += 3;
|
|
|
|
|
|
|
|
WriteTrace(TraceGlitch, TraceDebug, "Done");
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 22:18:19 +00:00
|
|
|
void gfxDrawLine(const gfxVERTEX *a, const gfxVERTEX *b)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-10 07:37:41 +00:00
|
|
|
void gfxDrawVertexArray(gfxDrawMode_t mode, uint32_t Count, void *pointers2)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
2016-03-10 18:29:41 +00:00
|
|
|
void **pointers = (void**)pointers2;
|
2017-05-25 08:05:53 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceDebug, "gfxDrawVertexArray(%d,%d)\r\n", mode, Count);
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
if (nvidia_viewport_hack && !render_to_texture)
|
|
|
|
{
|
2017-03-13 09:34:51 +00:00
|
|
|
glViewport(0, g_viewport_offset, viewport_width, viewport_height);
|
2016-03-10 18:29:41 +00:00
|
|
|
nvidia_viewport_hack = 0;
|
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
reloadTexture();
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
if (need_to_compile) compile_shader();
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2017-08-10 07:37:41 +00:00
|
|
|
if (mode != GFX_TRIANGLE_FAN)
|
2016-03-10 18:29:41 +00:00
|
|
|
{
|
2017-05-25 08:05:53 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceWarning, "gfxDrawVertexArray : unknown mode : %x", mode);
|
2016-03-10 18:29:41 +00:00
|
|
|
}
|
2015-10-09 04:44:33 +00:00
|
|
|
|
2016-03-10 18:29:41 +00:00
|
|
|
vbo_enable();
|
|
|
|
vbo_buffer(GL_TRIANGLE_FAN, 0, Count, pointers[0]);
|
2015-10-09 04:44:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 07:37:41 +00:00
|
|
|
void gfxDrawVertexArrayContiguous(gfxDrawMode_t mode, uint32_t Count, void *pointers, uint32_t stride)
|
2015-10-09 04:44:33 +00:00
|
|
|
{
|
2017-05-25 08:10:13 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceDebug, "gfxDrawVertexArrayContiguous(%d,%d,%d)\r\n", mode, Count, stride);
|
2016-03-10 18:29:41 +00:00
|
|
|
|
|
|
|
if (nvidia_viewport_hack && !render_to_texture)
|
|
|
|
{
|
2017-03-13 09:34:51 +00:00
|
|
|
glViewport(0, g_viewport_offset, viewport_width, viewport_height);
|
2016-03-10 18:29:41 +00:00
|
|
|
nvidia_viewport_hack = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stride != 156)
|
|
|
|
{
|
|
|
|
//LOGINFO("Incompatible stride\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
reloadTexture();
|
|
|
|
|
|
|
|
if (need_to_compile) compile_shader();
|
|
|
|
|
|
|
|
vbo_enable();
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
{
|
2017-08-10 07:37:41 +00:00
|
|
|
case GFX_TRIANGLE_STRIP:
|
2016-03-10 18:29:41 +00:00
|
|
|
vbo_buffer(GL_TRIANGLE_STRIP, 0, Count, pointers);
|
|
|
|
break;
|
2017-08-10 07:37:41 +00:00
|
|
|
case GFX_TRIANGLE_FAN:
|
2016-03-10 18:29:41 +00:00
|
|
|
vbo_buffer(GL_TRIANGLE_FAN, 0, Count, pointers);
|
|
|
|
break;
|
|
|
|
default:
|
2017-05-25 08:10:13 +00:00
|
|
|
WriteTrace(TraceGlitch, TraceWarning, "gfxDrawVertexArrayContiguous : unknown mode : %x", mode);
|
2016-03-10 18:29:41 +00:00
|
|
|
}
|
|
|
|
}
|