1
0
mirror of https://github.com/blawar/GLideN64.git synced 2024-06-25 22:09:35 +00:00

Refactor: replace custom binary tree implementation by std::map in Combiner.cpp

Removed many auxilary structs. Code is simplified.
This commit is contained in:
Sergey Lipskiy 2014-09-02 19:30:04 +07:00
parent bfc3d6c758
commit 9d692cc7ae
5 changed files with 112 additions and 167 deletions

View File

@ -105,46 +105,55 @@ static DWORD64 ACEncodeD[] =
void CombinerInfo::init()
{
InitGLSLCombiner();
root = NULL;
current = NULL;
m_pCurrent = NULL;
}
void CombinerInfo::destroy()
{
DestroyGLSLCombiner();
m_pCurrent = NULL;
for (Combiners::iterator cur = m_combiners.begin(); cur != m_combiners.end(); ++cur)
delete cur->second;
m_combiners.clear();
}
void CombinerInfo::updateCombineColors()
{
current->compiled->UpdateColors();
m_pCurrent->UpdateColors();
gDP.changed &= ~CHANGED_COMBINE_COLORS;
}
void CombinerInfo::updateCombineFBInfo()
{
current->compiled->UpdateFBInfo(true);
m_pCurrent->UpdateFBInfo(true);
gDP.changed &= ~CHANGED_FB_TEXTURE;
}
void CombinerInfo::updateCombineDepthInfo()
{
if (current != NULL)
current->compiled->UpdateDepthInfo(true);
if (m_pCurrent != NULL)
m_pCurrent->UpdateDepthInfo(true);
}
void CombinerInfo::updateAlphaTestInfo()
{
if (current != NULL)
current->compiled->UpdateAlphaTestInfo();
if (m_pCurrent != NULL)
m_pCurrent->UpdateAlphaTestInfo();
}
void CombinerInfo::updateTextureInfo()
{
if (current != NULL)
current->compiled->UpdateTextureInfo();
if (m_pCurrent != NULL)
m_pCurrent->UpdateTextureInfo();
}
void CombinerInfo::updateRenderState() {
if (current != NULL)
current->compiled->UpdateRenderState();
if (m_pCurrent != NULL)
m_pCurrent->UpdateRenderState();
}
void Combiner_SimplifyCycle( CombineCycle *cc, CombinerStage *stage )
static
void SimplifyCycle( CombineCycle *cc, CombinerStage *stage )
{
// Load the first operand
stage->op[0].op = LOAD;
@ -213,7 +222,7 @@ void Combiner_SimplifyCycle( CombineCycle *cc, CombinerStage *stage )
}
}
CachedCombiner *Combiner_Compile( u64 mux )
ShaderCombiner * CombinerInfo::_compile(u64 mux) const
{
gDPCombine combine;
@ -261,38 +270,30 @@ CachedCombiner *Combiner_Compile( u64 mux )
for (int i = 0; i < numCycles; i++)
{
// Simplify each RDP combiner cycle into a combiner stage
Combiner_SimplifyCycle( &cc[i], &color.stage[i] );
Combiner_SimplifyCycle( &ac[i], &alpha.stage[i] );
SimplifyCycle( &cc[i], &color.stage[i] );
SimplifyCycle( &ac[i], &alpha.stage[i] );
}
CachedCombiner *cached = (CachedCombiner*)malloc( sizeof( CachedCombiner ) );
cached->combine.mux = combine.mux;
cached->left = NULL;
cached->right = NULL;
cached->compiled = new GLSLCombiner( &color, &alpha );
return cached;
return new ShaderCombiner( color, alpha, combine );
}
void Combiner_DeleteCombiner( CachedCombiner *combiner )
void CombinerInfo::setCombine( u64 mux )
{
if (combiner->left) Combiner_DeleteCombiner( combiner->left );
if (combiner->right) Combiner_DeleteCombiner( combiner->right );
delete combiner->compiled;
free( combiner );
}
void CombinerInfo::destroy()
{
DestroyGLSLCombiner();
if (root) {
Combiner_DeleteCombiner( root );
root = NULL;
if (m_pCurrent != NULL && m_pCurrent->getMux() == mux) {
changed = false;
m_pCurrent->Update();
return;
}
Combiners::const_iterator iter = m_combiners.find(mux);
if (iter != m_combiners.end())
m_pCurrent = iter->second;
else {
m_pCurrent = _compile(mux);
m_combiners[mux] = m_pCurrent;
}
m_pCurrent->Update();
changed = true;
gDP.changed |= CHANGED_COMBINE_COLORS;
}
DWORD64 Combiner_EncodeCombineMode( WORD saRGB0, WORD sbRGB0, WORD mRGB0, WORD aRGB0,
@ -305,53 +306,3 @@ DWORD64 Combiner_EncodeCombineMode( WORD saRGB0, WORD sbRGB0, WORD mRGB0, WORD a
((DWORD64)CCEncodeA[saRGB1] << 37) | ((DWORD64)CCEncodeB[sbRGB1] << 24) | ((DWORD64)CCEncodeC[mRGB1] ) | ((DWORD64)CCEncodeD[aRGB1] << 6) |
((DWORD64)ACEncodeA[saA1] << 18) | ((DWORD64)ACEncodeB[sbA1] << 3) | ((DWORD64)ACEncodeC[mA1] << 18) | ((DWORD64)ACEncodeD[aA1] ));
}
void Combiner_SelectCombine( u64 mux )
{
if (CombinerInfo::get().current != NULL && CombinerInfo::get().current->combine.mux == mux) {
CombinerInfo::get().changed = false;
return;
}
CachedCombiner *current = CombinerInfo::get().root;
CachedCombiner *parent = current;
while (current)
{
parent = current;
if (mux == current->combine.mux)
break;
else if (mux < current->combine.mux)
current = current->left;
else
current = current->right;
}
if (current == NULL)
{
current = Combiner_Compile( mux );
if (parent == NULL)
CombinerInfo::get().root = current;
else if (parent->combine.mux > current->combine.mux)
parent->left = current;
else
parent->right = current;
}
CombinerInfo::get().current = current;
CombinerInfo::get().changed = true;
gDP.changed |= CHANGED_COMBINE_COLORS;
}
void Combiner_SetCombineStates()
{
CombinerInfo::get().current->compiled->Set();
}
void CombinerInfo::setCombine( u64 mux )
{
Combiner_SelectCombine( mux );
Combiner_SetCombineStates();
}

View File

@ -1,6 +1,8 @@
#ifndef COMBINER_H
#define COMBINER_H
#include <map>
#include "GLideN64.h"
#include "OpenGL.h"
#include "gDP.h"
@ -111,52 +113,38 @@ struct CombineCycle
int sa, sb, m, a;
};
class OGLCombiner {
public:
virtual void Set() = 0;
virtual void UpdateColors(bool _bForce = false) = 0;
virtual void UpdateFBInfo(bool _bForce = false) = 0;
virtual void UpdateDepthInfo(bool _bForce = false) = 0;
virtual void UpdateAlphaTestInfo(bool _bForce = false) = 0;
virtual void UpdateTextureInfo(bool _bForce = false) = 0;
virtual void UpdateRenderState(bool _bForce = false) = 0;
virtual void UpdateLight(bool _bForce = false) = 0;
};
struct CachedCombiner
{
gDPCombine combine;
OGLCombiner *compiled;
CachedCombiner *left, *right;
};
class ShaderCombiner;
class CombinerInfo
{
public:
void init();
void updateCombineColors();
void updateCombineFBInfo();
void updateCombineDepthInfo();
void updateAlphaTestInfo();
void updateTextureInfo();
void updateRenderState();
void setCombine( u64 mux );
void destroy();
void init();
void updateCombineColors();
void updateCombineFBInfo();
void updateCombineDepthInfo();
void updateAlphaTestInfo();
void updateTextureInfo();
void updateRenderState();
void setCombine( u64 mux );
void destroy();
static CombinerInfo & get() {
static CombinerInfo info;
return info;
}
CachedCombiner *root, *current;
ShaderCombiner * getCurrent() const {return m_pCurrent;}
bool usesT0, usesT1, usesLOD, usesShadeColor, changed;
bool usesT0, usesT1, usesLOD, usesShadeColor, changed;
private:
CombinerInfo() :
root(NULL), current(NULL), usesT0(false), usesT1(false),
m_pCurrent(NULL), usesT0(false), usesT1(false),
usesShadeColor(false), changed(false) {}
ShaderCombiner * _compile(u64 mux) const;
ShaderCombiner * m_pCurrent;
typedef std::map<u64, ShaderCombiner *> Combiners;
Combiners m_combiners;
};
#endif

View File

@ -336,26 +336,27 @@ int CompileCombiner(const CombinerStage & _stage, const char** _Input, char * _f
return nRes;
}
GLSLCombiner::GLSLCombiner(Combiner *_color, Combiner *_alpha) {
ShaderCombiner::ShaderCombiner(Combiner & _color, Combiner & _alpha, const gDPCombine & _combine) : m_combine(_combine)
{
char *fragment_shader = (char*)malloc(1024*5);
strcpy(fragment_shader, fragment_shader_header_common_variables);
char strCombiner[512];
strcpy(strCombiner, " alpha1 = ");
m_nInputs = CompileCombiner(_alpha->stage[0], AlphaInput, strCombiner);
m_nInputs = CompileCombiner(_alpha.stage[0], AlphaInput, strCombiner);
strcat(strCombiner, " color1 = ");
m_nInputs |= CompileCombiner(_color->stage[0], ColorInput, strCombiner);
m_nInputs |= CompileCombiner(_color.stage[0], ColorInput, strCombiner);
strcat(strCombiner, " combined_color = vec4(color1, alpha1); \n");
if (_alpha->numStages == 2) {
if (_alpha.numStages == 2) {
strcat(strCombiner, " alpha2 = ");
CorrectSecondStageParams(_alpha->stage[1]);
m_nInputs |= CompileCombiner(_alpha->stage[1], AlphaInput, strCombiner);
CorrectSecondStageParams(_alpha.stage[1]);
m_nInputs |= CompileCombiner(_alpha.stage[1], AlphaInput, strCombiner);
} else
strcat(strCombiner, " alpha2 = alpha1; \n");
if (_color->numStages == 2) {
if (_color.numStages == 2) {
strcat(strCombiner, " color2 = ");
CorrectSecondStageParams(_color->stage[1]);
m_nInputs |= CompileCombiner(_color->stage[1], ColorInput, strCombiner);
CorrectSecondStageParams(_color.stage[1]);
m_nInputs |= CompileCombiner(_color.stage[1], ColorInput, strCombiner);
} else
strcat(strCombiner, " color2 = color1; \n");
@ -461,7 +462,7 @@ GLSLCombiner::GLSLCombiner(Combiner *_color, Combiner *_alpha) {
_locateUniforms();
}
GLSLCombiner::~GLSLCombiner() {
ShaderCombiner::~ShaderCombiner() {
u32 shaderIndex = 0;
const u32 arraySize = sizeof(m_aShaders)/sizeof(m_aShaders[0]);
while (shaderIndex < arraySize && m_aShaders[shaderIndex] > 0) {
@ -474,7 +475,7 @@ GLSLCombiner::~GLSLCombiner() {
#define LocateUniform(A) \
m_uniforms.A.loc = glGetUniformLocation(m_program, #A);
void GLSLCombiner::_locateUniforms() {
void ShaderCombiner::_locateUniforms() {
LocateUniform(uTex0);
LocateUniform(uTex1);
LocateUniform(uTlutImage);
@ -516,9 +517,9 @@ void GLSLCombiner::_locateUniforms() {
LocateUniform(uFogColor);
LocateUniform(uCenterColor);
LocateUniform(uScaleColor);
LocateUniform(uRenderState);
LocateUniform(uTexScale);
LocateUniform(uTexOffset[0]);
LocateUniform(uTexOffset[1]);
@ -544,7 +545,7 @@ void GLSLCombiner::_locateUniforms() {
}
}
void GLSLCombiner::_locate_attributes() const {
void ShaderCombiner::_locate_attributes() const {
glBindAttribLocation(m_program, SC_POSITION, "aPosition");
glBindAttribLocation(m_program, SC_COLOR, "aColor");
glBindAttribLocation(m_program, SC_TEXCOORD0, "aTexCoord0");
@ -553,7 +554,7 @@ void GLSLCombiner::_locate_attributes() const {
glBindAttribLocation(m_program, SC_NUMLIGHTS, "aNumLights");
}
void GLSLCombiner::Set() {
void ShaderCombiner::Update() {
CombinerInfo::get().usesT0 = (m_nInputs & ((1<<TEXEL0)|(1<<TEXEL0_ALPHA))) != 0;
CombinerInfo::get().usesT1 = (m_nInputs & ((1<<TEXEL1)|(1<<TEXEL1_ALPHA))) != 0;
CombinerInfo::get().usesLOD = (m_nInputs & (1<<LOD_FRACTION)) != 0;
@ -574,11 +575,11 @@ void GLSLCombiner::Set() {
UpdateLight(true);
}
void GLSLCombiner::UpdateRenderState(bool _bForce) {
void ShaderCombiner::UpdateRenderState(bool _bForce) {
_setIUniform(m_uniforms.uRenderState, OGL.renderState, _bForce);
}
void GLSLCombiner::UpdateLight(bool _bForce) {
void ShaderCombiner::UpdateLight(bool _bForce) {
if (config.enableHWLighting == 0)
return;
for (s32 i = 0; i <= gSP.numLights; ++i) {
@ -587,7 +588,7 @@ void GLSLCombiner::UpdateLight(bool _bForce) {
}
}
void GLSLCombiner::UpdateColors(bool _bForce) {
void ShaderCombiner::UpdateColors(bool _bForce) {
_setV4Uniform(m_uniforms.uEnvColor, &gDP.envColor.r, _bForce);
_setV4Uniform(m_uniforms.uPrimColor, &gDP.primColor.r, _bForce);
_setV4Uniform(m_uniforms.uCenterColor, &gDP.key.center.r, _bForce);
@ -612,7 +613,7 @@ void GLSLCombiner::UpdateColors(bool _bForce) {
_setIUniform(m_uniforms.uTextureDetail, gDP.otherMode.textureDetail, _bForce);
}
}
_setIUniform(m_uniforms.uAlphaCompareMode, gDP.otherMode.alphaCompare, _bForce);
_setIUniform(m_uniforms.uAlphaDitherMode, gDP.otherMode.alphaDither, _bForce);
_setIUniform(m_uniforms.uColorDitherMode, gDP.otherMode.colorDither, _bForce);
@ -622,7 +623,7 @@ void GLSLCombiner::UpdateColors(bool _bForce) {
_setFUniform(m_uniforms.uNoiseTime, (float)(rand()&255), _bForce);
}
void GLSLCombiner::UpdateTextureInfo(bool _bForce) {
void ShaderCombiner::UpdateTextureInfo(bool _bForce) {
_setIUniform(m_uniforms.uTexturePersp, gDP.otherMode.texturePersp, _bForce);
_setFV2Uniform(m_uniforms.uTexScale, gSP.texture.scales, gSP.texture.scalet, _bForce);
int nFB0 = 0, nFB1 = 0;
@ -661,7 +662,7 @@ void GLSLCombiner::UpdateTextureInfo(bool _bForce) {
_setFUniform(m_uniforms.uPrimLod, gDP.primColor.l, _bForce);
}
void GLSLCombiner::UpdateFBInfo(bool _bForce) {
void ShaderCombiner::UpdateFBInfo(bool _bForce) {
int nFb8bitMode = 0, nFbFixedAlpha = 0;
if (cache.current[0] != NULL && cache.current[0]->frameBufferTexture == TRUE) {
if (cache.current[0]->size == G_IM_SIZ_8b) {
@ -681,7 +682,7 @@ void GLSLCombiner::UpdateFBInfo(bool _bForce) {
_setIUniform(m_uniforms.uFbFixedAlpha, nFbFixedAlpha, _bForce);
}
void GLSLCombiner::UpdateDepthInfo(bool _bForce) {
void ShaderCombiner::UpdateDepthInfo(bool _bForce) {
if (!OGL.bImageTexture)
return;
@ -702,7 +703,7 @@ void GLSLCombiner::UpdateDepthInfo(bool _bForce) {
_setFUniform(m_uniforms.uDepthTrans, gSP.viewport.vtrans[2]*32768.0f, _bForce);
}
void GLSLCombiner::UpdateAlphaTestInfo(bool _bForce) {
void ShaderCombiner::UpdateAlphaTestInfo(bool _bForce) {
if ((gDP.otherMode.alphaCompare == G_AC_THRESHOLD) && !(gDP.otherMode.alphaCvgSel)) {
_setIUniform(m_uniforms.uEnableAlphaTest, 1, _bForce);
_setFUniform(m_uniforms.uAlphaTestValue, gDP.blendColor.a, _bForce);
@ -741,7 +742,7 @@ void GLSL_RenderDepth() {
glBindTexture(GL_TEXTURE_2D, frameBuffer.top->pDepthBuffer->depth_texture->glName);
// glBindTexture(GL_TEXTURE_2D, g_zlut_tex);
CombinerInfo::get().setCombine( EncodeCombineMode( 0, 0, 0, TEXEL0, 0, 0, 0, 1, 0, 0, 0, TEXEL0, 0, 0, 0, 1 ) );
CombinerInfo::get().setCombine( EncodeCombineMode( 0, 0, 0, TEXEL0, 0, 0, 0, 1, 0, 0, 0, TEXEL0, 0, 0, 0, 1 ) );
glDisable( GL_BLEND );
glDisable( GL_ALPHA_TEST );
@ -767,16 +768,16 @@ void GLSL_RenderDepth() {
glDrawBuffer( GL_BACK );
#endif
glBegin(GL_QUADS);
glTexCoord2f( 0.0f, 0.0f );
glTexCoord2f( 0.0f, 0.0f );
glVertex2f( 0.0f, 0.0f );
glTexCoord2f( 0.0f, v1 );
glVertex2f( 0.0f, (GLfloat)OGL.height );
glTexCoord2f( u1, v1 );
glTexCoord2f( u1, v1 );
glVertex2f( (GLfloat)OGL.width, (GLfloat)OGL.height );
glTexCoord2f( u1, 0.0f );
glTexCoord2f( u1, 0.0f );
glVertex2f( (GLfloat)OGL.width, 0.0f );
glEnd();
#ifdef _WINDOWS

View File

@ -1,18 +1,21 @@
#ifndef GLSL_COMBINER_H
#define GLSL_COMBINER_H
class GLSLCombiner : public OGLCombiner {
#include "gDP.h"
class ShaderCombiner {
public:
GLSLCombiner(Combiner *_color, Combiner *_alpha);
virtual ~GLSLCombiner();
virtual void Set();
virtual void UpdateColors(bool _bForce = false);
virtual void UpdateFBInfo(bool _bForce = false);
virtual void UpdateDepthInfo(bool _bForce = false);
virtual void UpdateAlphaTestInfo(bool _bForce = false);
virtual void UpdateTextureInfo(bool _bForce = false);
virtual void UpdateRenderState(bool _bForce = false);
virtual void UpdateLight(bool _bForce = false);
ShaderCombiner(Combiner & _color, Combiner & _alpha, const gDPCombine & _combine);
~ShaderCombiner();
void Update();
void UpdateColors(bool _bForce = false);
void UpdateFBInfo(bool _bForce = false);
void UpdateDepthInfo(bool _bForce = false);
void UpdateAlphaTestInfo(bool _bForce = false);
void UpdateTextureInfo(bool _bForce = false);
void UpdateRenderState(bool _bForce = false);
void UpdateLight(bool _bForce = false);
u64 getMux() const {return m_combine.mux;}
private:
struct iUniform {GLint loc; int val;};
@ -87,6 +90,7 @@ private:
}
}
gDPCombine m_combine;
UniformLocation m_uniforms;
GLuint m_aShaders[8];
GLuint m_program;

View File

@ -24,6 +24,7 @@
#include "gDP.h"
#include "Textures.h"
#include "Combiner.h"
#include "GLSLCombiner.h"
#include "FrameBuffer.h"
#include "DepthBuffer.h"
#include "VI.h"
@ -766,7 +767,7 @@ void OGL_UpdateStates()
OGL_UpdateCullFace();
if (gSP.changed & CHANGED_LIGHT)
CombinerInfo::get().current->compiled->UpdateLight();
CombinerInfo::get().getCurrent()->UpdateLight();
if (config.frameBufferEmulation.N64DepthCompare) {
glDisable( GL_DEPTH_TEST );
@ -808,7 +809,7 @@ void OGL_UpdateStates()
if ((gSP.changed & CHANGED_TEXTURE) || (gDP.changed & CHANGED_TILE) || (gDP.changed & CHANGED_TMEM))
{
//For some reason updating the texture cache on the first frame of LOZ:OOT causes a NULL Pointer exception...
if (CombinerInfo::get().current != NULL)
if (CombinerInfo::get().getCurrent() != NULL)
{
if (CombinerInfo::get().usesT0)
TextureCache_Update(0);
@ -821,7 +822,7 @@ void OGL_UpdateStates()
TextureCache_Update(1);
else
TextureCache_ActivateDummy(1);
CombinerInfo::get().current->compiled->UpdateTextureInfo(true);
CombinerInfo::get().getCurrent()->UpdateTextureInfo(true);
}
}
@ -959,8 +960,8 @@ void OGL_DrawTriangles()
CombinerInfo::get().updateRenderState();
}
CombinerInfo::get().current->compiled->UpdateColors(true);
CombinerInfo::get().current->compiled->UpdateLight(true);
CombinerInfo::get().getCurrent()->UpdateColors(true);
CombinerInfo::get().getCurrent()->UpdateLight(true);
glDrawElements(GL_TRIANGLES, OGL.triangles.num, GL_UNSIGNED_BYTE, OGL.triangles.elements);
OGL.triangles.num = 0;
@ -1012,7 +1013,7 @@ void OGL_DrawRect( int ulx, int uly, int lrx, int lry, float *color )
glVertexAttrib4f(SC_COLOR, 0, 0, 0, 0);
glVertexAttrib4f(SC_POSITION, 0, 0, (gDP.otherMode.depthSource == G_ZS_PRIM) ? gDP.primDepth.z : gSP.viewport.nearz, 1.0);
glVertexAttribPointer(SC_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(GLVertex), &OGL.rect[0].x);
CombinerInfo::get().current->compiled->UpdateRenderState();
CombinerInfo::get().getCurrent()->UpdateRenderState();
}
if (frameBuffer.drawBuffer != GL_FRAMEBUFFER)
@ -1061,7 +1062,7 @@ void OGL_DrawTexturedRect( float ulx, float uly, float lrx, float lry, float uls
glVertexAttribPointer(SC_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(GLVertex), &OGL.rect[0].x);
glVertexAttribPointer(SC_TEXCOORD0, 2, GL_FLOAT, GL_FALSE, sizeof(GLVertex), &OGL.rect[0].s0);
glVertexAttribPointer(SC_TEXCOORD1, 2, GL_FLOAT, GL_FALSE, sizeof(GLVertex), &OGL.rect[0].s1);
CombinerInfo::get().current->compiled->UpdateRenderState();
CombinerInfo::get().getCurrent()->UpdateRenderState();
}
#ifndef GLES2