Permalink
Browse files

Merge pull request #10369 from hrydgard/shader-translate-gl-core

Use glslang to translate GLSL 1.x postshaders to GLSL 3.0 or later
  • Loading branch information...
hrydgard committed Dec 8, 2017
2 parents 253bba2 + 58fd674 commit 2afb6715d7dbb5f2c602480f2695d34a3bbf9149
View
@@ -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})
@@ -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;
@@ -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
}
@@ -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;
@@ -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
}
@@ -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"
@@ -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"
@@ -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.
View
@@ -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"));
@@ -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"
@@ -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;
@@ -398,6 +400,7 @@ void WindowsGLContext::Shutdown() {
hDC = NULL;
}
hWnd = NULL;
glslang::FinalizeProcess();
}
void WindowsGLContext::Resize() {
View
@@ -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 \
View
@@ -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
View
@@ -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)
@@ -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"
@@ -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);
@@ -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
@@ -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
@@ -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
@@ -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.