Skip to content

Commit

Permalink
opengl default pipe: dont do unnecessary alpha test
Browse files Browse the repository at this point in the history
  • Loading branch information
aap committed Feb 19, 2021
1 parent 276ac82 commit bb7fb68
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/gl/gl3device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ int32 u_lightColor;
int32 u_matColor;
int32 u_surfProps;

Shader *defaultShader;
Shader *defaultShader, *defaultShader_noAT;

static bool32 stateDirty = 1;
static bool32 sceneDirty = 1;
Expand Down Expand Up @@ -1829,6 +1829,9 @@ initOpenGL(void)
const char *fs[] = { shaderDecl, header_frag_src, simple_frag_src, nil };
defaultShader = Shader::create(vs, fs);
assert(defaultShader);
const char *fs_noAT[] = { shaderDecl, "#define NO_ALPHATEST\n", header_frag_src, simple_frag_src, nil };
defaultShader_noAT = Shader::create(vs, fs_noAT);
assert(defaultShader_noAT);

openIm2D();
openIm3D();
Expand All @@ -1841,6 +1844,15 @@ termOpenGL(void)
{
closeIm3D();
closeIm2D();

defaultShader->destroy();
defaultShader = nil;
defaultShader_noAT->destroy();
defaultShader_noAT = nil;

glDeleteTextures(1, &whitetex);
whitetex = nil;

return 1;
}

Expand Down
7 changes: 5 additions & 2 deletions src/gl/gl3render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,6 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
InstanceData *inst = header->inst;
int32 n = header->numMeshes;

defaultShader->use();

while(n--){
m = inst->material;

Expand All @@ -160,6 +158,11 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)

rw::SetRenderState(VERTEXALPHA, inst->vertexAlpha || m->color.alpha != 0xFF);

if(getAlphaTest())
defaultShader->use();
else
defaultShader_noAT->use();

drawInst(header, inst);
inst++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gl/rwgl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct InstanceDataHeader : rw::InstanceDataHeader

struct Shader;

extern Shader *defaultShader;
extern Shader *defaultShader, *defaultShader_noAT;

struct Im3DVertex
{
Expand Down
2 changes: 2 additions & 0 deletions src/gl/shaders/header.frag
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ out vec4 fragColor;

void DoAlphaTest(float a)
{
#ifndef NO_ALPHATEST
if(a < u_alphaRef.x || a >= u_alphaRef.y)
discard;
#endif
}
2 changes: 2 additions & 0 deletions src/gl/shaders/header_fs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const char *header_frag_src =

"void DoAlphaTest(float a)\n"
"{\n"
"#ifndef NO_ALPHATEST\n"
" if(a < u_alphaRef.x || a >= u_alphaRef.y)\n"
" discard;\n"
"#endif\n"
"}\n"
;

0 comments on commit bb7fb68

Please sign in to comment.