GLdc/GL/draw.c

1573 lines
48 KiB
C
Raw Normal View History

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
2018-08-27 20:35:07 +00:00
#include <math.h>
#include <assert.h>
#include <dc/vec3f.h>
2018-05-05 19:38:55 +00:00
#include "../include/gl.h"
#include "../include/glext.h"
2018-05-05 19:38:55 +00:00
#include "private.h"
#include "profiler.h"
2018-05-05 19:38:55 +00:00
static AttribPointer VERTEX_POINTER;
static AttribPointer UV_POINTER;
static AttribPointer ST_POINTER;
static AttribPointer NORMAL_POINTER;
static AttribPointer DIFFUSE_POINTER;
static GLuint ENABLED_VERTEX_ATTRIBUTES = 0;
static GLubyte ACTIVE_CLIENT_TEXTURE = 0;
static GLboolean FAST_PATH_ENABLED = GL_FALSE;
#define ITERATE(count) \
2019-03-28 13:07:53 +00:00
GLuint i = count; \
while(i--)
void _glInitAttributePointers() {
TRACE();
2018-05-05 19:38:55 +00:00
VERTEX_POINTER.ptr = NULL;
VERTEX_POINTER.stride = 0;
VERTEX_POINTER.type = GL_FLOAT;
VERTEX_POINTER.size = 4;
2018-05-19 08:17:24 +00:00
DIFFUSE_POINTER.ptr = NULL;
DIFFUSE_POINTER.stride = 0;
DIFFUSE_POINTER.type = GL_FLOAT;
DIFFUSE_POINTER.size = 4;
UV_POINTER.ptr = NULL;
UV_POINTER.stride = 0;
UV_POINTER.type = GL_FLOAT;
UV_POINTER.size = 4;
ST_POINTER.ptr = NULL;
ST_POINTER.stride = 0;
ST_POINTER.type = GL_FLOAT;
ST_POINTER.size = 4;
NORMAL_POINTER.ptr = NULL;
NORMAL_POINTER.stride = 0;
NORMAL_POINTER.type = GL_FLOAT;
NORMAL_POINTER.size = 3;
2018-05-05 19:38:55 +00:00
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE GLboolean _glIsVertexDataFastPathCompatible() {
/*
* We provide a "fast path" if vertex data is provided in
* exactly the right format that matches what the PVR can handle.
* This function returns true if all the requirements are met.
*/
/*
* At least these attributes need to be enabled, because we're not going to do any checking
* in the loop
*/
if((ENABLED_VERTEX_ATTRIBUTES & VERTEX_ENABLED_FLAG) != VERTEX_ENABLED_FLAG) return GL_FALSE;
if((ENABLED_VERTEX_ATTRIBUTES & UV_ENABLED_FLAG) != UV_ENABLED_FLAG) return GL_FALSE;
if((ENABLED_VERTEX_ATTRIBUTES & DIFFUSE_ENABLED_FLAG) != DIFFUSE_ENABLED_FLAG) return GL_FALSE;
// All 3 attribute types must have a stride of 32
if(VERTEX_POINTER.stride != 32) return GL_FALSE;
if(UV_POINTER.stride != 32) return GL_FALSE;
if(DIFFUSE_POINTER.stride != 32) return GL_FALSE;
// UV must follow vertex, diffuse must follow UV
if((UV_POINTER.ptr - VERTEX_POINTER.ptr) != sizeof(GLfloat) * 3) return GL_FALSE;
if((DIFFUSE_POINTER.ptr - UV_POINTER.ptr) != sizeof(GLfloat) * 2) return GL_FALSE;
if(VERTEX_POINTER.type != GL_FLOAT) return GL_FALSE;
if(VERTEX_POINTER.size != 3) return GL_FALSE;
if(UV_POINTER.type != GL_FLOAT) return GL_FALSE;
if(UV_POINTER.size != 2) return GL_FALSE;
if(DIFFUSE_POINTER.type != GL_UNSIGNED_BYTE) return GL_FALSE;
2019-09-30 20:59:11 +00:00
/* BGRA is the required color order */
if(DIFFUSE_POINTER.size != GL_BGRA) return GL_FALSE;
return GL_TRUE;
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE GLuint byte_size(GLenum type) {
2018-05-05 19:38:55 +00:00
switch(type) {
case GL_BYTE: return sizeof(GLbyte);
case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
case GL_SHORT: return sizeof(GLshort);
case GL_UNSIGNED_SHORT: return sizeof(GLushort);
case GL_INT: return sizeof(GLint);
case GL_UNSIGNED_INT: return sizeof(GLuint);
case GL_DOUBLE: return sizeof(GLdouble);
case GL_UNSIGNED_INT_2_10_10_10_REV: return sizeof(GLuint);
2018-05-19 08:17:24 +00:00
case GL_FLOAT:
2018-05-05 19:38:55 +00:00
default: return sizeof(GLfloat);
}
}
typedef void (*FloatParseFunc)(GLfloat* out, const GLubyte* in);
typedef void (*ByteParseFunc)(GLubyte* out, const GLubyte* in);
2019-03-25 09:44:59 +00:00
typedef void (*PolyBuildFunc)(Vertex* first, Vertex* previous, Vertex* vertex, Vertex* next, const GLsizei i);
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE float clamp(float d, float min, float max) {
const float t = d < min ? min : d;
return t > max ? max : t;
}
static void _readVertexData3f3f(const float* input, GLuint count, GLubyte stride, float* output) {
ITERATE(count) {
output[0] = input[0];
output[1] = input[1];
output[2] = input[2];
input = (float*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE float conv_i10_to_norm_float(int i10) {
struct attr_bits_10 {
signed int x:10;
} val;
val.x = i10;
return (2.0F * (float)val.x + 1.0F) * (1.0F / 1023.0F);
}
// 10:10:10:2REV format
static void _readVertexData1i3f(const GLuint* input, GLuint count, GLubyte stride, float* output) {
ITERATE(count) {
int inp = *input;
output[0] = conv_i10_to_norm_float((inp) & 0x3ff);
output[1] = conv_i10_to_norm_float(((inp) >> 10) & 0x3ff);
output[2] = conv_i10_to_norm_float(((inp) >> 20) & 0x3ff);
// fprintf(stderr, "%d -> %f %f %f\n", inp, output[0], output[1], output[2]);
input = (GLuint*) (((GLubyte*) input) + stride);
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
2019-03-24 08:09:02 +00:00
/* VE == VertexExtra */
static void _readVertexData3f3fVE(const float* input, GLuint count, GLubyte stride, float* output) {
ITERATE(count) {
2019-03-24 08:09:02 +00:00
output[0] = input[0];
output[1] = input[1];
output[2] = input[2];
input = (float*) (((GLubyte*) input) + stride);
output = (float*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
static void _readVertexData3us3f(const GLushort* input, GLuint count, GLubyte stride, GLfloat* output) {
ITERATE(count) {
output[0] = input[0];
output[1] = input[1];
output[2] = input[2];
input = (GLushort*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
2019-03-24 08:09:02 +00:00
static void _readVertexData3us3fVE(const GLushort* input, GLuint count, GLubyte stride, GLfloat* output) {
ITERATE(count) {
2019-03-24 08:09:02 +00:00
output[0] = input[0];
output[1] = input[1];
output[2] = input[2];
input = (GLushort*) (((GLubyte*) input) + stride);
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
static void _readVertexData3ui3f(const GLuint* input, GLuint count, GLubyte stride, GLfloat* output) {
ITERATE(count) {
output[0] = input[0];
output[1] = input[1];
output[2] = input[2];
input = (GLuint*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
2019-03-24 08:09:02 +00:00
static void _readVertexData3ui3fVE(const GLuint* input, GLuint count, GLubyte stride, GLfloat* output) {
ITERATE(count) {
2019-03-24 08:09:02 +00:00
output[0] = input[0];
output[1] = input[1];
output[2] = input[2];
input = (GLuint*) (((GLubyte*) input) + stride);
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
static void _readVertexData3ub3f(const GLubyte* input, GLuint count, GLubyte stride, float* output) {
const float ONE_OVER_TWO_FIVE_FIVE = 1.0f / 255.0f;
ITERATE(count) {
output[0] = input[0] * ONE_OVER_TWO_FIVE_FIVE;
output[1] = input[1] * ONE_OVER_TWO_FIVE_FIVE;
output[2] = input[2] * ONE_OVER_TWO_FIVE_FIVE;
input += stride;
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
2019-03-24 08:09:02 +00:00
static void _readVertexData3ub3fVE(const GLubyte* input, GLuint count, GLubyte stride, GLfloat* output) {
const float ONE_OVER_TWO_FIVE_FIVE = 1.0f / 255.0f;
ITERATE(count) {
2019-03-24 08:09:02 +00:00
output[0] = input[0] * ONE_OVER_TWO_FIVE_FIVE;
output[1] = input[1] * ONE_OVER_TWO_FIVE_FIVE;
output[2] = input[2] * ONE_OVER_TWO_FIVE_FIVE;
input += stride;
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
static void _readVertexData2f2f(const float* input, GLuint count, GLubyte stride, float* output) {
ITERATE(count) {
output[0] = input[0];
output[1] = input[1];
input = (float*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
2019-03-24 08:09:02 +00:00
static void _readVertexData2f2fVE(const float* input, GLuint count, GLubyte stride, GLfloat* output) {
ITERATE(count) {
2019-03-24 08:09:02 +00:00
output[0] = input[0];
output[1] = input[1];
input = (float*) (((GLubyte*) input) + stride);
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
static void _readVertexData2f3f(const float* input, GLuint count, GLubyte stride, float* output) {
ITERATE(count) {
output[0] = input[0];
output[1] = input[1];
output[2] = 0.0f;
input = (float*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
static void _readVertexData2ub3f(const GLubyte* input, GLuint count, GLubyte stride, float* output) {
const float ONE_OVER_TWO_FIVE_FIVE = 1.0f / 255.0f;
ITERATE(count) {
output[0] = input[0] * ONE_OVER_TWO_FIVE_FIVE;
output[1] = input[1] * ONE_OVER_TWO_FIVE_FIVE;
output[2] = 0.0f;
input += stride;
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
static void _readVertexData2us3f(const GLushort* input, GLuint count, GLubyte stride, float* output) {
ITERATE(count) {
output[0] = input[0];
output[1] = input[1];
output[2] = 0.0f;
input = (GLushort*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
static void _readVertexData2us2f(const GLushort* input, GLuint count, GLubyte stride, float* output) {
ITERATE(count) {
output[0] = input[0];
output[1] = input[1];
input = (GLushort*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
2019-03-24 08:09:02 +00:00
static void _readVertexData2us2fVE(const GLushort* input, GLuint count, GLubyte stride, GLfloat* output) {
ITERATE(count) {
2019-03-24 08:09:02 +00:00
output[0] = input[0];
output[1] = input[1];
input = (GLushort*) (((GLubyte*) input) + stride);
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
static void _readVertexData2ui2f(const GLuint* input, GLuint count, GLubyte stride, float* output) {
ITERATE(count) {
output[0] = input[0];
output[1] = input[1];
input = (GLuint*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
2019-03-24 08:09:02 +00:00
static void _readVertexData2ui2fVE(const GLuint* input, GLuint count, GLubyte stride, GLfloat* output) {
ITERATE(count) {
2019-03-24 08:09:02 +00:00
output[0] = input[0];
output[1] = input[1];
input = (GLuint*) (((GLubyte*) input) + stride);
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
static void _readVertexData2ub2f(const GLubyte* input, GLuint count, GLubyte stride, float* output) {
const float ONE_OVER_TWO_FIVE_FIVE = 1.0f / 255.0f;
ITERATE(count) {
output[0] = input[0] * ONE_OVER_TWO_FIVE_FIVE;
output[1] = input[1] * ONE_OVER_TWO_FIVE_FIVE;
input = (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
2019-03-24 08:09:02 +00:00
static void _readVertexData2ub2fVE(const GLubyte* input, GLuint count, GLubyte stride, GLfloat* output) {
const float ONE_OVER_TWO_FIVE_FIVE = 1.0f / 255.0f;
ITERATE(count) {
2019-03-24 08:09:02 +00:00
output[0] = input[0] * ONE_OVER_TWO_FIVE_FIVE;
output[1] = input[1] * ONE_OVER_TWO_FIVE_FIVE;
input = (((GLubyte*) input) + stride);
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
static void _readVertexData2ui3f(const GLuint* input, GLuint count, GLubyte stride, float* output) {
ITERATE(count) {
output[0] = input[0];
output[1] = input[1];
output[2] = 0.0f;
input = (GLuint*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (float*) (((GLubyte*) output) + sizeof(Vertex));
}
}
static void _readVertexData4ubARGB(const GLubyte* input, GLuint count, GLubyte stride, GLubyte* output) {
ITERATE(count) {
output[R8IDX] = input[0];
output[G8IDX] = input[1];
output[B8IDX] = input[2];
output[A8IDX] = input[3];
input += stride;
output += sizeof(Vertex);
}
}
static void _readVertexData4fARGB(const float* input, GLuint count, GLubyte stride, GLubyte* output) {
ITERATE(count) {
output[R8IDX] = (GLubyte) clamp(input[0] * 255.0f, 0, 255);
output[G8IDX] = (GLubyte) clamp(input[1] * 255.0f, 0, 255);
output[B8IDX] = (GLubyte) clamp(input[2] * 255.0f, 0, 255);
output[A8IDX] = (GLubyte) clamp(input[3] * 255.0f, 0, 255);
input = (float*) (((GLubyte*) input) + stride);
output += sizeof(Vertex);
}
}
static void _readVertexData3fARGB(const float* input, GLuint count, GLubyte stride, GLubyte* output) {
ITERATE(count) {
output[R8IDX] = (GLubyte) clamp(input[0] * 255.0f, 0, 255);
output[G8IDX] = (GLubyte) clamp(input[1] * 255.0f, 0, 255);
output[B8IDX] = (GLubyte) clamp(input[2] * 255.0f, 0, 255);
output[A8IDX] = 1.0f;
input = (float*) (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (GLubyte*) (((GLubyte*) output) + sizeof(Vertex));
}
}
static void _readVertexData3ubARGB(const GLubyte* input, GLuint count, GLubyte stride, GLubyte* output) {
ITERATE(count) {
output[R8IDX] = input[0];
output[G8IDX] = input[1];
output[B8IDX] = input[2];
output[A8IDX] = 1.0f;
input = (((GLubyte*) input) + stride);
2019-03-25 09:44:59 +00:00
output = (GLubyte*) (((GLubyte*) output) + sizeof(Vertex));
}
}
2019-09-30 20:59:11 +00:00
static void _readVertexData4ubRevARGB(const GLubyte* input, GLuint count, GLubyte stride, GLubyte* output) {
ITERATE(count) {
2019-11-30 10:07:41 +00:00
output[B8IDX] = input[0];
output[G8IDX] = input[1];
output[R8IDX] = input[2];
output[A8IDX] = input[3];
2019-09-30 20:59:11 +00:00
input += stride;
output += sizeof(Vertex);
}
}
static void _readVertexData4fRevARGB(const float* input, GLuint count, GLubyte stride, GLubyte* output) {
ITERATE(count) {
output[0] = (GLubyte) clamp(input[0] * 255.0f, 0, 255);
output[1] = (GLubyte) clamp(input[1] * 255.0f, 0, 255);
output[2] = (GLubyte) clamp(input[2] * 255.0f, 0, 255);
output[3] = (GLubyte) clamp(input[3] * 255.0f, 0, 255);
input = (float*) (((GLubyte*) input) + stride);
output += sizeof(Vertex);
}
}
2019-03-24 08:09:02 +00:00
static void _fillWithNegZVE(GLuint count, GLfloat* output) {
ITERATE(count) {
output[0] = output[1] = 0.0f;
output[2] = -1.0f;
2019-03-24 08:09:02 +00:00
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
GL_FORCE_INLINE void _fillWhiteARGB(GLuint count, GLubyte* output) {
ITERATE(count) {
output[R8IDX] = 255;
output[G8IDX] = 255;
output[B8IDX] = 255;
output[A8IDX] = 255;
2019-03-25 09:44:59 +00:00
output += sizeof(Vertex);
}
}
static void _fillZero2f(GLuint count, GLfloat* output) {
ITERATE(count) {
output[0] = output[1] = 0.0f;
2019-03-25 09:44:59 +00:00
output = (GLfloat*) (((GLubyte*) output) + sizeof(Vertex));
2019-03-24 08:09:02 +00:00
}
}
static void _fillZero2fVE(GLuint count, GLfloat* output) {
ITERATE(count) {
2019-03-24 08:09:02 +00:00
output[0] = output[1] = 0.0f;
output = (GLfloat*) (((GLubyte*) output) + sizeof(VertexExtra));
}
}
static void _readVertexData3usARGB(const GLushort* input, GLuint count, GLubyte stride, GLubyte* output) {
_GL_UNUSED(input);
_GL_UNUSED(count);
_GL_UNUSED(stride);
_GL_UNUSED(output);
assert(0 && "Not Implemented");
}
static void _readVertexData3uiARGB(const GLuint* input, GLuint count, GLubyte stride, GLubyte* output) {
_GL_UNUSED(input);
_GL_UNUSED(count);
_GL_UNUSED(stride);
_GL_UNUSED(output);
assert(0 && "Not Implemented");
}
static void _readVertexData4usARGB(const GLushort* input, GLuint count, GLubyte stride, GLubyte* output) {
_GL_UNUSED(input);
_GL_UNUSED(count);
_GL_UNUSED(stride);
_GL_UNUSED(output);
assert(0 && "Not Implemented");
}
static void _readVertexData4uiARGB(const GLuint* input, GLuint count, GLubyte stride, GLubyte* output) {
_GL_UNUSED(input);
_GL_UNUSED(count);
_GL_UNUSED(stride);
_GL_UNUSED(output);
assert(0 && "Not Implemented");
}
2019-09-30 20:59:11 +00:00
static void _readVertexData4usRevARGB(const GLushort* input, GLuint count, GLubyte stride, GLubyte* output) {
_GL_UNUSED(input);
_GL_UNUSED(count);
_GL_UNUSED(stride);
_GL_UNUSED(output);
2019-09-30 20:59:11 +00:00
assert(0 && "Not Implemented");
}
static void _readVertexData4uiRevARGB(const GLuint* input, GLuint count, GLubyte stride, GLubyte* output) {
_GL_UNUSED(input);
_GL_UNUSED(count);
_GL_UNUSED(stride);
_GL_UNUSED(output);
2019-09-30 20:59:11 +00:00
assert(0 && "Not Implemented");
}
2019-03-29 08:47:55 +00:00
GLuint* _glGetEnabledAttributes() {
return &ENABLED_VERTEX_ATTRIBUTES;
}
2018-08-21 15:15:43 +00:00
AttribPointer* _glGetVertexAttribPointer() {
return &VERTEX_POINTER;
}
AttribPointer* _glGetDiffuseAttribPointer() {
return &DIFFUSE_POINTER;
}
AttribPointer* _glGetNormalAttribPointer() {
return &NORMAL_POINTER;
}
AttribPointer* _glGetUVAttribPointer() {
return &UV_POINTER;
}
AttribPointer* _glGetSTAttribPointer() {
return &ST_POINTER;
}
typedef GLuint (*IndexParseFunc)(const GLubyte* in);
static inline GLuint _parseUByteIndex(const GLubyte* in) {
return (GLuint) *in;
}
static inline GLuint _parseUIntIndex(const GLubyte* in) {
return *((GLuint*) in);
}
static inline GLuint _parseUShortIndex(const GLubyte* in) {
return *((GLshort*) in);
2018-05-05 19:38:55 +00:00
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE IndexParseFunc _calcParseIndexFunc(GLenum type) {
2018-05-05 19:38:55 +00:00
switch(type) {
case GL_UNSIGNED_BYTE:
return &_parseUByteIndex;
break;
case GL_UNSIGNED_INT:
return &_parseUIntIndex;
2018-05-05 19:38:55 +00:00
break;
case GL_UNSIGNED_SHORT:
default:
break;
2018-05-05 19:38:55 +00:00
}
return &_parseUShortIndex;
2018-05-05 19:38:55 +00:00
}
2018-05-19 08:17:24 +00:00
/* There was a bug in this macro that shipped with Kos
* which has now been fixed. But just in case...
*/
#undef mat_trans_single3_nodiv
#define mat_trans_single3_nodiv(x, y, z) { \
register float __x __asm__("fr12") = (x); \
register float __y __asm__("fr13") = (y); \
register float __z __asm__("fr14") = (z); \
__asm__ __volatile__( \
"fldi1 fr15\n" \
"ftrv xmtrx, fv12\n" \
: "=f" (__x), "=f" (__y), "=f" (__z) \
: "0" (__x), "1" (__y), "2" (__z) \
: "fr15"); \
x = __x; y = __y; z = __z; \
}
/* FIXME: Is this right? Shouldn't it be fr12->15? */
2018-05-19 08:17:24 +00:00
#undef mat_trans_normal3
#define mat_trans_normal3(x, y, z) { \
register float __x __asm__("fr8") = (x); \
register float __y __asm__("fr9") = (y); \
register float __z __asm__("fr10") = (z); \
__asm__ __volatile__( \
"fldi0 fr11\n" \
"ftrv xmtrx, fv8\n" \
: "=f" (__x), "=f" (__y), "=f" (__z) \
: "0" (__x), "1" (__y), "2" (__z) \
: "fr11"); \
x = __x; y = __y; z = __z; \
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void transformToEyeSpace(GLfloat* point) {
_glMatrixLoadModelView();
mat_trans_single3_nodiv(point[0], point[1], point[2]);
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void transformNormalToEyeSpace(GLfloat* normal) {
_glMatrixLoadNormal();
mat_trans_normal3(normal[0], normal[1], normal[2]);
}
2019-03-24 08:09:02 +00:00
PVRHeader* _glSubmissionTargetHeader(SubmissionTarget* target) {
2019-03-25 19:43:03 +00:00
assert(target->header_offset < target->output->vector.size);
2019-03-24 08:09:02 +00:00
return aligned_vector_at(&target->output->vector, target->header_offset);
}
GL_INLINE_DEBUG Vertex* _glSubmissionTargetStart(SubmissionTarget* target) {
2019-03-25 19:43:03 +00:00
assert(target->start_offset < target->output->vector.size);
2019-03-24 08:09:02 +00:00
return aligned_vector_at(&target->output->vector, target->start_offset);
}
2019-03-25 09:44:59 +00:00
Vertex* _glSubmissionTargetEnd(SubmissionTarget* target) {
2019-03-24 08:09:02 +00:00
return _glSubmissionTargetStart(target) + target->count;
}
2019-03-25 09:44:59 +00:00
static inline void genTriangles(Vertex* output, GLuint count) {
Vertex* it = output + 2;
2020-02-22 21:00:33 +00:00
GLuint i;
for(i = 0; i < count; i += 3) {
2019-03-13 17:38:42 +00:00
it->flags = PVR_CMD_VERTEX_EOL;
it += 3;
2018-10-08 21:03:50 +00:00
}
}
2019-03-25 09:44:59 +00:00
static inline void genQuads(Vertex* output, GLuint count) {
2019-11-30 10:07:41 +00:00
Vertex* final = output + 3;
2020-02-22 21:00:33 +00:00
GLuint i;
for(i = 0; i < count; i += 4) {
2019-11-30 10:07:41 +00:00
swapVertex((final - 1), final);
final->flags = PVR_CMD_VERTEX_EOL;
final += 4;
2018-10-08 21:03:50 +00:00
}
}
2019-03-25 09:44:59 +00:00
static void genTriangleStrip(Vertex* output, GLuint count) {
2018-10-08 21:03:50 +00:00
output[count - 1].flags = PVR_CMD_VERTEX_EOL;
}
2019-03-25 09:44:59 +00:00
static void genTriangleFan(Vertex* output, GLuint count) {
assert(count <= 255);
2018-10-08 21:03:50 +00:00
Vertex* dst = output + (((count - 2) * 3) - 1);
Vertex* src = output + (count - 1);
2018-10-08 21:03:50 +00:00
GLubyte i = count - 2;
while(i--) {
*dst = *src--;
(*dst--).flags = PVR_CMD_VERTEX_EOL;
*dst-- = *src;
*dst-- = *output;
2018-10-08 21:03:50 +00:00
}
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void _readPositionData(const GLuint first, const GLuint count, Vertex* output) {
const GLubyte vstride = (VERTEX_POINTER.stride) ? VERTEX_POINTER.stride : VERTEX_POINTER.size * byte_size(VERTEX_POINTER.type);
const void* vptr = ((GLubyte*) VERTEX_POINTER.ptr + (first * vstride));
if(VERTEX_POINTER.size == 3) {
switch(VERTEX_POINTER.type) {
case GL_DOUBLE:
case GL_FLOAT:
_readVertexData3f3f(vptr, count, vstride, output[0].xyz);
break;
case GL_BYTE:
case GL_UNSIGNED_BYTE:
_readVertexData3ub3f(vptr, count, vstride, output[0].xyz);
break;
case GL_SHORT:
case GL_UNSIGNED_SHORT:
_readVertexData3us3f(vptr, count, vstride, output[0].xyz);
break;
case GL_INT:
case GL_UNSIGNED_INT:
_readVertexData3ui3f(vptr, count, vstride, output[0].xyz);
break;
default:
assert(0 && "Not Implemented");
}
} else if(VERTEX_POINTER.size == 2) {
switch(VERTEX_POINTER.type) {
case GL_DOUBLE:
case GL_FLOAT:
_readVertexData2f3f(vptr, count, vstride, output[0].xyz);
break;
case GL_BYTE:
case GL_UNSIGNED_BYTE:
_readVertexData2ub3f(vptr, count, vstride, output[0].xyz);
break;
case GL_SHORT:
case GL_UNSIGNED_SHORT:
_readVertexData2us3f(vptr, count, vstride, output[0].xyz);
break;
case GL_INT:
case GL_UNSIGNED_INT:
_readVertexData2ui3f(vptr, count, vstride, output[0].xyz);
break;
default:
assert(0 && "Not Implemented");
}
} else {
assert(0 && "Not Implemented");
}
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void _readUVData(const GLuint first, const GLuint count, Vertex* output) {
if((ENABLED_VERTEX_ATTRIBUTES & UV_ENABLED_FLAG) != UV_ENABLED_FLAG) {
_fillZero2f(count, output->uv);
return;
}
const GLubyte uvstride = (UV_POINTER.stride) ? UV_POINTER.stride : UV_POINTER.size * byte_size(UV_POINTER.type);
const void* uvptr = ((GLubyte*) UV_POINTER.ptr + (first * uvstride));
if(UV_POINTER.size == 2) {
switch(UV_POINTER.type) {
case GL_DOUBLE:
case GL_FLOAT:
_readVertexData2f2f(uvptr, count, uvstride, output[0].uv);
break;
case GL_BYTE:
case GL_UNSIGNED_BYTE:
_readVertexData2ub2f(uvptr, count, uvstride, output[0].uv);
break;
case GL_SHORT:
case GL_UNSIGNED_SHORT:
_readVertexData2us2f(uvptr, count, uvstride, output[0].uv);
break;
case GL_INT:
case GL_UNSIGNED_INT:
_readVertexData2ui2f(uvptr, count, uvstride, output[0].uv);
break;
default:
assert(0 && "Not Implemented");
}
} else {
assert(0 && "Not Implemented");
}
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void _readSTData(const GLuint first, const GLuint count, VertexExtra* extra) {
if((ENABLED_VERTEX_ATTRIBUTES & ST_ENABLED_FLAG) != ST_ENABLED_FLAG) {
2019-03-24 08:09:02 +00:00
_fillZero2fVE(count, extra->st);
return;
}
const GLubyte ststride = (ST_POINTER.stride) ? ST_POINTER.stride : ST_POINTER.size * byte_size(ST_POINTER.type);
const void* stptr = ((GLubyte*) ST_POINTER.ptr + (first * ststride));
if(ST_POINTER.size == 2) {
switch(ST_POINTER.type) {
case GL_DOUBLE:
case GL_FLOAT:
2019-03-24 08:09:02 +00:00
_readVertexData2f2fVE(stptr, count, ststride, extra->st);
break;
case GL_BYTE:
case GL_UNSIGNED_BYTE:
2019-03-24 08:09:02 +00:00
_readVertexData2ub2fVE(stptr, count, ststride, extra->st);
break;
case GL_SHORT:
case GL_UNSIGNED_SHORT:
2019-03-24 08:09:02 +00:00
_readVertexData2us2fVE(stptr, count, ststride, extra->st);
break;
case GL_INT:
case GL_UNSIGNED_INT:
2019-03-24 08:09:02 +00:00
_readVertexData2ui2fVE(stptr, count, ststride, extra->st);
break;
default:
assert(0 && "Not Implemented");
}
} else {
assert(0 && "Not Implemented");
}
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void _readNormalData(const GLuint first, const GLuint count, VertexExtra* extra) {
if((ENABLED_VERTEX_ATTRIBUTES & NORMAL_ENABLED_FLAG) != NORMAL_ENABLED_FLAG) {
2019-03-24 08:09:02 +00:00
_fillWithNegZVE(count, extra->nxyz);
return;
}
const GLuint nstride = (NORMAL_POINTER.stride) ? NORMAL_POINTER.stride : NORMAL_POINTER.size * byte_size(NORMAL_POINTER.type);
const void* nptr = ((GLubyte*) NORMAL_POINTER.ptr + (first * nstride));
2020-02-29 09:47:04 +00:00
if(NORMAL_POINTER.size == 3 || NORMAL_POINTER.type == GL_UNSIGNED_INT_2_10_10_10_REV) {
switch(NORMAL_POINTER.type) {
case GL_DOUBLE:
case GL_FLOAT:
2019-03-24 08:09:02 +00:00
_readVertexData3f3fVE(nptr, count, nstride, extra->nxyz);
break;
case GL_BYTE:
case GL_UNSIGNED_BYTE:
2019-03-24 08:09:02 +00:00
_readVertexData3ub3fVE(nptr, count, nstride, extra->nxyz);
break;
case GL_SHORT:
case GL_UNSIGNED_SHORT:
2019-03-24 08:09:02 +00:00
_readVertexData3us3fVE(nptr, count, nstride, extra->nxyz);
break;
case GL_INT:
case GL_UNSIGNED_INT:
2019-03-24 08:09:02 +00:00
_readVertexData3ui3fVE(nptr, count, nstride, extra->nxyz);
break;
2020-02-29 09:47:04 +00:00
case GL_UNSIGNED_INT_2_10_10_10_REV:
_readVertexData1i3f(nptr, count, nstride, extra->nxyz);
break;
default:
2020-02-29 09:47:04 +00:00
fprintf(stderr, "Invalid normal pointer type: %d\n", NORMAL_POINTER.type);
assert(0 && "Not Implemented");
}
} else {
2020-02-29 09:47:04 +00:00
fprintf(stderr, "Invalid normal pointer type or stride (%d or %d)\n", NORMAL_POINTER.type, NORMAL_POINTER.stride);
assert(0 && "Not Implemented");
}
if(_glIsNormalizeEnabled()) {
GLubyte* ptr = (GLubyte*) extra->nxyz;
ITERATE(count) {
GLfloat* n = (GLfloat*) ptr;
float temp = n[0] * n[0];
temp = MATH_fmac(n[1], n[1], temp);
temp = MATH_fmac(n[2], n[2], temp);
float ilength = MATH_fsrra(temp);
n[0] *= ilength;
n[1] *= ilength;
n[2] *= ilength;
ptr += sizeof(VertexExtra);
}
}
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void _readDiffuseData(const GLuint first, const GLuint count, Vertex* output) {
if((ENABLED_VERTEX_ATTRIBUTES & DIFFUSE_ENABLED_FLAG) != DIFFUSE_ENABLED_FLAG) {
/* Just fill the whole thing white if the attribute is disabled */
_fillWhiteARGB(count, output[0].bgra);
return;
}
2020-02-29 13:25:30 +00:00
const GLuint cstride = (DIFFUSE_POINTER.stride) ? DIFFUSE_POINTER.stride : DIFFUSE_POINTER.size * byte_size(DIFFUSE_POINTER.type);
const void* cptr = ((GLubyte*) DIFFUSE_POINTER.ptr) + (first * cstride);
if(DIFFUSE_POINTER.size == 3) {
switch(DIFFUSE_POINTER.type) {
case GL_DOUBLE:
case GL_FLOAT:
_readVertexData3fARGB(cptr, count, cstride, output[0].bgra);
break;
case GL_BYTE:
case GL_UNSIGNED_BYTE:
_readVertexData3ubARGB(cptr, count, cstride, output[0].bgra);
break;
case GL_SHORT:
case GL_UNSIGNED_SHORT:
_readVertexData3usARGB(cptr, count, cstride, output[0].bgra);
break;
case GL_INT:
case GL_UNSIGNED_INT:
_readVertexData3uiARGB(cptr, count, cstride, output[0].bgra);
break;
default:
assert(0 && "Not Implemented");
}
} else if(DIFFUSE_POINTER.size == 4) {
switch(DIFFUSE_POINTER.type) {
case GL_DOUBLE:
case GL_FLOAT:
_readVertexData4fARGB(cptr, count, cstride, output[0].bgra);
break;
case GL_BYTE:
case GL_UNSIGNED_BYTE:
_readVertexData4ubARGB(cptr, count, cstride, output[0].bgra);
break;
case GL_SHORT:
case GL_UNSIGNED_SHORT:
_readVertexData4usARGB(cptr, count, cstride, output[0].bgra);
break;
case GL_INT:
case GL_UNSIGNED_INT:
_readVertexData4uiARGB(cptr, count, cstride, output[0].bgra);
break;
default:
assert(0 && "Not Implemented");
}
2019-09-30 20:59:11 +00:00
} else if(DIFFUSE_POINTER.size == GL_BGRA) {
switch(DIFFUSE_POINTER.type) {
case GL_DOUBLE:
case GL_FLOAT:
_readVertexData4fRevARGB(cptr, count, cstride, output[0].bgra);
break;
case GL_BYTE:
case GL_UNSIGNED_BYTE:
_readVertexData4ubRevARGB(cptr, count, cstride, output[0].bgra);
break;
case GL_SHORT:
case GL_UNSIGNED_SHORT:
_readVertexData4usRevARGB(cptr, count, cstride, output[0].bgra);
break;
case GL_INT:
case GL_UNSIGNED_INT:
_readVertexData4uiRevARGB(cptr, count, cstride, output[0].bgra);
break;
default:
assert(0 && "Not Implemented");
}
}else {
assert(0 && "Not Implemented");
}
}
2019-03-24 08:09:02 +00:00
static void generate(SubmissionTarget* target, const GLenum mode, const GLsizei first, const GLuint count,
2018-08-19 20:10:42 +00:00
const GLubyte* indices, const GLenum type, const GLboolean doTexture, const GLboolean doMultitexture, const GLboolean doLighting) {
/* Read from the client buffers and generate an array of ClipVertices */
2019-03-28 13:09:04 +00:00
TRACE();
2019-09-30 20:59:11 +00:00
static const uint32_t FAST_PATH_BYTE_SIZE = (sizeof(GLfloat) * 3) + (sizeof(GLfloat) * 2) + (sizeof(GLubyte) * 4);
const GLsizei istride = byte_size(type);
2018-10-08 21:03:50 +00:00
if(!indices) {
2019-03-25 16:06:08 +00:00
profiler_push(__func__);
2019-03-28 13:10:53 +00:00
Vertex* start = _glSubmissionTargetStart(target);
if(FAST_PATH_ENABLED) {
/* Copy the pos, uv and color directly in one go */
const GLubyte* pos = VERTEX_POINTER.ptr;
Vertex* it = start;
ITERATE(count) {
it->flags = PVR_CMD_VERTEX;
2019-09-30 20:59:11 +00:00
memcpy(it->xyz, pos, FAST_PATH_BYTE_SIZE);
it++;
pos += VERTEX_POINTER.stride;
}
} else {
_readPositionData(first, count, start);
profiler_checkpoint("positions");
_readDiffuseData(first, count, start);
profiler_checkpoint("diffuse");
if(doTexture) _readUVData(first, count, start);
2019-03-25 16:06:08 +00:00
Vertex* it = _glSubmissionTargetStart(target);
2019-03-25 16:06:08 +00:00
ITERATE(count) {
it->flags = PVR_CMD_VERTEX;
++it;
}
}
2019-03-27 09:39:06 +00:00
VertexExtra* ve = aligned_vector_at(target->extras, 0);
if(doLighting) _readNormalData(first, count, ve);
if(doTexture && doMultitexture) _readSTData(first, count, ve);
2019-03-25 16:06:08 +00:00
profiler_checkpoint("others");
2019-03-24 08:09:02 +00:00
2018-10-08 21:03:50 +00:00
// Drawing arrays
switch(mode) {
case GL_TRIANGLES:
2019-03-28 13:10:53 +00:00
genTriangles(start, count);
break;
2018-10-08 21:03:50 +00:00
case GL_QUADS:
2019-03-28 13:10:53 +00:00
genQuads(start, count);
break;
2018-10-08 21:03:50 +00:00
case GL_TRIANGLE_FAN:
2019-03-28 13:10:53 +00:00
genTriangleFan(start, count);
break;
2018-10-08 21:03:50 +00:00
case GL_TRIANGLE_STRIP:
2019-03-24 08:09:02 +00:00
genTriangleStrip(_glSubmissionTargetStart(target), count);
break;
2018-10-08 21:03:50 +00:00
default:
fprintf(stderr, "Unhandled mode %d\n", (int) mode);
assert(0 && "Not Implemented");
2018-10-08 21:03:50 +00:00
}
2019-03-25 16:06:08 +00:00
profiler_checkpoint("quads");
profiler_pop();
} else {
const IndexParseFunc indexFunc = _calcParseIndexFunc(type);
GLuint j;
const GLubyte* idx = indices;
2019-03-27 09:39:06 +00:00
Vertex* vertices = _glSubmissionTargetStart(target);
VertexExtra* extras = aligned_vector_at(target->extras, 0);
if(FAST_PATH_ENABLED) {
typedef struct FastPath {
float xyz[3];
float uv[2];
2019-11-30 10:07:41 +00:00
uint8_t bgra[4];
} FastPath;
2019-03-27 09:39:06 +00:00
GLboolean readST = doTexture && doMultitexture;
2019-03-27 09:39:06 +00:00
ITERATE(count) {
j = indexFunc(idx);
2019-03-27 09:39:06 +00:00
vertices->flags = PVR_CMD_VERTEX;
FastPath* srcV = (FastPath*) ((uint8_t*) VERTEX_POINTER.ptr + (VERTEX_POINTER.stride * j));
2019-11-30 10:07:41 +00:00
FastPath* dst = (FastPath*) vertices->xyz;
*dst = *srcV;
if(doLighting) _readNormalData(j, 1, extras);
if(readST) _readSTData(j, 1, extras);
++vertices;
++extras;
idx += istride;
}
} else {
ITERATE(count) {
j = indexFunc(idx);
vertices->flags = PVR_CMD_VERTEX;
_readPositionData(j, 1, vertices);
_readDiffuseData(j, 1, vertices);
if(doTexture) _readUVData(j, 1, vertices);
if(doLighting) _readNormalData(j, 1, extras);
if(doTexture && doMultitexture) _readSTData(j, 1, extras);
++vertices;
++extras;
idx += istride;
}
}
Vertex* it = _glSubmissionTargetStart(target);
// Drawing arrays
switch(mode) {
case GL_TRIANGLES:
2019-03-24 08:09:02 +00:00
genTriangles(it, count);
break;
case GL_QUADS:
2019-03-24 08:09:02 +00:00
genQuads(it, count);
break;
case GL_TRIANGLE_FAN:
2019-03-24 08:09:02 +00:00
genTriangleFan(it, count);
break;
case GL_TRIANGLE_STRIP:
2019-03-24 08:09:02 +00:00
genTriangleStrip(it, count);
break;
default:
assert(0 && "Not Implemented");
}
}
}
2019-03-24 08:09:02 +00:00
static void transform(SubmissionTarget* target) {
2019-03-28 13:09:04 +00:00
TRACE();
/* Perform modelview transform, storing W */
2019-03-25 09:44:59 +00:00
Vertex* vertex = _glSubmissionTargetStart(target);
_glApplyRenderMatrix(); /* Apply the Render Matrix Stack */
ITERATE(target->count) {
register float __x __asm__("fr12") = (vertex->xyz[0]);
register float __y __asm__("fr13") = (vertex->xyz[1]);
register float __z __asm__("fr14") = (vertex->xyz[2]);
register float __w __asm__("fr15") = (vertex->w);
__asm__ __volatile__(
"fldi1 fr15\n"
"ftrv xmtrx,fv12\n"
: "=f" (__x), "=f" (__y), "=f" (__z), "=f" (__w)
: "0" (__x), "1" (__y), "2" (__z), "3" (__w)
);
vertex->xyz[0] = __x;
vertex->xyz[1] = __y;
vertex->xyz[2] = __z;
vertex->w = __w;
2018-08-19 20:10:42 +00:00
++vertex;
}
}
2019-03-24 08:09:02 +00:00
static void clip(SubmissionTarget* target) {
2019-03-28 13:09:04 +00:00
TRACE();
/* Perform clipping, generating new vertices as necessary */
2019-03-24 08:09:02 +00:00
_glClipTriangleStrip(target, _glGetShadeModel() == GL_FLAT);
2018-09-06 19:08:55 +00:00
2019-03-24 08:09:02 +00:00
/* Reset the count now that we may have added vertices */
target->count = target->output->vector.size - target->start_offset;
}
2018-08-19 20:10:42 +00:00
static void mat_transform3(const float* xyz, const float* xyzOut, const uint32_t count, const uint32_t inStride, const uint32_t outStride) {
uint8_t* dataIn = (uint8_t*) xyz;
uint8_t* dataOut = (uint8_t*) xyzOut;
ITERATE(count) {
float* in = (float*) dataIn;
float* out = (float*) dataOut;
mat_trans_single3_nodiv_nomod(in[0], in[1], in[2], out[0], out[1], out[2]);
2018-08-19 20:10:42 +00:00
dataIn += inStride;
dataOut += outStride;
}
}
2018-08-19 20:10:42 +00:00
static void mat_transform_normal3(const float* xyz, const float* xyzOut, const uint32_t count, const uint32_t inStride, const uint32_t outStride) {
uint8_t* dataIn = (uint8_t*) xyz;
uint8_t* dataOut = (uint8_t*) xyzOut;
ITERATE(count) {
float* in = (float*) dataIn;
float* out = (float*) dataOut;
mat_trans_normal3_nomod(in[0], in[1], in[2], out[0], out[1], out[2]);
2018-08-19 20:10:42 +00:00
dataIn += inStride;
dataOut += outStride;
}
}
2019-03-24 08:09:02 +00:00
static void light(SubmissionTarget* target) {
2018-08-19 20:10:42 +00:00
static AlignedVector* eye_space_data = NULL;
if(!eye_space_data) {
eye_space_data = (AlignedVector*) malloc(sizeof(AlignedVector));
aligned_vector_init(eye_space_data, sizeof(EyeSpaceData));
}
2019-03-24 08:09:02 +00:00
aligned_vector_resize(eye_space_data, target->count);
2018-08-19 20:10:42 +00:00
/* Perform lighting calculations and manipulate the colour */
2019-03-25 09:44:59 +00:00
Vertex* vertex = _glSubmissionTargetStart(target);
2019-03-24 08:09:02 +00:00
VertexExtra* extra = aligned_vector_at(target->extras, 0);
2018-08-19 20:10:42 +00:00
EyeSpaceData* eye_space = (EyeSpaceData*) eye_space_data->data;
_glMatrixLoadModelView();
2019-03-25 09:44:59 +00:00
mat_transform3(vertex->xyz, eye_space->xyz, target->count, sizeof(Vertex), sizeof(EyeSpaceData));
_glMatrixLoadNormal();
2019-03-24 08:09:02 +00:00
mat_transform_normal3(extra->nxyz, eye_space->n, target->count, sizeof(VertexExtra), sizeof(EyeSpaceData));
2018-08-19 20:10:42 +00:00
EyeSpaceData* ES = aligned_vector_at(eye_space_data, 0);
2019-12-27 10:36:30 +00:00
_glPerformLighting(vertex, ES, target->count);
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void divide(SubmissionTarget* target) {
2019-03-28 13:09:04 +00:00
TRACE();
/* Perform perspective divide on each vertex */
2019-03-25 09:44:59 +00:00
Vertex* vertex = _glSubmissionTargetStart(target);
2019-03-28 13:10:53 +00:00
ITERATE(target->count) {
2020-02-22 21:00:33 +00:00
float f = MATH_fsrra(vertex->w * vertex->w);
vertex->xyz[0] *= f;
vertex->xyz[1] *= f;
2020-03-24 13:58:44 +00:00
vertex->xyz[2] = f;
/* FIXME: Consider taking glDepthRange into account. PVR is designed to use invW rather
* than Z which is unlike most GPUs - this apparently provides advantages.
*
* This can be done (if Z is between -1 and 1) with:
*
* //((DEPTH_RANGE_MULTIPLIER_L * vertex->xyz[2] * f) + DEPTH_RANGE_MULTIPLIER_H);
*/
2018-08-19 20:10:42 +00:00
++vertex;
}
}
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void push(PVRHeader* header, GLboolean multiTextureHeader, PolyList* activePolyList, GLshort textureUnit) {
2019-03-28 13:09:04 +00:00
TRACE();
// Compile the header
pvr_poly_cxt_t cxt = *_glGetPVRContext();
cxt.list_type = activePolyList->list_type;
_glUpdatePVRTextureContext(&cxt, textureUnit);
2019-09-09 08:36:06 +00:00
if(multiTextureHeader) {
assert(cxt.list_type == PVR_LIST_TR_POLY);
cxt.gen.alpha = PVR_ALPHA_ENABLE;
cxt.txr.alpha = PVR_TXRALPHA_ENABLE;
cxt.blend.src = PVR_BLEND_ZERO;
cxt.blend.dst = PVR_BLEND_DESTCOLOR;
cxt.depth.comparison = PVR_DEPTHCMP_EQUAL;
}
pvr_poly_compile(&header->hdr, &cxt);
/* Post-process the vertex list */
2019-03-24 08:09:02 +00:00
/*
* This is currently unnecessary. aligned_vector memsets the allocated objects
* to zero, and we don't touch oargb, also, we don't *enable* oargb yet in the
* pvr header so it should be ignored anyway. If this ever becomes a problem,
* uncomment this.
ClipVertex* vout = output;
2019-03-24 08:09:02 +00:00
const ClipVertex* end = output + count;
while(vout < end) {
vout->oargb = 0;
2018-05-05 19:38:55 +00:00
}
2019-03-24 08:09:02 +00:00
*/
2018-05-05 19:38:55 +00:00
}
2018-09-06 18:45:52 +00:00
#define DEBUG_CLIPPING 0
2020-02-29 13:25:30 +00:00
GL_FORCE_INLINE void submitVertices(GLenum mode, GLsizei first, GLuint count, GLenum type, const GLvoid* indices) {
2019-03-28 13:09:04 +00:00
TRACE();
/* Do nothing if vertices aren't enabled */
if(!(ENABLED_VERTEX_ATTRIBUTES & VERTEX_ENABLED_FLAG)) {
return;
}
/* No vertices? Do nothing */
if(!count) {
return;
}
if(mode == GL_LINE_STRIP || mode == GL_LINES) {
fprintf(stderr, "Line drawing is currently unsupported\n");
return;
}
2019-03-24 08:09:02 +00:00
static SubmissionTarget* target = NULL;
static AlignedVector extras;
/* Initialization of the target and extras */
if(!target) {
target = (SubmissionTarget*) malloc(sizeof(SubmissionTarget));
target->extras = NULL;
target->count = 0;
target->output = NULL;
target->header_offset = target->start_offset = 0;
aligned_vector_init(&extras, sizeof(VertexExtra));
target->extras = &extras;
}
2018-08-19 20:10:42 +00:00
GLboolean doMultitexture, doTexture, doLighting;
GLint activeTexture;
glGetIntegerv(GL_ACTIVE_TEXTURE_ARB, &activeTexture);
glActiveTextureARB(GL_TEXTURE0);
glGetBooleanv(GL_TEXTURE_2D, &doTexture);
glActiveTextureARB(GL_TEXTURE1);
glGetBooleanv(GL_TEXTURE_2D, &doMultitexture);
doLighting = _glIsLightingEnabled();
2018-08-19 20:10:42 +00:00
glActiveTextureARB(activeTexture);
2018-08-19 20:10:42 +00:00
profiler_push(__func__);
2019-03-25 09:48:58 +00:00
/* Polygons are treated as triangle fans, the only time this would be a
* problem is if we supported glPolygonMode(..., GL_LINE) but we don't.
* We optimise the triangle and quad cases.
*/
if(mode == GL_POLYGON) {
if(count == 3) {
mode = GL_TRIANGLES;
} else if(count == 4) {
mode = GL_QUADS;
} else {
mode = GL_TRIANGLE_FAN;
}
}
// We don't handle this any further, so just make sure we never pass it down */
assert(mode != GL_POLYGON);
2019-03-24 08:09:02 +00:00
target->output = _glActivePolyList();
2019-03-25 09:48:58 +00:00
target->count = (mode == GL_TRIANGLE_FAN) ? ((count - 2) * 3) : count;
2019-03-24 08:09:02 +00:00
target->header_offset = target->output->vector.size;
target->start_offset = target->header_offset + 1;
2018-08-01 11:00:56 +00:00
2019-03-28 13:10:53 +00:00
assert(target->count);
2019-03-24 08:09:02 +00:00
/* Make sure we have enough room for all the "extra" data */
aligned_vector_resize(&extras, target->count);
2019-03-24 08:09:02 +00:00
/* Make room for the vertices and header */
aligned_vector_extend(&target->output->vector, target->count + 1);
2018-08-19 20:10:42 +00:00
profiler_checkpoint("allocate");
2019-03-24 08:09:02 +00:00
generate(target, mode, first, count, (GLubyte*) indices, type, doTexture, doMultitexture, doLighting);
2018-08-19 20:10:42 +00:00
profiler_checkpoint("generate");
2018-08-04 20:00:26 +00:00
if(doLighting){
light(target);
}
2018-08-19 20:10:42 +00:00
profiler_checkpoint("light");
2019-03-24 08:09:02 +00:00
transform(target);
2018-08-01 10:32:07 +00:00
2018-08-19 20:10:42 +00:00
profiler_checkpoint("transform");
if(_glIsClippingEnabled()) {
2018-09-06 18:45:52 +00:00
#if DEBUG_CLIPPING
uint32_t i = 0;
fprintf(stderr, "=========\n");
for(i = offset; i < activeList->vector.size; ++i) {
ClipVertex* v = aligned_vector_at(&activeList->vector, i);
if(v->flags == 0xe0000000 || v->flags == 0xf0000000) {
fprintf(stderr, "(%f, %f, %f) -> %x\n", v->xyz[0], v->xyz[1], v->xyz[2], v->flags);
} else {
fprintf(stderr, "%x\n", *((uint32_t*)v));
}
2018-09-06 18:45:52 +00:00
}
#endif
2019-03-24 08:09:02 +00:00
clip(target);
2019-03-25 19:43:03 +00:00
assert(extras.size == target->count);
2018-09-06 18:45:52 +00:00
#if DEBUG_CLIPPING
fprintf(stderr, "--------\n");
for(i = offset; i < activeList->vector.size; ++i) {
ClipVertex* v = aligned_vector_at(&activeList->vector, i);
if(v->flags == 0xe0000000 || v->flags == 0xf0000000) {
fprintf(stderr, "(%f, %f, %f) -> %x\n", v->xyz[0], v->xyz[1], v->xyz[2], v->flags);
} else {
fprintf(stderr, "%x\n", *((uint32_t*)v));
}
}
2018-09-06 18:46:04 +00:00
#endif
2018-08-01 10:32:07 +00:00
}
2018-08-19 20:10:42 +00:00
profiler_checkpoint("clip");
2019-03-24 08:09:02 +00:00
divide(target);
2018-08-19 20:10:42 +00:00
profiler_checkpoint("divide");
2019-09-09 08:36:06 +00:00
push(_glSubmissionTargetHeader(target), GL_FALSE, target->output, 0);
2018-08-04 20:00:26 +00:00
2018-08-19 20:10:42 +00:00
profiler_checkpoint("push");
2018-08-04 20:00:26 +00:00
/*
Now, if multitexturing is enabled, we want to send exactly the same vertices again, except:
- We want to enable blending, and send them to the TR list
- We want to set the depth func to GL_EQUAL
- We want to set the second texture ID
- We want to set the uv coordinates to the passed st ones
*/
2019-03-25 16:07:27 +00:00
if(!doMultitexture) {
/* Multitexture actively disabled */
profiler_pop();
return;
}
TextureObject* texture1 = _glGetTexture1();
2018-08-04 20:00:26 +00:00
2019-03-24 08:09:02 +00:00
/* Multitexture implicitly disabled */
2018-08-04 20:00:26 +00:00
if(!texture1 || ((ENABLED_VERTEX_ATTRIBUTES & ST_ENABLED_FLAG) != ST_ENABLED_FLAG)) {
2019-03-24 08:09:02 +00:00
/* Multitexture actively disabled */
2018-08-19 20:10:42 +00:00
profiler_pop();
2018-08-04 20:00:26 +00:00
return;
}
/* Push back a copy of the list to the transparent poly list, including the header
2019-03-24 08:09:02 +00:00
(hence the + 1)
*/
2019-03-25 09:44:59 +00:00
Vertex* vertex = aligned_vector_push_back(
&_glTransparentPolyList()->vector, (Vertex*) _glSubmissionTargetHeader(target), target->count + 1
);
2019-03-28 13:10:53 +00:00
assert(vertex);
PVRHeader* mtHeader = (PVRHeader*) vertex++;
2018-08-04 20:00:26 +00:00
2019-03-24 08:09:02 +00:00
/* Replace the UV coordinates with the ST ones */
VertexExtra* ve = aligned_vector_at(target->extras, 0);
2019-03-28 13:10:53 +00:00
ITERATE(target->count) {
2019-03-24 08:09:02 +00:00
vertex->uv[0] = ve->st[0];
vertex->uv[1] = ve->st[1];
2018-08-19 20:10:42 +00:00
++vertex;
2019-03-24 08:09:02 +00:00
++ve;
2018-08-04 20:00:26 +00:00
}
/* Send the buffer again to the transparent list */
2019-09-09 08:36:06 +00:00
push(mtHeader, GL_TRUE, _glTransparentPolyList(), 1);
2019-03-29 07:51:09 +00:00
profiler_pop();
}
void APIENTRY glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid* indices) {
TRACE();
if(_glCheckImmediateModeInactive(__func__)) {
2018-05-12 13:54:06 +00:00
return;
}
_glRecalcFastPath();
2018-05-12 13:54:06 +00:00
submitVertices(mode, 0, count, type, indices);
2018-05-05 19:38:55 +00:00
}
void APIENTRY glDrawArrays(GLenum mode, GLint first, GLsizei count) {
TRACE();
if(_glCheckImmediateModeInactive(__func__)) {
2018-05-12 13:54:06 +00:00
return;
}
_glRecalcFastPath();
2018-05-12 13:54:06 +00:00
2019-03-28 13:07:53 +00:00
submitVertices(mode, first, count, GL_UNSIGNED_INT, NULL);
2018-05-05 19:38:55 +00:00
}
void APIENTRY glEnableClientState(GLenum cap) {
TRACE();
2018-05-05 19:38:55 +00:00
switch(cap) {
case GL_VERTEX_ARRAY:
ENABLED_VERTEX_ATTRIBUTES |= VERTEX_ENABLED_FLAG;
break;
case GL_COLOR_ARRAY:
ENABLED_VERTEX_ATTRIBUTES |= DIFFUSE_ENABLED_FLAG;
break;
case GL_NORMAL_ARRAY:
ENABLED_VERTEX_ATTRIBUTES |= NORMAL_ENABLED_FLAG;
break;
case GL_TEXTURE_COORD_ARRAY:
(ACTIVE_CLIENT_TEXTURE) ?
(ENABLED_VERTEX_ATTRIBUTES |= ST_ENABLED_FLAG):
(ENABLED_VERTEX_ATTRIBUTES |= UV_ENABLED_FLAG);
break;
default:
2019-09-22 19:52:58 +00:00
_glKosThrowError(GL_INVALID_ENUM, __func__);
2018-05-05 19:38:55 +00:00
}
}
void APIENTRY glDisableClientState(GLenum cap) {
TRACE();
2018-05-05 19:38:55 +00:00
switch(cap) {
case GL_VERTEX_ARRAY:
ENABLED_VERTEX_ATTRIBUTES &= ~VERTEX_ENABLED_FLAG;
break;
case GL_COLOR_ARRAY:
ENABLED_VERTEX_ATTRIBUTES &= ~DIFFUSE_ENABLED_FLAG;
break;
case GL_NORMAL_ARRAY:
ENABLED_VERTEX_ATTRIBUTES &= ~NORMAL_ENABLED_FLAG;
break;
case GL_TEXTURE_COORD_ARRAY:
(ACTIVE_CLIENT_TEXTURE) ?
(ENABLED_VERTEX_ATTRIBUTES &= ~ST_ENABLED_FLAG):
(ENABLED_VERTEX_ATTRIBUTES &= ~UV_ENABLED_FLAG);
break;
default:
2019-09-22 19:52:58 +00:00
_glKosThrowError(GL_INVALID_ENUM, __func__);
2018-05-05 19:38:55 +00:00
}
}
GLuint _glGetActiveClientTexture() {
return ACTIVE_CLIENT_TEXTURE;
}
void APIENTRY glClientActiveTextureARB(GLenum texture) {
TRACE();
if(texture < GL_TEXTURE0_ARB || texture > GL_TEXTURE0_ARB + MAX_TEXTURE_UNITS) {
_glKosThrowError(GL_INVALID_ENUM, __func__);
}
if(_glKosHasError()) {
_glKosPrintError();
return;
}
ACTIVE_CLIENT_TEXTURE = (texture == GL_TEXTURE1_ARB) ? 1 : 0;
2018-05-05 19:38:55 +00:00
}
GLboolean _glRecalcFastPath() {
FAST_PATH_ENABLED = _glIsVertexDataFastPathCompatible();
return FAST_PATH_ENABLED;
}
void APIENTRY glTexCoordPointer(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer) {
TRACE();
if(size < 1 || size > 4) {
_glKosThrowError(GL_INVALID_VALUE, __func__);
_glKosPrintError();
return;
}
AttribPointer* tointer = (ACTIVE_CLIENT_TEXTURE == 0) ? &UV_POINTER : &ST_POINTER;
2018-05-05 19:38:55 +00:00
tointer->ptr = pointer;
tointer->stride = stride;
tointer->type = type;
tointer->size = size;
2018-05-05 19:38:55 +00:00
}
void APIENTRY glVertexPointer(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer) {
TRACE();
if(size < 2 || size > 4) {
_glKosThrowError(GL_INVALID_VALUE, __func__);
_glKosPrintError();
return;
}
2018-05-05 19:38:55 +00:00
VERTEX_POINTER.ptr = pointer;
VERTEX_POINTER.stride = stride;
VERTEX_POINTER.type = type;
VERTEX_POINTER.size = size;
}
void APIENTRY glColorPointer(GLint size, GLenum type, GLsizei stride, const GLvoid * pointer) {
TRACE();
2019-09-30 20:59:11 +00:00
if(size != 3 && size != 4 && size != GL_BGRA) {
_glKosThrowError(GL_INVALID_VALUE, __func__);
_glKosPrintError();
return;
}
2018-05-05 19:38:55 +00:00
DIFFUSE_POINTER.ptr = pointer;
DIFFUSE_POINTER.stride = stride;
DIFFUSE_POINTER.type = type;
DIFFUSE_POINTER.size = size;
}
void APIENTRY glNormalPointer(GLenum type, GLsizei stride, const GLvoid * pointer) {
TRACE();
2020-02-29 09:47:04 +00:00
GLint validTypes[] = {
GL_DOUBLE,
GL_FLOAT,
GL_BYTE,
GL_UNSIGNED_BYTE,
GL_INT,
GL_UNSIGNED_INT,
GL_UNSIGNED_INT_2_10_10_10_REV,
0
};
if(_glCheckValidEnum(type, validTypes, __func__) != 0) {
return;
}
2018-05-05 19:38:55 +00:00
NORMAL_POINTER.ptr = pointer;
NORMAL_POINTER.stride = stride;
NORMAL_POINTER.type = type;
2020-02-29 09:47:04 +00:00
NORMAL_POINTER.size = (type == GL_UNSIGNED_INT_2_10_10_10_REV) ? 1 : 3;
2018-05-05 19:38:55 +00:00
}