Hardware vertex transform, preliminary and optional. No lighting yet. Disabled until it works right...

This commit is contained in:
Henrik Rydgard committed 2012-12-20 15:07:58 +01:00
1 parent 1b5407f505
commit 4f376a2c34
9 files changed
+522 -157

No files matched your search

+14
View File
@@ -609,21 +609,25 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff)
case GE_CMD_TEXSCALEU:
gstate_c.uScale = getFloat24(data);
DEBUG_LOG(G3D, "DL Texture U Scale: %f", gstate_c.uScale);
shaderManager.DirtyUniform(DIRTY_UVSCALEOFFSET);
break;
case GE_CMD_TEXSCALEV:
gstate_c.vScale = getFloat24(data);
DEBUG_LOG(G3D, "DL Texture V Scale: %f", gstate_c.vScale);
shaderManager.DirtyUniform(DIRTY_UVSCALEOFFSET);
break;
case GE_CMD_TEXOFFSETU:
gstate_c.uOff = getFloat24(data);
DEBUG_LOG(G3D, "DL Texture U Offset: %f", gstate_c.uOff);
shaderManager.DirtyUniform(DIRTY_UVSCALEOFFSET);
break;
case GE_CMD_TEXOFFSETV:
gstate_c.vOff = getFloat24(data);
DEBUG_LOG(G3D, "DL Texture V Offset: %f", gstate_c.vOff);
shaderManager.DirtyUniform(DIRTY_UVSCALEOFFSET);
break;
case GE_CMD_SCISSOR1:
@@ -829,6 +833,8 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff)
case GE_CMD_MATERIALAMBIENT:
DEBUG_LOG(G3D,"DL Material Ambient Color: %06x", data);
if (diff)
shaderManager.DirtyUniform(DIRTY_MATAMBIENTALPHA);
break;
case GE_CMD_MATERIALDIFFUSE:
@@ -845,6 +851,8 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff)
case GE_CMD_MATERIALALPHA:
DEBUG_LOG(G3D,"DL Material Alpha Color: %06x", data);
if (diff)
shaderManager.DirtyUniform(DIRTY_MATAMBIENTALPHA);
break;
case GE_CMD_MATERIALSPECULARCOEF:
@@ -1034,6 +1042,8 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff)
break;
case GE_CMD_TEXENVCOLOR:
DEBUG_LOG(G3D,"DL TexEnvColor %06x", data);
if (diff)
shaderManager.DirtyUniform(DIRTY_TEXENV);
break;
case GE_CMD_TEXMODE:
DEBUG_LOG(G3D,"DL TexMode %08x", data);
@@ -1100,6 +1110,7 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff)
if (num < 12)
gstate.worldMatrix[num++] = getFloat24(data);
gstate.worldmtxnum = (gstate.worldmtxnum & 0xFF000000) | (num & 0xF);
shaderManager.DirtyUniform(DIRTY_WORLDMATRIX);
}
break;
@@ -1115,6 +1126,7 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff)
if (num < 12)
gstate.viewMatrix[num++] = getFloat24(data);
gstate.viewmtxnum = (gstate.viewmtxnum & 0xFF000000) | (num & 0xF);
shaderManager.DirtyUniform(DIRTY_VIEWMATRIX);
}
break;
@@ -1146,6 +1158,7 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff)
gstate.tgenMatrix[num++] = getFloat24(data);
gstate.texmtxnum = (gstate.texmtxnum & 0xFF000000) | (num & 0xF);
}
shaderManager.DirtyUniform(DIRTY_TEXMATRIX);
break;
case GE_CMD_BONEMATRIXNUMBER:
@@ -1157,6 +1170,7 @@ void GLES_GPU::ExecuteOp(u32 op, u32 diff)
DEBUG_LOG(G3D,"DL BONE data #%i %f", gstate.boneMatrixNumber & 0x7f, getFloat24(data));
{
int num = gstate.boneMatrixNumber & 0x7F;
shaderManager.DirtyUniform(DIRTY_BONEMATRIX0 << (num / 12));
if (num < 96) {
gstate.boneMatrix[num++] = getFloat24(data);
}
+2
View File
@@ -25,6 +25,7 @@
#include "gfx_es2/fbo.h"
class ShaderManager;
struct DecVtxFormat;
class GLES_GPU : public GPUInterface
{
@@ -51,6 +52,7 @@ public:
private:
// TransformPipeline.cpp
void TransformAndDrawPrim(void *verts, void *inds, int prim, int vertexCount, float *customUV, int forceIndexType, int *bytesRead = 0);
void SoftwareTransformAndDraw(int prim, LinkedShader *program, int forceIndexType, int vertexCount, void *inds, const DecVtxFormat &decVtxFormat, int indexLowerBound, int indexUpperBound, float *customUV);
void ApplyDrawState();
void Flush(int prim);
void UpdateViewportAndProjection();
+2 -2
View File
@@ -88,7 +88,7 @@ char *GenerateFragmentShader()
WRITE(p, "uniform vec3 u_texenv;\n");
WRITE(p, "varying vec4 v_color0;\n");
if (lmode)
WRITE(p, "varying vec4 v_color1;\n");
WRITE(p, "varying vec3 v_color1;\n");
if (doTexture)
WRITE(p, "varying vec2 v_texcoord;\n");
if (gstate.isFogEnabled())
@@ -107,7 +107,7 @@ char *GenerateFragmentShader()
const char *secondary = "";
// Secondary color for specular on top of texture
if (lmode) {
WRITE(p, " vec4 s = vec4(v_color1.xyz, 0.0);");
WRITE(p, " vec4 s = vec4(v_color1, 0.0);");
secondary = " + s";
} else {
WRITE(p, " vec4 s = vec4(0.0, 0.0, 0.0, 0.0);\n");
+50 -2
View File
@@ -81,6 +81,20 @@ LinkedShader::LinkedShader(Shader *vs, Shader *fs)
u_fogcoef = glGetUniformLocation(program, "u_fogcoef");
u_alphacolorref = glGetUniformLocation(program, "u_alphacolorref");
// Transform
u_view = glGetUniformLocation(program, "u_view");
u_world = glGetUniformLocation(program, "u_world");
u_texmtx = glGetUniformLocation(program, "u_texmtx");
for (int i = 0; i < 8; i++) {
char name[64];
sprintf(name, "u_bone%i", i);
u_bone[i] = glGetUniformLocation(program, name);
}
// Lighting, texturing
u_matambientalpha = glGetUniformLocation(program, "u_matambientalpha");
u_uvscaleoffset = glGetUniformLocation(program, "u_uvscaleoffset");
a_position = glGetAttribLocation(program, "a_position");
a_color0 = glGetAttribLocation(program, "a_color0");
a_color1 = glGetAttribLocation(program, "a_color1");
@@ -93,7 +107,7 @@ LinkedShader::LinkedShader(Shader *vs, Shader *fs)
// Default uniform values
glUniform1i(u_tex, 0);
// The rest, use the "dirty" mechanism.
dirtyUniforms = DIRTY_PROJMATRIX | DIRTY_PROJTHROUGHMATRIX | DIRTY_TEXENV | DIRTY_ALPHACOLORREF;
dirtyUniforms = DIRTY_ALL;
}
LinkedShader::~LinkedShader() {
@@ -107,6 +121,12 @@ static void SetColorUniform3(int uniform, u32 color)
glUniform3fv(uniform, 1, col);
}
static void SetColorUniform3Alpha(int uniform, u32 color, u8 alpha)
{
const float col[4] = { ((color & 0xFF0000) >> 16) / 255.0f, ((color & 0xFF00) >> 8) / 255.0f, ((color & 0xFF)) / 255.0f, alpha/255.0f};
glUniform4fv(uniform, 1, col);
}
void LinkedShader::use() {
glUseProgram(program);
glUniform1i(u_tex, 0);
@@ -149,6 +169,34 @@ void LinkedShader::use() {
glUniform2fv(u_fogcoef, 1, fogcoef);
}
// Texturing
if (u_uvscaleoffset != -1 && (dirtyUniforms & DIRTY_UVSCALEOFFSET)) {
const float uvscaleoff[4] = { gstate_c.uScale, gstate_c.vScale, gstate_c.uOff, gstate_c.vOff};
glUniform4fv(u_uvscaleoffset, 1, uvscaleoff);
}
// Transform
if (u_world != -1 && (dirtyUniforms & DIRTY_WORLDMATRIX)) {
glUniformMatrix4x3fv(u_world, 1, GL_FALSE, gstate.worldMatrix);
}
if (u_view != -1 && (dirtyUniforms & DIRTY_VIEWMATRIX)) {
glUniformMatrix4x3fv(u_view, 1, GL_FALSE, gstate.viewMatrix);
}
if (u_texmtx != -1 && (dirtyUniforms & DIRTY_TEXMATRIX)) {
glUniformMatrix4x3fv(u_texmtx, 1, GL_FALSE, gstate.tgenMatrix);
}
for (int i = 0; i < 8; i++) {
if (u_bone[i] != -1 && (dirtyUniforms & (DIRTY_BONEMATRIX0 << i))) {
glUniformMatrix4x3fv(u_bone[i], 1, GL_FALSE, gstate.boneMatrix + 12 * i);
}
}
// Lighting
if (u_matambientalpha != -1 && (dirtyUniforms & DIRTY_MATAMBIENTALPHA)) {
SetColorUniform3Alpha(u_matambientalpha, gstate.materialambient, gstate.materialalpha & 0xFF);
}
dirtyUniforms = 0;
}
@@ -211,7 +259,7 @@ LinkedShader *ShaderManager::ApplyShader(int prim)
Shader *vs;
if (vsIter == vsCache.end()) {
// Vertex shader not in cache. Let's compile it.
char *shaderCode = GenerateVertexShader();
char *shaderCode = GenerateVertexShader(prim);
vs = new Shader(shaderCode, GL_VERTEX_SHADER);
vsCache[VSID] = vs;
} else {
+11 -3
View File
@@ -48,14 +48,21 @@ struct LinkedShader
int u_proj;
int u_proj_through;
int u_texenv;
int u_view;
int u_texmtx;
int u_world;
int u_bone[8];
// Fragment processing inputs
int u_alphacolorref;
int u_fogcolor;
int u_fogcoef;
// Texturing
int u_uvscaleoffset;
// Lighting
int u_ambientcolor;
int u_matambientalpha;
int u_light[4]; // each light consist of vec4[3]
};
@@ -75,10 +82,11 @@ enum
DIRTY_LIGHT2 = (1 << 14),
DIRTY_LIGHT3 = (1 << 15),
DIRTY_GLOBALAMBIENT = (1 << 16),
DIRTY_MATAMBIENTALPHA = (1 << 16),
DIRTY_MATERIAL = (1 << 17), // let's set all 4 together (emissive ambient diffuse specular). We hide specular coef in specular.a
DIRTY_UVSCALEOFFSET = (1 << 18), // this will be dirtied ALL THE TIME... maybe we'll need to do "last value with this shader compares"
DIRTY_WORLDMATRIX = (1 << 21),
DIRTY_VIEWMATRIX = (1 << 22), // Maybe we'll fold this into projmatrix eventually
DIRTY_TEXMATRIX = (1 << 23),
DIRTY_BONEMATRIX0 = (1 << 24),
+150 -112
View File
@@ -143,14 +143,6 @@ void Lighter::Light(float colorOut0[4], float colorOut1[4], const float colorIn[
bool doSpecular = (comp != GE_LIGHTCOMP_ONLYDIFFUSE);
bool poweredDiffuse = comp == GE_LIGHTCOMP_BOTHWITHPOWDIFFUSE;
float lightScale = 1.0f;
if (type != GE_LIGHTTYPE_DIRECTIONAL)
{
float distance = toLight.Normalize();
lightScale = 1.0f / (gstate_c.lightatt[l][0] + gstate_c.lightatt[l][1]*distance + gstate_c.lightatt[l][2]*distance*distance);
if (lightScale > 1.0f) lightScale = 1.0f;
}
float dot = toLight * norm;
// Clamp dot to zero.
@@ -159,6 +151,14 @@ void Lighter::Light(float colorOut0[4], float colorOut1[4], const float colorIn[
if (poweredDiffuse)
dot = powf(dot, specCoef_);
float lightScale = 1.0f;
if (type != GE_LIGHTTYPE_DIRECTIONAL)
{
float distance = toLight.Normalize();
lightScale = 1.0f / (gstate_c.lightatt[l][0] + gstate_c.lightatt[l][1]*distance + gstate_c.lightatt[l][2]*distance*distance);
if (lightScale > 1.0f) lightScale = 1.0f;
}
Color4 diff = (gstate_c.lightColor[1][l] * *diffuse) * (dot * lightScale);
// Real PSP specular
@@ -192,47 +192,59 @@ void Lighter::Light(float colorOut0[4], float colorOut1[4], const float colorIn[
}
}
// This is the software transform pipeline, which is necessary for supporting RECT
// primitives correctly. Other primitives are possible to transform and light in hardware
// using vertex shader, which will be way, way faster, especially on mobile. This has
// not yet been implemented though.
void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int vertexCount, float *customUV, int forceIndexType, int *bytesRead)
struct GlTypeInfo {
GLuint type;
int count;
GLboolean normalized;
};
const GlTypeInfo GLComp[8] = {
{0}, // DEC_NONE,
{GL_FLOAT, 1, GL_FALSE}, // DEC_FLOAT_1,
{GL_FLOAT, 2, GL_FALSE}, // DEC_FLOAT_2,
{GL_FLOAT, 3, GL_FALSE}, // DEC_FLOAT_3,
{GL_FLOAT, 4, GL_FALSE}, // DEC_FLOAT_4,
{GL_BYTE, 3, GL_TRUE}, // DEC_S8_3,
{GL_SHORT, 3, GL_TRUE},// DEC_S16_3,
{GL_UNSIGNED_BYTE, 4, GL_TRUE},// DEC_U8_4,
};
static inline void VertexAttribSetup(int attrib, int fmt, int stride, u8 *ptr) {
if (attrib != -1 && fmt) {
const GlTypeInfo &type = GLComp[fmt];
glEnableVertexAttribArray(attrib);
glVertexAttribPointer(attrib, type.count, type.type, type.normalized, stride, ptr);
}
}
static inline void VertexAttribDisable(int attrib, int fmt) {
if (attrib != -1 && fmt) {
glDisableVertexAttribArray(attrib);
}
}
// TODO: Use VBO and get rid of the vertexData pointers - with that, we will supply only offsets
static void SetupDecFmtForDraw(LinkedShader *program, const DecVtxFormat &decFmt, u8 *vertexData) {
VertexAttribSetup(program->a_weight0123, decFmt.w0fmt, decFmt.stride, vertexData + decFmt.w0off);
VertexAttribSetup(program->a_weight4567, decFmt.w1fmt, decFmt.stride, vertexData + decFmt.w1off);
VertexAttribSetup(program->a_texcoord, decFmt.uvfmt, decFmt.stride, vertexData + decFmt.uvoff);
VertexAttribSetup(program->a_color0, decFmt.c0fmt, decFmt.stride, vertexData + decFmt.c0off);
VertexAttribSetup(program->a_color1, decFmt.c1fmt, decFmt.stride, vertexData + decFmt.c1off);
VertexAttribSetup(program->a_normal, decFmt.nrmfmt, decFmt.stride, vertexData + decFmt.nrmoff);
VertexAttribSetup(program->a_position, decFmt.posfmt, decFmt.stride, vertexData + decFmt.posoff);
}
static void DesetupDecFmtForDraw(LinkedShader *program, const DecVtxFormat &decFmt) {
VertexAttribDisable(program->a_weight0123, decFmt.w0fmt);
VertexAttribDisable(program->a_weight4567, decFmt.w1fmt);
VertexAttribDisable(program->a_texcoord, decFmt.uvfmt);
VertexAttribDisable(program->a_color0, decFmt.c0fmt);
VertexAttribDisable(program->a_color1, decFmt.c1fmt);
VertexAttribDisable(program->a_normal, decFmt.nrmfmt);
VertexAttribDisable(program->a_position, decFmt.posfmt);
}
void GLES_GPU::SoftwareTransformAndDraw(int prim, LinkedShader *program, int forceIndexType, int vertexCount, void *inds, const DecVtxFormat &decVtxFormat, int indexLowerBound, int indexUpperBound, float *customUV)
{
int indexLowerBound, indexUpperBound;
// First, decode the verts and apply morphing
VertexDecoder dec;
dec.SetVertexType(gstate.vertType);
dec.DecodeVerts(decoded, verts, inds, prim, vertexCount, &indexLowerBound, &indexUpperBound);
if (bytesRead)
*bytesRead = vertexCount * dec.VertexSize();
// And here we should return, having collected the morphed but untransformed vertices.
// Note that DecodeVerts should convert strips into indexed lists etc, adding to our
// current vertex buffer and index buffer.
// The rest below here should only execute on Flush.
#if 0
for (int i = indexLowerBound; i <= indexUpperBound; i++) {
PrintDecodedVertex(decoded[i], gstate.vertType);
}
#endif
bool useTexCoord = false;
// Check if anything needs updating
if (gstate_c.textureChanged)
{
if ((gstate.textureMapEnable & 1) && !gstate.isModeClear())
{
PSPSetTexture();
useTexCoord = true;
}
}
gpuStats.numDrawCalls++;
gpuStats.numVertsTransformed += vertexCount;
bool throughmode = (gstate.vertType & GE_VTYPE_THROUGH_MASK) != 0;
/*
DEBUG_LOG(G3D, "View matrix:");
const float *m = &gstate.viewMatrix[0];
@@ -259,6 +271,9 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
float v2[3] = {0};
float uv2[2] = {0};
bool throughmode = (gstate.vertType & GE_VTYPE_THROUGH_MASK) != 0;
// TODO: Could use glDrawElements in some cases, see below.
// TODO: Split up into multiple draw calls for GLES 2.0 where you can't guarantee support for more than 0x10000 verts.
@@ -270,7 +285,7 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
Lighter lighter;
VertexReader reader(decoded, dec.GetDecVtxFmt());
VertexReader reader(decoded, decVtxFormat);
for (int index = indexLowerBound; index <= indexUpperBound; index++)
{
reader.Goto(index);
@@ -349,6 +364,7 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
}
}
// Yes, we really must multiply by the world matrix too.
Vec3ByMatrix43(out, psum.v, gstate.worldMatrix);
if (reader.hasNormal()) {
nsum.Normalize();
@@ -368,7 +384,7 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
if (gstate.lightingEnable & 1)
{
// TODO: don't ignore gstate.lmode - we should send two colors in that case
// Don't ignore gstate.lmode - we should send two colors in that case
if (gstate.lmode & 1) {
// Separate colors
for (int j = 0; j < 4; j++) {
@@ -385,7 +401,7 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
}
else
{
if(dec.hasColor()) {
if (reader.hasColor0()) {
for (int j = 0; j < 4; j++) {
c0[j] = unlitColor[j];
c1[j] = 0.0f;
@@ -405,7 +421,7 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
float ruv[2];
reader.ReadUV(ruv);
// Perform texture coordinate generation after the transform and lighting - one style of UV depends on lights.
switch (gstate.texmapmode & 0x3)
switch (gstate.getUVGenMode())
{
case 0: // UV mapping
// Texture scale/offset is only performed in this mode.
@@ -416,7 +432,7 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
{
// Projection mapping
Vec3 source;
switch ((gstate.texmapmode >> 8) & 0x3)
switch (gstate.getUVProjMode())
{
case 0: // Use model space XYZ as source
source = pos;
@@ -439,12 +455,10 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
}
break;
case 2:
// Shade mapping
// Shade mapping - use dots from light sources to generate U and V.
{
int lightsource1 = gstate.texshade & 0x3;
int lightsource2 = (gstate.texshade >> 8) & 0x3;
uv[0] = dots[lightsource1];
uv[1] = dots[lightsource2];
uv[0] = dots[gstate.getUVLS0()];
uv[1] = dots[gstate.getUVLS1()];
}
break;
case 3:
@@ -569,22 +583,14 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
}
}
// TODO: This should not be done on every drawcall, we should collect vertex data
// until critical state changes. That's when we draw (flush).
ApplyDrawState();
UpdateViewportAndProjection();
LinkedShader *program = shaderManager_->ApplyShader(prim);
// TODO: Make a cache for glEnableVertexAttribArray and glVertexAttribPtr states, these spam the gDebugger log.
glEnableVertexAttribArray(program->a_position);
if (useTexCoord && program->a_texcoord != -1) glEnableVertexAttribArray(program->a_texcoord);
if (program->a_texcoord != -1) glEnableVertexAttribArray(program->a_texcoord);
if (program->a_color0 != -1) glEnableVertexAttribArray(program->a_color0);
if (program->a_color1 != -1) glEnableVertexAttribArray(program->a_color1);
const int vertexSize = sizeof(transformed[0]);
glVertexAttribPointer(program->a_position, 3, GL_FLOAT, GL_FALSE, vertexSize, drawBuffer);
if (useTexCoord && program->a_texcoord != -1) glVertexAttribPointer(program->a_texcoord, 2, GL_FLOAT, GL_FALSE, vertexSize, ((uint8_t*)drawBuffer) + 3 * 4);
if (program->a_texcoord != -1) glVertexAttribPointer(program->a_texcoord, 2, GL_FLOAT, GL_FALSE, vertexSize, ((uint8_t*)drawBuffer) + 3 * 4);
if (program->a_color0 != -1) glVertexAttribPointer(program->a_color0, 4, GL_FLOAT, GL_FALSE, vertexSize, ((uint8_t*)drawBuffer) + 5 * 4);
if (program->a_color1 != -1) glVertexAttribPointer(program->a_color1, 4, GL_FLOAT, GL_FALSE, vertexSize, ((uint8_t*)drawBuffer) + 9 * 4);
// NOTICE_LOG(G3D,"DrawPrimitive: %i", numTrans);
@@ -594,60 +600,92 @@ void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int verte
glDrawArrays(glprim[prim], 0, numTrans);
}
glDisableVertexAttribArray(program->a_position);
if (useTexCoord && program->a_texcoord != -1) glDisableVertexAttribArray(program->a_texcoord);
if (program->a_texcoord != -1) glDisableVertexAttribArray(program->a_texcoord);
if (program->a_color0 != -1) glDisableVertexAttribArray(program->a_color0);
if (program->a_color1 != -1) glDisableVertexAttribArray(program->a_color1);
}
struct GlTypeInfo {
GLuint type;
int count;
GLboolean normalized;
};
// This is the software transform pipeline, which is necessary for supporting RECT
// primitives correctly. Other primitives are possible to transform and light in hardware
// using vertex shader, which will be way, way faster, especially on mobile. This has
// not yet been implemented though.
void GLES_GPU::TransformAndDrawPrim(void *verts, void *inds, int prim, int vertexCount, float *customUV, int forceIndexType, int *bytesRead)
{
int indexLowerBound, indexUpperBound;
// First, decode the verts and apply morphing
VertexDecoder dec;
dec.SetVertexType(gstate.vertType);
dec.DecodeVerts(decoded, verts, inds, prim, vertexCount, &indexLowerBound, &indexUpperBound);
if (bytesRead)
*bytesRead = vertexCount * dec.VertexSize();
const GlTypeInfo GLComp[8] = {
{0}, // DEC_NONE,
{GL_FLOAT, 1, GL_FALSE}, // DEC_FLOAT_1,
{GL_FLOAT, 2, GL_FALSE}, // DEC_FLOAT_2,
{GL_FLOAT, 3, GL_FALSE}, // DEC_FLOAT_3,
{GL_FLOAT, 4, GL_FALSE}, // DEC_FLOAT_4,
{GL_BYTE, 3, GL_TRUE}, // DEC_S8_3,
{GL_SHORT, 3, GL_TRUE},// DEC_S16_3,
{GL_BYTE, 4, GL_TRUE},// DEC_U8_4,
};
// And here we should return, having collected the morphed but untransformed vertices.
// Note that DecodeVerts should convert strips into indexed lists etc, adding to our
// current vertex buffer and index buffer.
static inline void VertexAttribSetup(int attrib, int fmt, int stride, u8 *ptr) {
if (attrib != -1 && fmt) {
const GlTypeInfo &type = GLComp[fmt];
glEnableVertexAttribArray(attrib);
glVertexAttribPointer(attrib, type.count, type.type, type.normalized, stride, ptr);
// The rest below here should only execute on Flush.
#if 0
for (int i = indexLowerBound; i <= indexUpperBound; i++) {
PrintDecodedVertex(decoded[i], gstate.vertType);
}
}
static inline void VertexAttribDisable(int attrib, int fmt) {
if (attrib != -1 && fmt) {
glDisableVertexAttribArray(attrib);
#endif
bool useTexCoord = false;
// Check if anything needs updating
if (gstate_c.textureChanged)
{
if ((gstate.textureMapEnable & 1) && !gstate.isModeClear())
{
PSPSetTexture();
useTexCoord = true;
}
}
}
gpuStats.numDrawCalls++;
gpuStats.numVertsTransformed += vertexCount;
// TODO: Use VBO and get rid of the vertexData pointers - with that, we will supply only offsets
static void SetupDecFmtForDraw(LinkedShader *program, const DecVtxFormat &decFmt, u8 *vertexData) {
VertexAttribSetup(program->a_weight0123, decFmt.w0fmt, decFmt.stride, vertexData + decFmt.w0off);
VertexAttribSetup(program->a_weight4567, decFmt.w1fmt, decFmt.stride, vertexData + decFmt.w1off);
VertexAttribSetup(program->a_texcoord, decFmt.uvfmt, decFmt.stride, vertexData + decFmt.uvoff);
VertexAttribSetup(program->a_color0, decFmt.c0fmt, decFmt.stride, vertexData + decFmt.c0off);
VertexAttribSetup(program->a_color1, decFmt.c1fmt, decFmt.stride, vertexData + decFmt.c1off);
VertexAttribSetup(program->a_normal, decFmt.nrmfmt, decFmt.stride, vertexData + decFmt.nrmoff);
VertexAttribSetup(program->a_position, decFmt.posfmt, decFmt.stride, vertexData + decFmt.posoff);
}
// TODO: This should not be done on every drawcall, we should collect vertex data
// until critical state changes. That's when we draw (flush).
static void DesetupDecFmtForDraw(LinkedShader *program, const DecVtxFormat &decFmt) {
VertexAttribDisable(program->a_weight0123, decFmt.w0fmt);
VertexAttribDisable(program->a_weight4567, decFmt.w1fmt);
VertexAttribDisable(program->a_texcoord, decFmt.uvfmt);
VertexAttribDisable(program->a_color0, decFmt.c0fmt);
VertexAttribDisable(program->a_color1, decFmt.c1fmt);
VertexAttribDisable(program->a_normal, decFmt.nrmfmt);
VertexAttribDisable(program->a_position, decFmt.posfmt);
ApplyDrawState();
UpdateViewportAndProjection();
LinkedShader *program = shaderManager_->ApplyShader(prim);
if (CanUseHardwareTransform(prim)) {
SetupDecFmtForDraw(program, dec.GetDecVtxFmt(), decoded);
bool drawIndexed;
GLuint glIndexType;
int indexType = (gstate.vertType & GE_VTYPE_IDX_MASK);
if (forceIndexType != -1) {
indexType = forceIndexType;
}
int numTrans = vertexCount;
switch (indexType) {
case GE_VTYPE_IDX_8BIT:
drawIndexed = true;
glIndexType = GL_UNSIGNED_BYTE;
break;
case GE_VTYPE_IDX_16BIT:
drawIndexed = true;
glIndexType = GL_UNSIGNED_SHORT;
break;
default:
drawIndexed = false;
break;
}
// NOTICE_LOG(G3D,"DrawPrimitive: %i", numTrans);
if (drawIndexed) {
glDrawElements(glprim[prim], numTrans, glIndexType, (GLvoid *)inds);
} else {
glDrawArrays(glprim[prim], 0, numTrans);
}
DesetupDecFmtForDraw(program, dec.GetDecVtxFmt());
} else {
SoftwareTransformAndDraw(prim, program, forceIndexType, vertexCount, inds, dec.GetDecVtxFmt(), indexLowerBound, indexUpperBound, customUV);
}
}
void GLES_GPU::Flush(int prim) {
+279 -35
View File
@@ -20,6 +20,9 @@
// the transforms ourselves.
#include <stdio.h>
#if defined(_WIN32) && defined(_DEBUG)
#include <windows.h>
#endif
#include "../ge_constants.h"
#include "../GPUState.h"
@@ -36,18 +39,53 @@
static char buffer[16384];
#define WRITE(x, ...) p+=sprintf(p, x "\n" __VA_ARGS__)
#define WRITE p+=sprintf
bool CanUseHardwareTransform(int prim)
{
return !gstate.isModeThrough() && false; // prim != GE_PRIM_RECTANGLES;
// return !gstate.isModeThrough() && prim != GE_PRIM_RECTANGLES;
}
// prim so we can special case for RECTANGLES :(
void ComputeVertexShaderID(VertexShaderID *id, int prim)
{
int doTexture = (gstate.textureMapEnable & 1) && !(gstate.clearmode & 1);
bool hasColor = (gstate.vertType & GE_VTYPE_COL_MASK) != 0;
bool hasNormal = (gstate.vertType & GE_VTYPE_NRM_MASK) != 0;
bool hasBones = (gstate.vertType & GE_VTYPE_WEIGHT_MASK) != 0;
int shadeLight0 = gstate.getUVGenMode() == 2 ? gstate.getUVLS0() : -1;
int shadeLight1 = gstate.getUVGenMode() == 2 ? gstate.getUVLS1() : -1;
memset(id->d, 0, sizeof(id->d));
id->d[0] = gstate.lmode & 1;
id->d[0] |= ((int)gstate.isModeThrough()) << 1;
id->d[0] |= ((int)gstate.isFogEnabled()) << 2;
id->d[0] |= doTexture << 3;
id->d[0] |= (hasColor & 1) << 4;
if (CanUseHardwareTransform(prim)) {
id->d[0] |= 1 << 8;
id->d[0] |= (hasNormal & 1) << 9;
id->d[0] |= (hasBones & 1) << 10;
// UV generation mode
id->d[0] |= gstate.getUVGenMode() << 16;
// The next bits are used differently depending on UVgen mode
if (gstate.getUVGenMode() == 1) {
id->d[0] |= gstate.getUVProjMode() << 18;
} else if (gstate.getUVGenMode() == 2) {
id->d[0] |= gstate.getUVLS0() << 18;
id->d[0] |= gstate.getUVLS1() << 20;
}
// Bones
id->d[0] |= (gstate.getNumBoneWeights() - 1) << 22;
// Light bits
}
// Bits that we will need:
// lightenable * 4
@@ -62,56 +100,262 @@ void WriteLight(char *p, int l) {
// TODO
}
char *GenerateVertexShader()
const char *boneWeightAttrDecl[8] = {
"#ERROR",
"attribute vec2 a_weight0123;\n",
"attribute vec3 a_weight0123;\n",
"attribute vec4 a_weight0123;\n",
"attribute vec4 a_weight0123;\nattribute float a_weight4567;\n",
"attribute vec4 a_weight0123;\nattribute vec2 a_weight4567;\n",
"attribute vec4 a_weight0123;\nattribute vec3 a_weight4567;\n",
"attribute vec4 a_weight0123;\nattribute vec4 a_weight4567;\n",
};
const char *boneWeightAttr[8] = {
"a_weight0123.x",
"a_weight0123.y",
"a_weight0123.z",
"a_weight0123.w",
"a_weight4567.x",
"a_weight4567.y",
"a_weight4567.z",
"a_weight4567.w",
};
enum DoLightComputation {
LIGHT_OFF,
LIGHT_DOTONLY,
LIGHT_FULL,
};
char *GenerateVertexShader(int prim)
{
char *p = buffer;
#if defined(USING_GLES2)
WRITE("precision highp float;");
WRITE(p, "precision highp float;\n");
#elif !defined(FORCE_OPENGL_2_0)
WRITE("#version 130");
WRITE(p, "#version 130\n");
#endif
int lmode = gstate.lmode & 1;
int doTexture = (gstate.textureMapEnable & 1) && !(gstate.clearmode & 1);
WRITE("attribute vec3 a_position;");
if (doTexture)
WRITE("attribute vec2 a_texcoord;");
WRITE("attribute vec4 a_color0;");
if (lmode)
WRITE("attribute vec4 a_color1;");
bool hwXForm = CanUseHardwareTransform(prim);
bool hasColor = (gstate.vertType & GE_VTYPE_COL_MASK) != 0 || !hwXForm;
bool hasNormal = (gstate.vertType & GE_VTYPE_NRM_MASK) != 0 && hwXForm;
DoLightComputation doLight[4] = {LIGHT_OFF, LIGHT_OFF, LIGHT_OFF, LIGHT_OFF};
if (hwXForm) {
int shadeLight0 = gstate.getUVGenMode() == 2 ? gstate.getUVLS0() : -1;
int shadeLight1 = gstate.getUVGenMode() == 2 ? gstate.getUVLS1() : -1;
for (int i = 0; i < 4; i++) {
if (!hasNormal)
continue;
if (i == shadeLight0 || i == shadeLight1)
doLight[i] = LIGHT_DOTONLY;
if ((gstate.lightingEnable & 1) && (gstate.lightEnable[i] & 1))
doLight[i] = LIGHT_FULL;
}
}
if ((gstate.vertType & GE_VTYPE_WEIGHT_MASK) != GE_VTYPE_WEIGHT_NONE) {
WRITE(p, "%s", boneWeightAttrDecl[gstate.getNumBoneWeights() - 1]);
}
WRITE(p, "attribute vec3 a_position;\n");
if (doTexture) WRITE(p, "attribute vec2 a_texcoord;\n");
if (hasColor) {
WRITE(p, "attribute vec4 a_color0;\n");
if (lmode && !hwXForm) // only software transform supplies color1 as vertex data
WRITE(p, "attribute vec3 a_color1;\n");
}
if (hwXForm && hasNormal)
WRITE(p, "attribute vec3 a_normal;\n");
if (gstate.isModeThrough()) {
WRITE("uniform mat4 u_proj_through;");
WRITE(p, "uniform mat4 u_proj_through;\n");
} else {
WRITE("uniform mat4 u_proj;");
WRITE(p, "uniform mat4 u_proj;\n");
// Add all the uniforms we'll need to transform properly.
}
WRITE("varying vec4 v_color0;");
if (lmode)
WRITE("varying vec4 v_color1;");
if (doTexture)
WRITE("varying vec2 v_texcoord;");
if (gstate.isFogEnabled())
WRITE("varying float v_depth;");
WRITE("void main() {");
WRITE(" v_color0 = a_color0;");
if (lmode)
WRITE(" v_color1 = a_color1;");
if (doTexture)
WRITE(" v_texcoord = a_texcoord;");
if (gstate.isModeThrough()) {
WRITE(" gl_Position = u_proj_through * vec4(a_position, 1.0);");
} else {
WRITE(" gl_Position = u_proj * vec4(a_position, 1.0);");
}
if (gstate.isFogEnabled()) {
WRITE(" v_depth = gl_Position.z;");
}
WRITE("}");
if (hwXForm || !hasColor)
WRITE(p, "uniform vec4 u_matambientalpha;\n"); // matambient + matalpha
if (hwXForm) {
// When transforming by hardware, we need a great deal more uniforms...
WRITE(p, "uniform mat4x3 u_world;\n");
WRITE(p, "uniform mat4x3 u_view;\n");
if (gstate.getUVGenMode() == 0)
WRITE(p, "uniform vec4 u_uvscaleoffset;\n");
else if (gstate.getUVGenMode() == 1)
WRITE(p, "uniform mat4x3 u_texmtx;\n");
if ((gstate.vertType & GE_VTYPE_WEIGHT_MASK) != GE_VTYPE_WEIGHT_NONE) {
int numBones = 1 + ((gstate.vertType & GE_VTYPE_WEIGHTCOUNT_MASK) >> GE_VTYPE_WEIGHTCOUNT_SHIFT);
for (int i = 0; i < numBones; i++) {
WRITE(p, "uniform mat4x3 u_bone%i;\n", i);
}
}
}
WRITE(p, "varying vec4 v_color0;\n");
if (lmode) WRITE(p, "varying vec3 v_color1;\n");
if (doTexture) WRITE(p, "varying vec2 v_texcoord;\n");
if (gstate.isFogEnabled()) WRITE(p, "varying float v_depth;\n");
WRITE(p, "void main() {\n");
if (!hwXForm) {
// Simple pass-through of vertex data to fragment shader
if (doTexture)
WRITE(p, " v_texcoord = a_texcoord;\n");
if (hasColor) {
WRITE(p, " v_color0 = a_color0;\n");
if (lmode)
WRITE(p, " v_color1 = a_color1;\n");
} else {
WRITE(p, " v_color0 = u_matambientalpha;\n");
if (lmode)
WRITE(p, " v_color1 = vec3(0.0, 0.0, 0.0);\n");
}
if (gstate.isModeThrough()) {
WRITE(p, " gl_Position = u_proj_through * vec4(a_position, 1.0);\n");
} else {
WRITE(p, " gl_Position = u_proj * vec4(a_position, 1.0);\n");
}
} else {
// This is the real deal.
// Step 1: World Transform / Skinning
if ((gstate.vertType & GE_VTYPE_WEIGHT_MASK) == GE_VTYPE_WEIGHT_NONE) {
// No skinning, just standard T&L.
WRITE(p, " vec3 worldpos = u_world * vec4(a_position, 1.0);\n");
if (hasNormal)
WRITE(p, " vec3 worldnormal = u_world * vec4(a_normal, 0.0);\n");
} else {
WRITE(p, " vec3 worldpos = vec3(0.0, 0.0, 0.0);\n");
if (hasNormal)
WRITE(p, " vec3 worldnormal = vec3(0.0, 0.0, 0.0);\n");
int numWeights = 1 + ((gstate.vertType & GE_VTYPE_WEIGHTCOUNT_MASK) >> GE_VTYPE_WEIGHTCOUNT_SHIFT);
for (int i = 0; i < numWeights; i++) {
WRITE(p, " worldpos += %s * (u_bone%i * vec4(a_position, 1.0));\n", boneWeightAttr[i], i);
if (hasNormal)
WRITE(p, " worldnormal += %s * (u_bone%i * vec4(a_normal, 0.0));\n", boneWeightAttr[i], i);
}
// Finally, multiply by world matrix (yes, we have to).
WRITE(p, " worldpos = u_world * vec4(worldpos, 1.0);\n");
if (hasNormal)
WRITE(p, " worldnormal = u_world * vec4(worldnormal, 0.0);\n");
}
// Step 2: Color/Lighting
if (hasColor) {
WRITE(p, " vec4 unlitColor = a_color0;\n");
} else {
WRITE(p, " vec4 unlitColor(1.0, 1.0, 1.0, 1.0);\n");
}
// TODO: Declare variables for dots for shade mapping if needed.
const char *ambient = (gstate.materialupdate & 1) ? "a_color0" : "u_ambientalpha";
const char *diffuse = (gstate.materialupdate & 2) ? "a_color0" : "u_matdiffuse";
const char *specular = (gstate.materialupdate & 4) ? "a_color0" : "u_matspecular";
if (gstate.lightingEnable & 1) {
WRITE(p, " vec4 lightSum0 = vec4(0.0);\n");
WRITE(p, " vec3 lightSum1 = vec3(0.0);\n");
}
/*
// Calculate lights if needed. If shade mapping is enabled, lights may need to be
// at least partially calculated
for (int i = 0; i < 4; i++) {
if (doLight[i] == LIGHT_OFF)
continue;
GELightComputation comp = (GELightComputation)(gstate.ltype[i] & 3);
GELightType type = (GELightType)((gstate.ltype[i] >> 8) & 3);
if (type == GE_LIGHTTYPE_DIRECTIONAL)
WRITE(p, " vec3 toLight%i = u_lightpos%i;\n", i, i);
else
WRITE(p, " vec3 toLight%i = u_lightpos%i - worldpos;\n", i, i);
bool doSpecular = (comp != GE_LIGHTCOMP_ONLYDIFFUSE);
bool poweredDiffuse = comp == GE_LIGHTCOMP_BOTHWITHPOWDIFFUSE;
WRITE(p, " float dot%i = dot(tolight, worldnormal);");
if (doLight[i] == LIGHT_DOTONLY)
continue; // Actually, might want specular dot.... TODO
}*/
if (false && (gstate.lightingEnable & 1)) {
// Sum up ambient, emissive here.
WRITE(p, " v_color0 = lightSum0 + u_ambient * %s + u_matemissive;\n", ambient);
if (lmode) {
WRITE(p, " v_color1 = lightSum1;\n");
} else {
WRITE(p, " v_color0 += vec4(lightSum1, 0.0);\n");
}
} else {
// Lighting doesn't affect color.
if (hasColor) {
WRITE(p, " v_color0 = unlitColor;\n");
} else {
WRITE(p, " v_color0 = u_matambientalpha;\n");
}
if (lmode)
WRITE(p, " v_color1 = vec3(0.0, 0.0, 0.0);\n");
}
// Step 3: UV generation
if (doTexture) {
switch (gstate.getUVGenMode()) {
case 0: // Scale-offset. Easy.
WRITE(p, " v_texcoord = a_texcoord * u_uvscaleoffset.xy + u_uvscaleoffset.zw;\n");
break;
case 1: // Projection mapping.
switch (gstate.getUVProjMode()) {
case 0: // Use model space XYZ as source
WRITE(p, " vec3 temp_tc = a_position;\n");
break;
case 1: // Use unscaled UV as source
WRITE(p, " vec3 temp_tc = vec3(a_texcoord.xy, 0.0);\n");
break;
case 2: // Use normalized transformed normal as source
WRITE(p, " vec3 temp_tc = normalize(v_normal);\n");
break;
case 3: // Use non-normalized transformed normal as source
WRITE(p, " vec3 temp_tc = normalize(v_normal);\n");
break;
}
// Transform by texture matrix
WRITE(p, " v_texcoord = (u_texmtx * temp_tc).xy;\n");
break;
case 2: // Shade mapping - use dots from light sources.
WRITE(p, " v_texcoord = vec2(dot%i, dot%i);\n", gstate.getUVLS0(), gstate.getUVLS1());
break;
case 3:
// ILLEGAL
break;
}
}
// Step 4: Final view and projection transforms.
WRITE(p, " gl_Position = u_proj * vec4(u_view * vec4(worldpos, 1.0), 1.0);\n");
}
if (gstate.isFogEnabled())
WRITE(p, " v_depth = gl_Position.z;\n");
WRITE(p, "}\n");
// DEBUG_LOG(HLE, "\n%s", buffer);
#if defined(_WIN32) && defined(_DEBUG)
OutputDebugString(buffer);
#endif
return buffer;
}
+3 -1
View File
@@ -46,7 +46,9 @@ struct VertexShaderID
}
};
bool CanUseHardwareTransform(int prim);
void ComputeVertexShaderID(VertexShaderID *id, int prim);
// The return value is only valid until the function is called again.
char *GenerateVertexShader();
char *GenerateVertexShader(int prim);
+11 -2
View File
@@ -252,9 +252,18 @@ struct GPUgstate
bool isDepthWriteEnabled() const { return !(zmsk & 1); }
int getDepthTestFunc() const { return ztestfunc & 0x7; }
bool isFogEnabled() const { return fogEnable & 1; }
};
// Real data in the context ends here
int getUVGenMode() const { return texmapmode & 3;} // 2 bits
int getUVProjMode() const { return (texmapmode >> 8) & 3;} // 2 bits
int getUVLS0() const { return texshade & 0x3; } // 2 bits
int getUVLS1() const { return (texshade >> 8) & 0x3; } // 2 bits
int getNumBoneWeights() const {
return 1 + ((vertType & GE_VTYPE_WEIGHTCOUNT_MASK) >> GE_VTYPE_WEIGHTCOUNT_SHIFT);
}
// Real data in the context ends here
};
// The rest is cached simplified/converted data for fast access.
// Does not need to be saved when saving/restoring context.
struct GPUStateCache