From bb7fb685316c973e5cf1c1763299d4eef0e1ec4e Mon Sep 17 00:00:00 2001 From: aap Date: Sat, 20 Feb 2021 00:02:51 +0100 Subject: [PATCH] opengl default pipe: dont do unnecessary alpha test --- src/gl/gl3device.cpp | 14 +++++++++++++- src/gl/gl3render.cpp | 7 +++++-- src/gl/rwgl3.h | 2 +- src/gl/shaders/header.frag | 2 ++ src/gl/shaders/header_fs.inc | 2 ++ 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/gl/gl3device.cpp b/src/gl/gl3device.cpp index 1e511bbd..6d1f95aa 100644 --- a/src/gl/gl3device.cpp +++ b/src/gl/gl3device.cpp @@ -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; @@ -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(); @@ -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; } diff --git a/src/gl/gl3render.cpp b/src/gl/gl3render.cpp index fadbb45a..011475b8 100644 --- a/src/gl/gl3render.cpp +++ b/src/gl/gl3render.cpp @@ -149,8 +149,6 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header) InstanceData *inst = header->inst; int32 n = header->numMeshes; - defaultShader->use(); - while(n--){ m = inst->material; @@ -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++; } diff --git a/src/gl/rwgl3.h b/src/gl/rwgl3.h index 4d323302..f6e5b3bd 100644 --- a/src/gl/rwgl3.h +++ b/src/gl/rwgl3.h @@ -97,7 +97,7 @@ struct InstanceDataHeader : rw::InstanceDataHeader struct Shader; -extern Shader *defaultShader; +extern Shader *defaultShader, *defaultShader_noAT; struct Im3DVertex { diff --git a/src/gl/shaders/header.frag b/src/gl/shaders/header.frag index da58bba7..ba50165a 100644 --- a/src/gl/shaders/header.frag +++ b/src/gl/shaders/header.frag @@ -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 } diff --git a/src/gl/shaders/header_fs.inc b/src/gl/shaders/header_fs.inc index b0284985..802bb4f7 100644 --- a/src/gl/shaders/header_fs.inc +++ b/src/gl/shaders/header_fs.inc @@ -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" ;