mirror of https://github.com/aap/librw.git
gl optimization and im2d shader override
This commit is contained in:
parent
113d76cfaa
commit
5e299fb12e
|
@ -98,6 +98,12 @@ struct UniformObject
|
||||||
RGBAf lightColor[MAX_LIGHTS];
|
RGBAf lightColor[MAX_LIGHTS];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct GLShaderState
|
||||||
|
{
|
||||||
|
RGBA matColor;
|
||||||
|
SurfaceProperties surfProps;
|
||||||
|
};
|
||||||
|
|
||||||
const char *shaderDecl330 = "#version 330\n";
|
const char *shaderDecl330 = "#version 330\n";
|
||||||
const char *shaderDecl100es =
|
const char *shaderDecl100es =
|
||||||
"#version 100\n"\
|
"#version 100\n"\
|
||||||
|
@ -129,6 +135,7 @@ static GLuint whitetex;
|
||||||
static UniformState uniformState;
|
static UniformState uniformState;
|
||||||
static UniformScene uniformScene;
|
static UniformScene uniformScene;
|
||||||
static UniformObject uniformObject;
|
static UniformObject uniformObject;
|
||||||
|
static GLShaderState shaderState;
|
||||||
|
|
||||||
#ifndef RW_GL_USE_UBOS
|
#ifndef RW_GL_USE_UBOS
|
||||||
// State
|
// State
|
||||||
|
@ -878,13 +885,39 @@ setViewMatrix(float32 *mat)
|
||||||
|
|
||||||
Shader *lastShaderUploaded;
|
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
|
void
|
||||||
flushCache(void)
|
flushCache(void)
|
||||||
{
|
{
|
||||||
flushGlRenderState();
|
flushGlRenderState();
|
||||||
|
|
||||||
#ifndef RW_GL_USE_UBOS
|
#ifndef RW_GL_USE_UBOS
|
||||||
#define U(i) currentShader->uniformLocations[i]
|
|
||||||
|
|
||||||
// TODO: this is probably a stupid way to do it without UBOs
|
// TODO: this is probably a stupid way to do it without UBOs
|
||||||
if(lastShaderUploaded != currentShader){
|
if(lastShaderUploaded != currentShader){
|
||||||
|
|
|
@ -23,6 +23,9 @@ uint32 im2DVbo, im2DIbo;
|
||||||
#ifdef RW_GL_USE_VAOS
|
#ifdef RW_GL_USE_VAOS
|
||||||
uint32 im2DVao;
|
uint32 im2DVao;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Shader *im2dOverrideShader;
|
||||||
|
|
||||||
static int32 u_xform;
|
static int32 u_xform;
|
||||||
|
|
||||||
#define STARTINDICES 10000
|
#define STARTINDICES 10000
|
||||||
|
@ -130,6 +133,9 @@ im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices)
|
||||||
xform[2] = -1.0f;
|
xform[2] = -1.0f;
|
||||||
xform[3] = 1.0f;
|
xform[3] = 1.0f;
|
||||||
|
|
||||||
|
if(im2dOverrideShader)
|
||||||
|
im2dOverrideShader->use();
|
||||||
|
else
|
||||||
im2dShader->use();
|
im2dShader->use();
|
||||||
#ifndef RW_GL_USE_VAOS
|
#ifndef RW_GL_USE_VAOS
|
||||||
setAttribPointers(im2dattribDesc, 3);
|
setAttribPointers(im2dattribDesc, 3);
|
||||||
|
@ -170,6 +176,9 @@ im2DRenderIndexedPrimitive(PrimitiveType primType,
|
||||||
xform[2] = -1.0f;
|
xform[2] = -1.0f;
|
||||||
xform[3] = 1.0f;
|
xform[3] = 1.0f;
|
||||||
|
|
||||||
|
if(im2dOverrideShader)
|
||||||
|
im2dOverrideShader->use();
|
||||||
|
else
|
||||||
im2dShader->use();
|
im2dShader->use();
|
||||||
#ifndef RW_GL_USE_VAOS
|
#ifndef RW_GL_USE_VAOS
|
||||||
setAttribPointers(im2dattribDesc, 3);
|
setAttribPointers(im2dattribDesc, 3);
|
||||||
|
|
|
@ -72,20 +72,11 @@ void
|
||||||
matfxDefaultRender(InstanceDataHeader *header, InstanceData *inst)
|
matfxDefaultRender(InstanceDataHeader *header, InstanceData *inst)
|
||||||
{
|
{
|
||||||
Material *m;
|
Material *m;
|
||||||
RGBAf col;
|
|
||||||
GLfloat surfProps[4];
|
|
||||||
m = inst->material;
|
m = inst->material;
|
||||||
|
|
||||||
defaultShader->use();
|
defaultShader->use();
|
||||||
|
|
||||||
convColor(&col, &m->color);
|
setMaterial(m->color, m->surfaceProps);
|
||||||
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);
|
|
||||||
|
|
||||||
setTexture(0, m->texture);
|
setTexture(0, m->texture);
|
||||||
|
|
||||||
|
@ -128,8 +119,6 @@ void
|
||||||
matfxEnvRender(InstanceDataHeader *header, InstanceData *inst, MatFX::Env *env)
|
matfxEnvRender(InstanceDataHeader *header, InstanceData *inst, MatFX::Env *env)
|
||||||
{
|
{
|
||||||
Material *m;
|
Material *m;
|
||||||
RGBAf col;
|
|
||||||
GLfloat surfProps[4];
|
|
||||||
m = inst->material;
|
m = inst->material;
|
||||||
|
|
||||||
if(env->tex == nil || env->coefficient == 0.0f){
|
if(env->tex == nil || env->coefficient == 0.0f){
|
||||||
|
@ -143,14 +132,7 @@ matfxEnvRender(InstanceDataHeader *header, InstanceData *inst, MatFX::Env *env)
|
||||||
setTexture(1, env->tex);
|
setTexture(1, env->tex);
|
||||||
uploadEnvMatrix(env->frame);
|
uploadEnvMatrix(env->frame);
|
||||||
|
|
||||||
convColor(&col, &m->color);
|
setMaterial(m->color, m->surfaceProps);
|
||||||
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);
|
|
||||||
|
|
||||||
float fxparams[2];
|
float fxparams[2];
|
||||||
fxparams[0] = env->coefficient;
|
fxparams[0] = env->coefficient;
|
||||||
|
|
|
@ -144,14 +144,7 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
|
||||||
while(n--){
|
while(n--){
|
||||||
m = inst->material;
|
m = inst->material;
|
||||||
|
|
||||||
convColor(&col, &m->color);
|
setMaterial(m->color, m->surfaceProps);
|
||||||
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);
|
|
||||||
|
|
||||||
setTexture(0, m->texture);
|
setTexture(0, m->texture);
|
||||||
|
|
||||||
|
|
|
@ -85,12 +85,13 @@ printShaderSource(const char **src)
|
||||||
bool printline;
|
bool printline;
|
||||||
int line = 1;
|
int line = 1;
|
||||||
for(f = 0; src[f]; f++){
|
for(f = 0; src[f]; f++){
|
||||||
|
int fileline = 1;
|
||||||
char c;
|
char c;
|
||||||
file = src[f];
|
file = src[f];
|
||||||
printline = true;
|
printline = true;
|
||||||
while(c = *file++, c != '\0'){
|
while(c = *file++, c != '\0'){
|
||||||
if(printline)
|
if(printline)
|
||||||
printf("%.4d: ", line++);
|
printf("%.4d/%d:%.4d: ", line++, f, fileline++);
|
||||||
putchar(c);
|
putchar(c);
|
||||||
printline = c == '\n';
|
printline = c == '\n';
|
||||||
}
|
}
|
||||||
|
@ -243,6 +244,10 @@ Shader::create(const char **vsrc, const char **fsrc)
|
||||||
glUniform1i(loc, i);
|
glUniform1i(loc, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reset program
|
||||||
|
if(currentShader)
|
||||||
|
glUseProgram(currentShader->program);
|
||||||
|
|
||||||
return sh;
|
return sh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -282,8 +282,6 @@ void
|
||||||
skinRenderCB(Atomic *atomic, InstanceDataHeader *header)
|
skinRenderCB(Atomic *atomic, InstanceDataHeader *header)
|
||||||
{
|
{
|
||||||
Material *m;
|
Material *m;
|
||||||
RGBAf col;
|
|
||||||
GLfloat surfProps[4];
|
|
||||||
|
|
||||||
setWorldMatrix(atomic->getFrame()->getLTM());
|
setWorldMatrix(atomic->getFrame()->getLTM());
|
||||||
lightingCB(atomic);
|
lightingCB(atomic);
|
||||||
|
@ -306,14 +304,7 @@ skinRenderCB(Atomic *atomic, InstanceDataHeader *header)
|
||||||
while(n--){
|
while(n--){
|
||||||
m = inst->material;
|
m = inst->material;
|
||||||
|
|
||||||
convColor(&col, &m->color);
|
setMaterial(m->color, m->surfaceProps);
|
||||||
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);
|
|
||||||
|
|
||||||
setTexture(0, m->texture);
|
setTexture(0, m->texture);
|
||||||
|
|
||||||
|
|
|
@ -170,6 +170,8 @@ extern const char *shaderDecl; // #version stuff
|
||||||
extern const char *header_vert_src;
|
extern const char *header_vert_src;
|
||||||
extern const char *header_frag_src;
|
extern const char *header_frag_src;
|
||||||
|
|
||||||
|
extern Shader *im2dOverrideShader;
|
||||||
|
|
||||||
// per Scene
|
// per Scene
|
||||||
void setProjectionMatrix(float32*);
|
void setProjectionMatrix(float32*);
|
||||||
void setViewMatrix(float32*);
|
void setViewMatrix(float32*);
|
||||||
|
@ -180,6 +182,7 @@ int32 setLights(WorldLights *lightData);
|
||||||
|
|
||||||
// per Mesh
|
// per Mesh
|
||||||
void setTexture(int32 n, Texture *tex);
|
void setTexture(int32 n, Texture *tex);
|
||||||
|
void setMaterial(const RGBA &color, const SurfaceProperties &surfaceprops);
|
||||||
|
|
||||||
void setAlphaBlend(bool32 enable);
|
void setAlphaBlend(bool32 enable);
|
||||||
bool32 getAlphaBlend(void);
|
bool32 getAlphaBlend(void);
|
||||||
|
|
|
@ -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
|
bool
|
||||||
InitRW(void)
|
InitRW(void)
|
||||||
{
|
{
|
||||||
|
@ -184,6 +243,8 @@ InitRW(void)
|
||||||
|
|
||||||
rw::d3d::isP8supported = false;
|
rw::d3d::isP8supported = false;
|
||||||
|
|
||||||
|
postfxtest();
|
||||||
|
|
||||||
initFont();
|
initFont();
|
||||||
|
|
||||||
rw::RGBA foreground = { 255, 255, 0, 255 };
|
rw::RGBA foreground = { 255, 255, 0, 255 };
|
||||||
|
|
Loading…
Reference in New Issue