Skip to content

Commit

Permalink
Merge pull request #10369 from hrydgard/shader-translate-gl-core
Browse files Browse the repository at this point in the history
Use glslang to translate GLSL 1.x postshaders to GLSL 3.0 or later
  • Loading branch information
hrydgard authored Dec 8, 2017
2 parents 253bba2 + 58fd674 commit 2afb671
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,7 @@ endif()

set(CoreExtraLibs ${CoreExtraLibs} armips)

set(GlslangLibs glslang OGLCompiler OSDependent SPIRV SPVRemapper)
set(GlslangLibs glslang OGLCompiler OSDependent SPIRV SPVRemapper spirv-cross-glsl)

target_link_libraries(${CoreLibName} Common native kirk cityhash sfmt19937 xbrz xxhash ${GlslangLibs}
${CoreExtraLibs} ${OPENGL_LIBRARIES} ${X11_LIBRARIES} ${CMAKE_DL_LIBS})
Expand Down
34 changes: 23 additions & 11 deletions GPU/Common/ShaderTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,18 @@
#include "ShaderTranslation.h"
#include "ext/glslang/SPIRV/GlslangToSpv.h"
#include "thin3d/thin3d.h"
#include "gfx_es2/gpu_features.h"

#if !defined(ANDROID)
#include "ext/SPIRV-Cross/spirv.hpp"
#include "ext/SPIRV-Cross/spirv_common.hpp"
#include "ext/SPIRV-Cross/spirv_cross.hpp"
#include "ext/SPIRV-Cross/spirv_glsl.hpp"
#ifdef _WIN32
#include "ext/SPIRV-Cross/spirv_hlsl.hpp"
#endif
#endif

extern void init_resources(TBuiltInResource &Resources);

#if !defined(ANDROID)
static EShLanguage GetLanguage(const Draw::ShaderStage stage) {
switch (stage) {
case Draw::ShaderStage::VERTEX: return EShLangVertex;
Expand All @@ -57,16 +55,15 @@ static EShLanguage GetLanguage(const Draw::ShaderStage stage) {
default: return EShLangVertex;
}
}
#endif

void ShaderTranslationInit() {
// TODO: We have TLS issues on UWP
#if !PPSSPP_PLATFORM(UWP) && !defined(ANDROID)
#if !PPSSPP_PLATFORM(UWP)
glslang::InitializeProcess();
#endif
}
void ShaderTranslationShutdown() {
#if !PPSSPP_PLATFORM(UWP) && !defined(ANDROID)
#if !PPSSPP_PLATFORM(UWP)
glslang::FinalizeProcess();
#endif
}
Expand Down Expand Up @@ -196,10 +193,9 @@ bool TranslateShader(std::string *dest, ShaderLanguage destLang, TranslatedShade
bool result = ConvertToVulkanGLSL(dest, destMetadata, src, stage, errorMessage);
return result;
}

#if defined(ANDROID)
return false;
#else
if (errorMessage) {
*errorMessage = "";
}

#if PPSSPP_PLATFORM(UWP)
return false;
Expand Down Expand Up @@ -307,8 +303,24 @@ bool TranslateShader(std::string *dest, ShaderLanguage destLang, TranslatedShade
*dest = glsl.compile();
return true;
}
case GLSL_300:
{
spirv_cross::CompilerGLSL glsl(std::move(spirv));
// The SPIR-V is now parsed, and we can perform reflection on it.
spirv_cross::ShaderResources resources = glsl.get_shader_resources();
// Set some options.
spirv_cross::CompilerGLSL::Options options;
if (gl_extensions.ver[0] >= 4) {
options.version = 400;
} else {
options.version = 300;
}
glsl.set_options(options);
// Compile to GLSL, ready to give to GL driver.
*dest = glsl.compile();
return true;
}
default:
return false;
}
#endif
}
41 changes: 40 additions & 1 deletion GPU/GLES/FramebufferManagerGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "thin3d/thin3d.h"

#include "base/timeutil.h"
#include "file/vfs.h"
#include "math/lin/matrix4x4.h"

#include "Common/ColorConv.h"
Expand All @@ -37,6 +38,7 @@
#include "GPU/GPUState.h"

#include "GPU/Common/PostShader.h"
#include "GPU/Common/ShaderTranslation.h"
#include "GPU/Common/TextureDecoder.h"
#include "GPU/Common/FramebufferCommon.h"
#include "GPU/Debugger/Stepping.h"
Expand Down Expand Up @@ -122,7 +124,44 @@ void FramebufferManagerGLES::CompilePostShader() {
if (shaderInfo) {
std::string errorString;
postShaderAtOutputResolution_ = shaderInfo->outputResolution;
postShaderProgram_ = glsl_create(shaderInfo->vertexShaderFile.c_str(), shaderInfo->fragmentShaderFile.c_str(), &errorString);

size_t sz;
char *vs = (char *)VFSReadFile(shaderInfo->vertexShaderFile.c_str(), &sz);
if (!vs)
return;
char *fs = (char *)VFSReadFile(shaderInfo->fragmentShaderFile.c_str(), &sz);
if (!fs) {
free(vs);
return;
}

std::string vshader;
std::string fshader;
bool translationFailed = false;
if (gl_extensions.IsCoreContext) {
// Gonna have to upconvert the shaders.
std::string errorMessage;
if (!TranslateShader(&vshader, GLSL_300, nullptr, vs, GLSL_140, Draw::ShaderStage::VERTEX, &errorMessage)) {
translationFailed = true;
ELOG("Failed to translate post-vshader: %s", errorMessage.c_str());
}
if (!TranslateShader(&fshader, GLSL_300, nullptr, fs, GLSL_140, Draw::ShaderStage::FRAGMENT, &errorMessage)) {
translationFailed = true;
ELOG("Failed to translate post-fshader: %s", errorMessage.c_str());
}
} else {
vshader = vs;
fshader = fs;
}

if (!translationFailed) {
postShaderProgram_ = glsl_create_source(vshader.c_str(), fshader.c_str(), &errorString);
} else {
ERROR_LOG(FRAMEBUF, "Failed to translate post shader!");
}
free(vs);
free(fs);

if (!postShaderProgram_) {
// DO NOT turn this into a report, as it will pollute our logs with all kinds of
// user shader experiments.
Expand Down
3 changes: 3 additions & 0 deletions UI/DevScreens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ void SystemInfoScreen::CreateViews() {
}
#endif
#endif
if (g_Config.iGPUBackend == GPU_BACKEND_OPENGL) {
deviceSpecs->Add(new InfoItem("Core Context", gl_extensions.IsCoreContext ? "Yes" : "No"));
}
deviceSpecs->Add(new ItemHeader("OS Information"));
deviceSpecs->Add(new InfoItem("Memory Page Size", StringFromFormat("%d bytes", GetMemoryProtectPageSize())));
deviceSpecs->Add(new InfoItem("RW/RX exclusive: ", PlatformIsWXExclusive() ? "Yes" : "No"));
Expand Down
3 changes: 3 additions & 0 deletions Windows/GPU/WindowsGLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "util/text/utf8.h"
#include "i18n/i18n.h"
#include "UI/OnScreenDisplay.h"
#include "ext/glslang/glslang/Public/ShaderLang.h"

#include "Windows/W32Util/Misc.h"
#include "Windows/GPU/WindowsGLContext.h"
Expand Down Expand Up @@ -151,6 +152,7 @@ void DebugCallbackARB(GLenum source, GLenum type, GLuint id, GLenum severity,
}

bool WindowsGLContext::Init(HINSTANCE hInst, HWND window, std::string *error_message) {
glslang::InitializeProcess();
*error_message = "ok";
hWnd = window;
GLuint PixelFormat;
Expand Down Expand Up @@ -398,6 +400,7 @@ void WindowsGLContext::Shutdown() {
hDC = NULL;
}
hWnd = NULL;
glslang::FinalizeProcess();
}

void WindowsGLContext::Resize() {
Expand Down
6 changes: 6 additions & 0 deletions android/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,16 @@ VULKAN_FILES := \
$(SRC)/GPU/Vulkan/VulkanUtil.cpp
#endif

SPIRV_CROSS_FILES := \
$(SRC)/ext/SPIRV-Cross/spirv_cfg.cpp \
$(SRC)/ext/SPIRV-Cross/spirv_cross.cpp \
$(SRC)/ext/SPIRV-Cross/spirv_glsl.cpp

EXEC_AND_LIB_FILES := \
$(ARCH_FILES) \
$(EGL_FILES) \
$(VULKAN_FILES) \
$(SPIRV_CROSS_FILES) \
TestRunner.cpp \
$(SRC)/Core/MIPS/MIPS.cpp.arm \
$(SRC)/Core/MIPS/MIPSAnalyst.cpp \
Expand Down
2 changes: 1 addition & 1 deletion android/jni/Locals.mk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# These are definitions for LOCAL_ variables for PPSSPP.
# They are shared between ppsspp_jni (lib for Android app) and ppsspp_headless.

LOCAL_CFLAGS := -DUSE_FFMPEG -DUSING_GLES2 -DMOBILE_DEVICE -O3 -fsigned-char -Wall -Wno-multichar -Wno-unused-variable -fno-strict-aliasing -D__STDC_CONSTANT_MACROS -Wno-format
LOCAL_CFLAGS := -DUSE_FFMPEG -DUSING_GLES2 -DMOBILE_DEVICE -O3 -fsigned-char -Wall -Wno-multichar -Wno-unused-variable -fno-strict-aliasing -D__STDC_CONSTANT_MACROS -Wno-format -DSPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS
# yes, it's really CPPFLAGS for C++
# deprecated-register is generated by Android default code and causes noise.
LOCAL_CPPFLAGS := -fno-exceptions -std=gnu++11 -fno-rtti -Wno-reorder -Wno-format -Wno-deprecated-register
Expand Down
1 change: 1 addition & 0 deletions ext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if(NOT USING_GLES2)
endif()

set(ENABLE_GLSLANG_BINARIES OFF CACHE BOOL "let's not build binaries we don't need" FORCE)
set(SPIRV_CROSS_EXCEPTIONS_TO_ASSERTIONS ON CACHE BOOL "let's not use exceptions" FORCE)

add_subdirectory(glslang)
add_subdirectory(snappy)
Expand Down
4 changes: 4 additions & 0 deletions ext/native/base/PCMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SDLJoystick *joystick = NULL;
#include "base/display.h"
#include "base/logging.h"
#include "base/timeutil.h"
#include "ext/glslang/glslang/Public/ShaderLang.h"
#include "gfx/gl_common.h"
#include "gfx_es2/gpu_features.h"
#include "input/input_state.h"
Expand Down Expand Up @@ -687,6 +688,8 @@ int main(int argc, char *argv[]) {
printf("Pixels: %i x %i\n", pixel_xres, pixel_yres);
printf("Virtual pixels: %i x %i\n", dp_xres, dp_yres);

glslang::InitializeProcess();

GraphicsContext *graphicsContext = new GLDummyGraphicsContext();
NativeInitGraphics(graphicsContext);

Expand Down Expand Up @@ -939,6 +942,7 @@ int main(int argc, char *argv[]) {
graphicsContext->Shutdown();
NativeShutdown();
delete graphicsContext;
glslang::FinalizeProcess();
// Faster exit, thanks to the OS. Remove this if you want to debug shutdown
// The speed difference is only really noticable on Linux. On Windows you do notice it though
#ifndef MOBILE_DEVICE
Expand Down
4 changes: 4 additions & 0 deletions ext/native/base/QtMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <QLocale>
#include <QThread>

#include "ext/glslang/glslang/Public/ShaderLang.h"

#if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
#include <QStandardPaths>
#ifdef QT_HAS_SYSTEMINFO
Expand Down Expand Up @@ -428,6 +430,7 @@ Q_DECL_EXPORT
#endif
int main(int argc, char *argv[])
{
glslang::InitializeProcess();
#if defined(Q_OS_LINUX)
QApplication::setAttribute(Qt::AA_X11InitThreads, true);
#endif
Expand Down Expand Up @@ -467,6 +470,7 @@ int main(int argc, char *argv[])
SDL_CloseAudio();
#endif
NativeShutdown();
glslang::FinalizeProcess();
return ret;
}

0 comments on commit 2afb671

Please sign in to comment.