/*************************************************************************** * Copyright (C) 2016 by iCatButler * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * ***************************************************************************/ /************************************************************************** * pgxp_gpu.c * PGXP - Parallel/Precision Geometry Xform Pipeline * * Created on: 25 Mar 2016 * Author: iCatButler ***************************************************************************/ #include "pgxp_gpu.h" #include "stdafx.h" #include "externals.h" #include typedef struct { float x; float y; float z; unsigned int valid; unsigned int count; unsigned int value; } PGXP_vertex; const unsigned int primStrideTable[] = { 1, 2, 1, 2, 2, 3, 2, 3, 0 }; const unsigned int primCountTable[] = { 3, 3, 4, 4, 3, 3, 4, 4, 0 }; PGXP_vertex* PGXP_Mem = NULL; // pointer to parallel memory unsigned int currentAddr = 0; // address of current DMA unsigned int numVertices = 0; // iCB: Used for glVertex3fv fix unsigned int vertexIdx = 0; // Set current DMA address and pointer to parallel memory void CALLBACK GPUpgxpMemory(unsigned int addr, unsigned char* pVRAM) { if (pVRAM) PGXP_Mem = (PGXP_vertex*)(pVRAM); currentAddr = addr; } // Set current DMA address void PGXP_SetAddress(unsigned int addr) { currentAddr = addr; } void PGXP_SetMatrix(float left, float right, float bottom, float top, float zNear, float zFar) { GLfloat m[16]; for (unsigned int i = 0; i < 16; ++i) m[i] = 0.f; //if ((right-left) != 0) //{ // m[0] = 2 / (right - left); // m[12] = -((right + left) / (right - left)); //} //if ((top-bottom) != 0) //{ // m[5] = 2 / (top - bottom); // m[13] = -((top + bottom) / (top - bottom)); //} //m[10] = -2 / (zFar - zNear); //m[14] = -((zFar + zNear) / (zFar - zNear)); //m[15] = 1; if ((right-left) != 0) { m[0] = 2 / (right - left); m[8] = -((right + left) / (right - left)); } if ((top-bottom) != 0) { m[5] = 2 / (top - bottom); m[9] = -((top + bottom) / (top - bottom)); } m[10] = -2 / (zFar - zNear); m[14] = -((zFar + zNear) / (zFar - zNear)); m[11] = 1; glLoadMatrixf(m); //glOrtho(left, right, bottom, top, zNear, zFar); } // Wrap glVertex3fv/glVertex4fv void PGXP_glVertexfv(GLfloat* pV) { // If there are PGXP vertices expected if (1)//(vertexIdx < numVertices) { float temp[4]; memcpy(temp, pV, sizeof(float) * 4); //pre-multiply each element by w (to negate perspective divide) for (unsigned int i = 0; i < 3; i++) temp[i] *= temp[3]; //pass complete vertex to OpenGL glVertex4fv(temp); vertexIdx++; //pV[3] = 1.f; } else { glVertex3fv(pV); } } // Get parallel vertex values int PGXP_GetVertices(unsigned int* addr, void* pOutput, int xOffs, int yOffs) { unsigned int primCmd = ((*addr >> 24) & 0xff); // primitive command unsigned int primIdx = min((primCmd - 0x20) >> 2, 8); // index to primitive lookup OGLVertex* pVertex = (OGLVertex*)pOutput; // pointer to output vertices unsigned int stride = primStrideTable[primIdx]; // stride between vertices unsigned int count = primCountTable[primIdx]; // number of vertices PGXP_vertex* primStart = NULL; // pointer to first vertex char invalidVert = 0; // Number of vertices without valid PGXP values float w = 0; // W coordinate of transformed vertex if (PGXP_Mem == NULL) return 0; // Reset vertex count numVertices = count; vertexIdx = 0; // Offset to start of primitive primStart = &PGXP_Mem[currentAddr + 1]; // Find any invalid vertices for (unsigned i = 0; i < count; ++i) { if(!primStart[stride * i].valid) invalidVert++; } for (unsigned i = 0; i < count; ++i) { w = primStart[stride * i].z; // If there are any invalid vertices set all w values to 1 // iCB: Could use plane equation to find w for single invalid vertex in a quad if (invalidVert > 0) w = 1; if (primStart[stride * i].valid) { pVertex[i].x = (primStart[stride * i].x + xOffs); pVertex[i].y = (primStart[stride * i].y + yOffs); pVertex[i].z = 0.95f; pVertex[i].w = w; pVertex[i].PGXP_flag = 1; } else pVertex[i].PGXP_flag = 2; } return 1; } ///////////////////////////////// //// Visual Debugging Functions ///////////////////////////////// unsigned int PGXP_vDebug = 0; const char blue[4] = { 0, 0, 255, 255 }; const char red[4] = { 255, 0, 0, 255 }; const char black[4] = { 0, 0, 0, 255 }; const char yellow[4] = { 255, 255, 0, 255 }; void CALLBACK GPUtoggleDebug(void) { if (PGXP_vDebug) PGXP_vDebug = 0; else PGXP_vDebug = 1; } const char* PGXP_colour(unsigned int flag) { switch (flag) { case 0: return yellow; case 1: return blue; case 2: return red; default: return black; } } void PGXP_DrawDebugTriQuad(OGLVertex* vertex1, OGLVertex* vertex2, OGLVertex* vertex3, OGLVertex* vertex4) { GLboolean bTexture = glIsEnabled(GL_TEXTURE_2D); GLfloat fColour[4]; glGetFloatv(GL_CURRENT_COLOR, fColour); glDisable(GL_TEXTURE_2D); glBegin(GL_TRIANGLE_STRIP); glColor4ubv(PGXP_colour(vertex1->PGXP_flag)); PGXP_glVertexfv(&vertex1->x); glColor4ubv(PGXP_colour(vertex2->PGXP_flag)); PGXP_glVertexfv(&vertex2->x); glColor4ubv(PGXP_colour(vertex3->PGXP_flag)); PGXP_glVertexfv(&vertex3->x); glColor4ubv(PGXP_colour(vertex4->PGXP_flag)); PGXP_glVertexfv(&vertex4->x); glEnd(); glPolygonMode(GL_FRONT, GL_LINE); glPolygonMode(GL_BACK, GL_LINE); glBegin(GL_TRIANGLE_STRIP); glColor4ubv(black); PGXP_glVertexfv(&vertex1->x); PGXP_glVertexfv(&vertex2->x); PGXP_glVertexfv(&vertex3->x); PGXP_glVertexfv(&vertex4->x); glColor4fv(fColour); glEnd(); glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_FILL); if(bTexture == GL_TRUE) glEnable(GL_TEXTURE_2D); } void PGXP_DrawDebugTri(OGLVertex* vertex1, OGLVertex* vertex2, OGLVertex* vertex3) { GLboolean bTexture = glIsEnabled(GL_TEXTURE_2D); GLfloat fColour[4]; glGetFloatv(GL_CURRENT_COLOR, fColour); glDisable(GL_TEXTURE_2D); glBegin(GL_TRIANGLES); glColor4ubv(PGXP_colour(vertex1->PGXP_flag)); PGXP_glVertexfv(&vertex1->x); glColor4ubv(PGXP_colour(vertex2->PGXP_flag)); PGXP_glVertexfv(&vertex2->x); glColor4ubv(PGXP_colour(vertex3->PGXP_flag)); PGXP_glVertexfv(&vertex3->x); glEnd(); glPolygonMode(GL_FRONT, GL_LINE); glPolygonMode(GL_BACK, GL_LINE); glBegin(GL_TRIANGLE_STRIP); glColor4ubv(black); PGXP_glVertexfv(&vertex1->x); PGXP_glVertexfv(&vertex2->x); PGXP_glVertexfv(&vertex3->x); glColor4fv(fColour); glEnd(); glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_FILL); if (bTexture == GL_TRUE) glEnable(GL_TEXTURE_2D); } void PGXP_DrawDebugQuad(OGLVertex* vertex1, OGLVertex* vertex2, OGLVertex* vertex3, OGLVertex* vertex4) { GLboolean bTexture = glIsEnabled(GL_TEXTURE_2D); GLfloat fColour[4]; glGetFloatv(GL_CURRENT_COLOR, fColour); glDisable(GL_TEXTURE_2D); glBegin(GL_QUADS); glColor4ubv(PGXP_colour(vertex1->PGXP_flag)); PGXP_glVertexfv(&vertex1->x); glColor4ubv(PGXP_colour(vertex2->PGXP_flag)); PGXP_glVertexfv(&vertex2->x); glColor4ubv(PGXP_colour(vertex3->PGXP_flag)); PGXP_glVertexfv(&vertex3->x); glColor4ubv(PGXP_colour(vertex4->PGXP_flag)); PGXP_glVertexfv(&vertex4->x); glEnd(); glPolygonMode(GL_FRONT, GL_LINE); glPolygonMode(GL_BACK, GL_LINE); glBegin(GL_TRIANGLE_STRIP); glColor4ubv(black); PGXP_glVertexfv(&vertex1->x); PGXP_glVertexfv(&vertex2->x); PGXP_glVertexfv(&vertex3->x); PGXP_glVertexfv(&vertex4->x); glColor4fv(fColour); glEnd(); glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_FILL); if (bTexture == GL_TRUE) glEnable(GL_TEXTURE_2D); }