Skip to content

Commit

Permalink
gl optimization and im2d shader override
Browse files Browse the repository at this point in the history
  • Loading branch information
aap committed Aug 11, 2020
1 parent 113d76c commit 5e299fb
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 42 deletions.
35 changes: 34 additions & 1 deletion src/gl/gl3device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ struct UniformObject
RGBAf lightColor[MAX_LIGHTS];
};

struct GLShaderState
{
RGBA matColor;
SurfaceProperties surfProps;
};

const char *shaderDecl330 = "#version 330\n";
const char *shaderDecl100es =
"#version 100\n"\
Expand Down Expand Up @@ -129,6 +135,7 @@ static GLuint whitetex;
static UniformState uniformState;
static UniformScene uniformScene;
static UniformObject uniformObject;
static GLShaderState shaderState;

#ifndef RW_GL_USE_UBOS
// State
Expand Down Expand Up @@ -878,13 +885,39 @@ setViewMatrix(float32 *mat)

Shader *lastShaderUploaded;

#define U(i) currentShader->uniformLocations[i]

void
setMaterial(const RGBA &color, const SurfaceProperties &surfaceprops)
{
bool force = lastShaderUploaded != currentShader;
if(force || !equal(shaderState.matColor, color)){
rw::RGBAf col;
convColor(&col, &color);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);
shaderState.matColor = color;
}

if(force ||
shaderState.surfProps.ambient != surfaceprops.ambient ||
shaderState.surfProps.specular != surfaceprops.specular ||
shaderState.surfProps.diffuse != surfaceprops.diffuse){
float surfProps[4];
surfProps[0] = surfaceprops.ambient;
surfProps[1] = surfaceprops.specular;
surfProps[2] = surfaceprops.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U(u_surfProps), 1, surfProps);
shaderState.surfProps = surfaceprops;
}
}

void
flushCache(void)
{
flushGlRenderState();

#ifndef RW_GL_USE_UBOS
#define U(i) currentShader->uniformLocations[i]

// TODO: this is probably a stupid way to do it without UBOs
if(lastShaderUploaded != currentShader){
Expand Down
13 changes: 11 additions & 2 deletions src/gl/gl3immed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ uint32 im2DVbo, im2DIbo;
#ifdef RW_GL_USE_VAOS
uint32 im2DVao;
#endif

Shader *im2dOverrideShader;

static int32 u_xform;

#define STARTINDICES 10000
Expand Down Expand Up @@ -130,7 +133,10 @@ im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
xform[2] = -1.0f;
xform[3] = 1.0f;

im2dShader->use();
if(im2dOverrideShader)
im2dOverrideShader->use();
else
im2dShader->use();
#ifndef RW_GL_USE_VAOS
setAttribPointers(im2dattribDesc, 3);
#endif
Expand Down Expand Up @@ -170,7 +176,10 @@ im2DRenderIndexedPrimitive(PrimitiveType primType,
xform[2] = -1.0f;
xform[3] = 1.0f;

im2dShader->use();
if(im2dOverrideShader)
im2dOverrideShader->use();
else
im2dShader->use();
#ifndef RW_GL_USE_VAOS
setAttribPointers(im2dattribDesc, 3);
#endif
Expand Down
22 changes: 2 additions & 20 deletions src/gl/gl3matfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,11 @@ void
matfxDefaultRender(InstanceDataHeader *header, InstanceData *inst)
{
Material *m;
RGBAf col;
GLfloat surfProps[4];
m = inst->material;

defaultShader->use();

convColor(&col, &m->color);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);

surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U(u_surfProps), 1, surfProps);
setMaterial(m->color, m->surfaceProps);

setTexture(0, m->texture);

Expand Down Expand Up @@ -128,8 +119,6 @@ void
matfxEnvRender(InstanceDataHeader *header, InstanceData *inst, MatFX::Env *env)
{
Material *m;
RGBAf col;
GLfloat surfProps[4];
m = inst->material;

if(env->tex == nil || env->coefficient == 0.0f){
Expand All @@ -143,14 +132,7 @@ matfxEnvRender(InstanceDataHeader *header, InstanceData *inst, MatFX::Env *env)
setTexture(1, env->tex);
uploadEnvMatrix(env->frame);

convColor(&col, &m->color);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);

surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U(u_surfProps), 1, surfProps);
setMaterial(m->color, m->surfaceProps);

float fxparams[2];
fxparams[0] = env->coefficient;
Expand Down
9 changes: 1 addition & 8 deletions src/gl/gl3render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,7 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
while(n--){
m = inst->material;

convColor(&col, &m->color);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);

surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U(u_surfProps), 1, surfProps);
setMaterial(m->color, m->surfaceProps);

setTexture(0, m->texture);

Expand Down
7 changes: 6 additions & 1 deletion src/gl/gl3shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ printShaderSource(const char **src)
bool printline;
int line = 1;
for(f = 0; src[f]; f++){
int fileline = 1;
char c;
file = src[f];
printline = true;
while(c = *file++, c != '\0'){
if(printline)
printf("%.4d: ", line++);
printf("%.4d/%d:%.4d: ", line++, f, fileline++);
putchar(c);
printline = c == '\n';
}
Expand Down Expand Up @@ -243,6 +244,10 @@ Shader::create(const char **vsrc, const char **fsrc)
glUniform1i(loc, i);
}

// reset program
if(currentShader)
glUseProgram(currentShader->program);

return sh;
}

Expand Down
11 changes: 1 addition & 10 deletions src/gl/gl3skin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ void
skinRenderCB(Atomic *atomic, InstanceDataHeader *header)
{
Material *m;
RGBAf col;
GLfloat surfProps[4];

setWorldMatrix(atomic->getFrame()->getLTM());
lightingCB(atomic);
Expand All @@ -306,14 +304,7 @@ skinRenderCB(Atomic *atomic, InstanceDataHeader *header)
while(n--){
m = inst->material;

convColor(&col, &m->color);
glUniform4fv(U(u_matColor), 1, (GLfloat*)&col);

surfProps[0] = m->surfaceProps.ambient;
surfProps[1] = m->surfaceProps.specular;
surfProps[2] = m->surfaceProps.diffuse;
surfProps[3] = 0.0f;
glUniform4fv(U(u_surfProps), 1, surfProps);
setMaterial(m->color, m->surfaceProps);

setTexture(0, m->texture);

Expand Down
3 changes: 3 additions & 0 deletions src/gl/rwgl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ extern const char *shaderDecl; // #version stuff
extern const char *header_vert_src;
extern const char *header_frag_src;

extern Shader *im2dOverrideShader;

// per Scene
void setProjectionMatrix(float32*);
void setViewMatrix(float32*);
Expand All @@ -180,6 +182,7 @@ int32 setLights(WorldLights *lightData);

// per Mesh
void setTexture(int32 n, Texture *tex);
void setMaterial(const RGBA &color, const SurfaceProperties &surfaceprops);

void setAlphaBlend(bool32 enable);
bool32 getAlphaBlend(void);
Expand Down
61 changes: 61 additions & 0 deletions tools/playground/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,65 @@ setupClump(rw::Clump *clump)
}
}

#define MUL(x, y) ((x)*(y)/255)

int
calcVCfx(int fb, int col, int a, int iter)
{
int prev = fb;
int col2 = col*2;
if(col2 > 255) col2 = 255;
for(int i = 0; i < iter; i++){
int tmp = MUL(fb, 255-a) + MUL(MUL(prev, col2), a);
tmp += MUL(prev, col);
tmp += MUL(prev, col);
prev = tmp > 255 ? 255 : tmp;
}
return prev;
}

int
calcIIIfx(int fb, int col, int a, int iter)
{
int prev = fb;
for(int i = 0; i < iter; i++){
int tmp = MUL(fb, 255-a) + MUL(MUL(prev, col), a);
prev = tmp > 255 ? 255 : tmp;
}
return prev;
}

void
postfxtest(void)
{
rw::Image *img = rw::Image::create(256, 256, 32);
img->allocate();
int x, y;
int iter;
static char filename[100];
for(iter = 0; iter < 10; iter++){
for(y = 0; y < 256; y++)
for(x = 0; x < 256; x++){
int res = calcVCfx(y, x, 30, iter);
// int res = calcIIIfx(y, x, 30, iter);
if(0 && res == y){
img->pixels[y*img->stride + x*img->bpp + 0] = 255;
img->pixels[y*img->stride + x*img->bpp + 1] = 0;
img->pixels[y*img->stride + x*img->bpp + 2] = 0;
}else{
img->pixels[y*img->stride + x*img->bpp + 0] = res;
img->pixels[y*img->stride + x*img->bpp + 1] = res;
img->pixels[y*img->stride + x*img->bpp + 2] = res;
}
img->pixels[y*img->stride + x*img->bpp + 3] = 255;
}
sprintf(filename, "vcfx_%02d.bmp", iter);
// sprintf(filename, "iiifx_%02d.bmp", iter);
rw::writeBMP(img, filename);
}
exit(0);
}

bool
InitRW(void)
{
Expand All @@ -184,6 +243,8 @@ InitRW(void)

rw::d3d::isP8supported = false;

postfxtest();

initFont();

rw::RGBA foreground = { 255, 255, 0, 255 };
Expand Down

0 comments on commit 5e299fb

Please sign in to comment.