yuzu-emu
/
yuzu
Archived
1
0
Fork 0

Merge pull request #4569 from ReinUsesLisp/glsl-cmake

video_core/host_shaders: Add CMake integration for string shaders
This commit is contained in:
bunnei 2020-08-26 22:57:39 -04:00 committed by GitHub
commit 1bb8c27a70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 127 additions and 51 deletions

View File

@ -1,3 +1,5 @@
add_subdirectory(host_shaders)
add_library(video_core STATIC add_library(video_core STATIC
buffer_cache/buffer_block.h buffer_cache/buffer_block.h
buffer_cache/buffer_cache.h buffer_cache/buffer_cache.h
@ -244,6 +246,9 @@ create_target_directory_groups(video_core)
target_link_libraries(video_core PUBLIC common core) target_link_libraries(video_core PUBLIC common core)
target_link_libraries(video_core PRIVATE glad xbyak) target_link_libraries(video_core PRIVATE glad xbyak)
add_dependencies(video_core host_shaders)
target_include_directories(video_core PRIVATE ${HOST_SHADERS_INCLUDE})
if (ENABLE_VULKAN) if (ENABLE_VULKAN)
target_include_directories(video_core PRIVATE sirit ../../externals/Vulkan-Headers/include) target_include_directories(video_core PRIVATE sirit ../../externals/Vulkan-Headers/include)
target_compile_definitions(video_core PRIVATE HAS_VULKAN) target_compile_definitions(video_core PRIVATE HAS_VULKAN)

View File

@ -0,0 +1,43 @@
set(SHADER_FILES
opengl_present.frag
opengl_present.vert
)
set(SHADER_INCLUDE ${CMAKE_CURRENT_BINARY_DIR}/include)
set(HOST_SHADERS_INCLUDE ${SHADER_INCLUDE} PARENT_SCOPE)
set(SHADER_DIR ${SHADER_INCLUDE}/video_core/host_shaders)
add_custom_command(
OUTPUT
${SHADER_DIR}
COMMAND
${CMAKE_COMMAND} -E make_directory ${SHADER_DIR}
)
set(INPUT_FILE ${CMAKE_CURRENT_SOURCE_DIR}/source_shader.h.in)
set(HEADER_GENERATOR ${CMAKE_CURRENT_SOURCE_DIR}/StringShaderHeader.cmake)
foreach(FILENAME IN ITEMS ${SHADER_FILES})
string(REPLACE "." "_" SHADER_NAME ${FILENAME})
set(SOURCE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${FILENAME})
set(HEADER_FILE ${SHADER_DIR}/${SHADER_NAME}.h)
add_custom_command(
OUTPUT
${HEADER_FILE}
COMMAND
${CMAKE_COMMAND} -P ${HEADER_GENERATOR} ${SOURCE_FILE} ${HEADER_FILE} ${INPUT_FILE}
MAIN_DEPENDENCY
${SOURCE_FILE}
DEPENDS
${HEADER_GENERATOR}
${INPUT_FILE}
)
set(SHADER_HEADERS ${SHADER_HEADERS} ${HEADER_FILE})
endforeach()
add_custom_target(host_shaders
DEPENDS
${SHADER_HEADERS}
SOURCES
${SHADER_FILES}
)

View File

@ -0,0 +1,11 @@
set(SOURCE_FILE ${CMAKE_ARGV3})
set(HEADER_FILE ${CMAKE_ARGV4})
set(INPUT_FILE ${CMAKE_ARGV5})
get_filename_component(CONTENTS_NAME ${SOURCE_FILE} NAME)
string(REPLACE "." "_" CONTENTS_NAME ${CONTENTS_NAME})
string(TOUPPER ${CONTENTS_NAME} CONTENTS_NAME)
file(READ ${SOURCE_FILE} CONTENTS)
configure_file(${INPUT_FILE} ${HEADER_FILE} @ONLY)

View File

@ -0,0 +1,10 @@
#version 430 core
layout (location = 0) in vec2 frag_tex_coord;
layout (location = 0) out vec4 color;
layout (binding = 0) uniform sampler2D color_texture;
void main() {
color = vec4(texture(color_texture, frag_tex_coord).rgb, 1.0f);
}

View File

@ -0,0 +1,24 @@
#version 430 core
out gl_PerVertex {
vec4 gl_Position;
};
layout (location = 0) in vec2 vert_position;
layout (location = 1) in vec2 vert_tex_coord;
layout (location = 0) out vec2 frag_tex_coord;
// This is a truncated 3x3 matrix for 2D transformations:
// The upper-left 2x2 submatrix performs scaling/rotation/mirroring.
// The third column performs translation.
// The third row could be used for projection, which we don't need in 2D. It hence is assumed to
// implicitly be [0, 0, 1]
layout (location = 0) uniform mat3x2 modelview_matrix;
void main() {
// Multiply input position by the rotscale part of the matrix and then manually translate by
// the last column. This is equivalent to using a full 3x3 matrix and expanding the vector
// to `vec3(vert_position.xy, 1.0)`
gl_Position = vec4(mat2(modelview_matrix) * vert_position + modelview_matrix[2], 0.0, 1.0);
frag_tex_coord = vert_tex_coord;
}

View File

@ -0,0 +1,9 @@
#pragma once
#include <string_view>
namespace HostShaders {
constexpr std::string_view @CONTENTS_NAME@ = R"(@CONTENTS@)";
} // namespace HostShaders

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <string_view>
#include <utility> #include <utility>
#include <glad/glad.h> #include <glad/glad.h>
#include "common/common_types.h" #include "common/common_types.h"
@ -82,11 +83,13 @@ void OGLSampler::Release() {
handle = 0; handle = 0;
} }
void OGLShader::Create(const char* source, GLenum type) { void OGLShader::Create(std::string_view source, GLenum type) {
if (handle != 0) if (handle != 0) {
return; return;
if (source == nullptr) }
if (source.empty()) {
return; return;
}
MICROPROFILE_SCOPE(OpenGL_ResourceCreation); MICROPROFILE_SCOPE(OpenGL_ResourceCreation);
handle = GLShader::LoadShader(source, type); handle = GLShader::LoadShader(source, type);

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <string_view>
#include <utility> #include <utility>
#include <glad/glad.h> #include <glad/glad.h>
#include "common/common_types.h" #include "common/common_types.h"
@ -127,7 +128,7 @@ public:
return *this; return *this;
} }
void Create(const char* source, GLenum type); void Create(std::string_view source, GLenum type);
void Release(); void Release();

View File

@ -22,6 +22,7 @@
#include "video_core/memory_manager.h" #include "video_core/memory_manager.h"
#include "video_core/renderer_opengl/gl_arb_decompiler.h" #include "video_core/renderer_opengl/gl_arb_decompiler.h"
#include "video_core/renderer_opengl/gl_rasterizer.h" #include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/gl_resource_manager.h"
#include "video_core/renderer_opengl/gl_shader_cache.h" #include "video_core/renderer_opengl/gl_shader_cache.h"
#include "video_core/renderer_opengl/gl_shader_decompiler.h" #include "video_core/renderer_opengl/gl_shader_decompiler.h"
#include "video_core/renderer_opengl/gl_shader_disk_cache.h" #include "video_core/renderer_opengl/gl_shader_disk_cache.h"

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <string_view>
#include <vector> #include <vector>
#include <glad/glad.h> #include <glad/glad.h>
#include "common/assert.h" #include "common/assert.h"
@ -11,7 +12,8 @@
namespace OpenGL::GLShader { namespace OpenGL::GLShader {
namespace { namespace {
const char* GetStageDebugName(GLenum type) {
std::string_view StageDebugName(GLenum type) {
switch (type) { switch (type) {
case GL_VERTEX_SHADER: case GL_VERTEX_SHADER:
return "vertex"; return "vertex";
@ -25,12 +27,17 @@ const char* GetStageDebugName(GLenum type) {
UNIMPLEMENTED(); UNIMPLEMENTED();
return "unknown"; return "unknown";
} }
} // Anonymous namespace } // Anonymous namespace
GLuint LoadShader(const char* source, GLenum type) { GLuint LoadShader(std::string_view source, GLenum type) {
const char* debug_type = GetStageDebugName(type); const std::string_view debug_type = StageDebugName(type);
const GLuint shader_id = glCreateShader(type); const GLuint shader_id = glCreateShader(type);
glShaderSource(shader_id, 1, &source, nullptr);
const GLchar* source_string = source.data();
const GLint source_length = static_cast<GLint>(source.size());
glShaderSource(shader_id, 1, &source_string, &source_length);
LOG_DEBUG(Render_OpenGL, "Compiling {} shader...", debug_type); LOG_DEBUG(Render_OpenGL, "Compiling {} shader...", debug_type);
glCompileShader(shader_id); glCompileShader(shader_id);

View File

@ -38,7 +38,7 @@ void LogShaderSource(T... shaders) {
* @param source String of the GLSL shader program * @param source String of the GLSL shader program
* @param type Type of the shader (GL_VERTEX_SHADER, GL_GEOMETRY_SHADER or GL_FRAGMENT_SHADER) * @param type Type of the shader (GL_VERTEX_SHADER, GL_GEOMETRY_SHADER or GL_FRAGMENT_SHADER)
*/ */
GLuint LoadShader(const char* source, GLenum type); GLuint LoadShader(std::string_view source, GLenum type);
/** /**
* Utility function to create and compile an OpenGL GLSL shader program (vertex + fragment shader) * Utility function to create and compile an OpenGL GLSL shader program (vertex + fragment shader)

View File

@ -21,6 +21,8 @@
#include "core/perf_stats.h" #include "core/perf_stats.h"
#include "core/settings.h" #include "core/settings.h"
#include "core/telemetry_session.h" #include "core/telemetry_session.h"
#include "video_core/host_shaders/opengl_present_frag.h"
#include "video_core/host_shaders/opengl_present_vert.h"
#include "video_core/morton.h" #include "video_core/morton.h"
#include "video_core/renderer_opengl/gl_rasterizer.h" #include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/gl_shader_manager.h" #include "video_core/renderer_opengl/gl_shader_manager.h"
@ -44,46 +46,6 @@ struct Frame {
bool is_srgb{}; /// Framebuffer is sRGB or RGB bool is_srgb{}; /// Framebuffer is sRGB or RGB
}; };
constexpr char VERTEX_SHADER[] = R"(
#version 430 core
out gl_PerVertex {
vec4 gl_Position;
};
layout (location = 0) in vec2 vert_position;
layout (location = 1) in vec2 vert_tex_coord;
layout (location = 0) out vec2 frag_tex_coord;
// This is a truncated 3x3 matrix for 2D transformations:
// The upper-left 2x2 submatrix performs scaling/rotation/mirroring.
// The third column performs translation.
// The third row could be used for projection, which we don't need in 2D. It hence is assumed to
// implicitly be [0, 0, 1]
layout (location = 0) uniform mat3x2 modelview_matrix;
void main() {
// Multiply input position by the rotscale part of the matrix and then manually translate by
// the last column. This is equivalent to using a full 3x3 matrix and expanding the vector
// to `vec3(vert_position.xy, 1.0)`
gl_Position = vec4(mat2(modelview_matrix) * vert_position + modelview_matrix[2], 0.0, 1.0);
frag_tex_coord = vert_tex_coord;
}
)";
constexpr char FRAGMENT_SHADER[] = R"(
#version 430 core
layout (location = 0) in vec2 frag_tex_coord;
layout (location = 0) out vec4 color;
layout (binding = 0) uniform sampler2D color_texture;
void main() {
color = vec4(texture(color_texture, frag_tex_coord).rgb, 1.0f);
}
)";
constexpr GLint PositionLocation = 0; constexpr GLint PositionLocation = 0;
constexpr GLint TexCoordLocation = 1; constexpr GLint TexCoordLocation = 1;
constexpr GLint ModelViewMatrixLocation = 0; constexpr GLint ModelViewMatrixLocation = 0;
@ -461,10 +423,10 @@ void RendererOpenGL::InitOpenGLObjects() {
// Create shader programs // Create shader programs
OGLShader vertex_shader; OGLShader vertex_shader;
vertex_shader.Create(VERTEX_SHADER, GL_VERTEX_SHADER); vertex_shader.Create(HostShaders::OPENGL_PRESENT_VERT, GL_VERTEX_SHADER);
OGLShader fragment_shader; OGLShader fragment_shader;
fragment_shader.Create(FRAGMENT_SHADER, GL_FRAGMENT_SHADER); fragment_shader.Create(HostShaders::OPENGL_PRESENT_FRAG, GL_FRAGMENT_SHADER);
vertex_program.Create(true, false, vertex_shader.handle); vertex_program.Create(true, false, vertex_shader.handle);
fragment_program.Create(true, false, fragment_shader.handle); fragment_program.Create(true, false, fragment_shader.handle);