From 3c193bb2435643f610e24728fa35959e0e2b42f0 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 5 Oct 2019 13:38:02 +0200 Subject: [PATCH] - moved the animvpx shader to the backend code. This removes the final access to OpenGL from the rest of the source, with the exception of the glFinish call in the swap code. --- source/CMakeLists.txt | 3 + source/build/include/polymost.h | 2 +- source/build/src/animvpx.cpp | 63 +------------------ source/glbackend/gl_shader.h | 2 +- source/glbackend/glbackend.cpp | 35 +++++++++-- source/glbackend/glbackend.h | 2 + .../static/demolition/shaders/glsl/animvpx.fp | 27 ++++++++ .../static/demolition/shaders/glsl/animvpx.vp | 7 +++ 8 files changed, 73 insertions(+), 68 deletions(-) create mode 100644 wadsrc/static/demolition/shaders/glsl/animvpx.fp create mode 100644 wadsrc/static/demolition/shaders/glsl/animvpx.vp diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 4ff73ac3a..55fe28d0a 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -675,6 +675,8 @@ file( GLOB HEADER_FILES libxmp-lite/src/*.h mact/include/*.h mact/src/*.h + common/*.h + common/utility/*.h build/src/*.h duke3d/src/*.h @@ -1018,6 +1020,7 @@ include_directories( libxmp-lite/include/libxmp-lite libsmackerdec/include thirdparty/include + common common/utility common/console platform diff --git a/source/build/include/polymost.h b/source/build/include/polymost.h index 88c433259..a9ce4973e 100644 --- a/source/build/include/polymost.h +++ b/source/build/include/polymost.h @@ -89,7 +89,7 @@ static FORCE_INLINE int32_t eligible_for_tileshades(int32_t const picnum, int32_ static inline float getshadefactor(int32_t const shade) { // 8-bit tiles, i.e. non-hightiles and non-models, don't get additional - // glColor() shading with r_usetileshades! + // shading with r_usetileshades! if (videoGetRenderMode() == REND_POLYMOST && !(globalflags & GLOBAL_NO_GL_TILESHADES) && eligible_for_tileshades(globalpicnum, globalpal)) return 1.f; diff --git a/source/build/src/animvpx.cpp b/source/build/src/animvpx.cpp index 863bfae37..b924d6e74 100644 --- a/source/build/src/animvpx.cpp +++ b/source/build/src/animvpx.cpp @@ -344,69 +344,11 @@ static FHardwareTexture* texture; static int sampler; static int32_t texuploaded; -#ifdef USE_GLEXT -// YUV->RGB conversion fragment shader adapted from -// http://www.fourcc.org/fccyvrgb.php: "Want some sample code?" -// direct link: http://www.fourcc.org/source/YUV420P-OpenGL-GLSLang.c -static const char *fragprog_src = - "#version 120\n" - - "uniform sampler2D tex;\n" - - "void main(void) {\n" - - " float r,g,b,y,u,v;\n" - " vec3 yuv;\n" - - " yuv = texture2D(tex, gl_TexCoord[0].st).rgb;\n" - " y = yuv.r;\n" - " u = yuv.g;\n" - " v = yuv.b;\n" - - " y = 1.1643*(y-0.0625);\n" - " u = u-0.5;\n" - " v = v-0.5;\n" - - " r = y + 1.5958*v;\n" - " g = y - 0.39173*u - 0.81290*v;\n" - " b = y + 2.017*u;\n" - - " gl_FragColor = vec4(r,g,b,1.0);\n" - "}\n"; -#endif - void animvpx_setup_glstate(int32_t animvpx_flags) { - GLint gli; - GLuint FSHandle, PHandle; - static char logbuf[512]; + static char logbuf[512]; - // first, compile the fragment shader - /* Set up program objects. */ - PHandle = glCreateProgram(); - FSHandle = glCreateShader(GL_FRAGMENT_SHADER); - - /* Compile the shader. */ - glShaderSource(FSHandle, 1, (const GLchar **)&fragprog_src, NULL); - glCompileShader(FSHandle); - - /* Print the compilation log. */ - glGetShaderiv(FSHandle, GL_COMPILE_STATUS, &gli); - glGetShaderInfoLog(FSHandle, sizeof(logbuf), NULL, logbuf); - if (logbuf[0]) - OSD_Printf("animvpx compile log: %s\n", logbuf); - - /* Create a complete program object. */ - glAttachShader(PHandle, FSHandle); - glLinkProgram(PHandle); - - /* And print the link log. */ - glGetProgramInfoLog(PHandle, sizeof(logbuf), NULL, logbuf); - if (logbuf[0]) - OSD_Printf("animvpx link log: %s\n", logbuf); - - /* Finally, use the program. */ - glUseProgram(PHandle); + GLInterface.SetVPXShader(); ////////// GL STATE ////////// @@ -444,7 +386,6 @@ void animvpx_setup_glstate(int32_t animvpx_flags) void animvpx_restore_glstate(void) { GLInterface.SetPolymostShader(); - delete texture; texture = nullptr; texuploaded = 0; diff --git a/source/glbackend/gl_shader.h b/source/glbackend/gl_shader.h index ea7de2c1f..a895961da 100644 --- a/source/glbackend/gl_shader.h +++ b/source/glbackend/gl_shader.h @@ -65,4 +65,4 @@ public: SurfaceShader() = default; virtual bool Load(const char* name, const char* vert_prog_lump, const char* fragprog); //, const char * fragprog2, const char *defines); -}; \ No newline at end of file +}; diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp index ce02c1926..cb286424a 100644 --- a/source/glbackend/glbackend.cpp +++ b/source/glbackend/glbackend.cpp @@ -63,6 +63,7 @@ void GLInstance::Init() } new(&renderState) PolymostRenderState; // reset to defaults. LoadSurfaceShader(); + LoadVPXShader(); LoadPolymostShader(); } @@ -70,20 +71,36 @@ void GLInstance::Init() void GLInstance::LoadPolymostShader() { auto fr1 = GetBaseResource("demolition/shaders/glsl/polymost.vp"); - TArray polymost1Vert = fr1.Read(); + TArray Vert = fr1.Read(); fr1 = GetBaseResource("demolition/shaders/glsl/polymost.fp"); - TArray polymost1Frag = fr1.Read(); + TArray Frag = fr1.Read(); // Zero-terminate both strings. - polymost1Vert.Push(0); - polymost1Frag.Push(0); + Vert.Push(0); + Frag.Push(0); polymostShader = new PolymostShader(); - if (!polymostShader->Load("PolymostShader", (const char*)polymost1Vert.Data(), (const char*)polymost1Frag.Data())) + if (!polymostShader->Load("PolymostShader", (const char*)Vert.Data(), (const char*)Frag.Data())) { exit(1); } SetPolymostShader(); } +void GLInstance::LoadVPXShader() +{ + auto fr1 = GetBaseResource("demolition/shaders/glsl/animvpx.vp"); + TArray Vert = fr1.Read(); + fr1 = GetBaseResource("demolition/shaders/glsl/animvpx.fp"); + TArray Frag = fr1.Read(); + // Zero-terminate both strings. + Vert.Push(0); + Frag.Push(0); + vpxShader = new FShader(); + if (!vpxShader->Load("VPXShader", (const char*)Vert.Data(), (const char*)Frag.Data())) + { + exit(1); + } +} + void GLInstance::LoadSurfaceShader() { auto fr1 = GetBaseResource("demolition/shaders/glsl/surface.vp"); @@ -399,6 +416,14 @@ void GLInstance::SetSurfaceShader() } } +void GLInstance::SetVPXShader() +{ + if (activeShader != vpxShader) + { + vpxShader->Bind(); + activeShader = vpxShader; + } +} void PolymostRenderState::Apply(PolymostShader* shader) diff --git a/source/glbackend/glbackend.h b/source/glbackend/glbackend.h index 76405ca8b..8ba9d7966 100644 --- a/source/glbackend/glbackend.h +++ b/source/glbackend/glbackend.h @@ -132,6 +132,7 @@ class GLInstance FShader* activeShader; PolymostShader* polymostShader; SurfaceShader* surfaceShader; + FShader* vpxShader; public: @@ -197,6 +198,7 @@ public: void SetWireframe(bool on); void SetPolymostShader(); void SetSurfaceShader(); + void SetVPXShader(); void ReadPixels(int w, int h, uint8_t* buffer); diff --git a/wadsrc/static/demolition/shaders/glsl/animvpx.fp b/wadsrc/static/demolition/shaders/glsl/animvpx.fp new file mode 100644 index 000000000..b52594941 --- /dev/null +++ b/wadsrc/static/demolition/shaders/glsl/animvpx.fp @@ -0,0 +1,27 @@ +// YUV->RGB conversion fragment shader adapted from +// http://www.fourcc.org/fccyvrgb.php: Want some sample code? +// direct link: http://www.fourcc.org/source/YUV420P-OpenGL-GLSLang.c +#version 120 + +uniform sampler2D tex; + +void main(void) { + + float r,g,b,y,u,v; + vec3 yuv; + + yuv = texture2D(tex, gl_TexCoord[0].st).rgb; + y = yuv.r; + u = yuv.g; + v = yuv.b; + + y = 1.1643*(y-0.0625); + u = u-0.5; + v = v-0.5; + + r = y + 1.5958*v; + g = y - 0.39173*u - 0.81290*v; + b = y + 2.017*u; + + gl_FragColor = vec4(r,g,b,1.0); +}; diff --git a/wadsrc/static/demolition/shaders/glsl/animvpx.vp b/wadsrc/static/demolition/shaders/glsl/animvpx.vp new file mode 100644 index 000000000..971aecab4 --- /dev/null +++ b/wadsrc/static/demolition/shaders/glsl/animvpx.vp @@ -0,0 +1,7 @@ +#version 110 + +void main() +{ + gl_Position = gl_Vertex; + gl_TexCoord[0] = gl_MultiTexCoord0; +}