Based on the repository content, I'll help create a simplified shader player that supports multipass rendering and common sections. Here's a design that implements the core functionality:
cpp Copy code
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <nlohmann/json.hpp>
#include <fstream>
#include <vector>
#include <memory>
#include <chrono>
#include <filesystem>
using json = nlohmann::json;
namespace fs = std::filesystem;
struct ShaderPass {
GLuint program;
std::string type; // "image", "buffer", "cubemap", "common"
std::vector<GLuint> inputs; // Texture inputs from other passes
GLuint outputTexture;
GLuint framebuffer;
int width;
int height;
};
class ShaderPlayer {
private:
GLFWwindow* window;
std::vector<ShaderPass> passes;
std::chrono::steady_clock::time_point startTime;
// Uniforms
float iTime;
float iTimeDelta;
float iFrame;
float iFrameRate;
glm::vec4 iMouse;
glm::vec4 iDate;
GLuint createShader(const std::string& source, GLenum type) {
GLuint shader = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infoLog[512];
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
throw std::runtime_error(std::string("Shader compilation failed: ") + infoLog);
}
return shader;
}
GLuint createProgram(const std::string& vertexSource, const std::string& fragmentSource) {
GLuint vertexShader = createShader(vertexSource, GL_VERTEX_SHADER);
GLuint fragmentShader = createShader(fragmentSource, GL_FRAGMENT_SHADER);
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
GLint success;
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (!success) {
GLchar infoLog[512];
glGetProgramInfoLog(program, 512, nullptr, infoLog);
throw std::runtime_error(std::string("Program linking failed: ") + infoLog);
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return program;
}
void setupFramebuffer(ShaderPass& pass) {
glGenFramebuffers(1, &pass.framebuffer);
glGenTextures(1, &pass.outputTexture);
glBindTexture(GL_TEXTURE_2D, pass.outputTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, pass.width, pass.height, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindFramebuffer(GL_FRAMEBUFFER, pass.framebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pass.outputTexture, 0);
}
public:
ShaderPlayer() : window(nullptr) {
if (!glfwInit()) {
throw std::runtime_error("Failed to initialize GLFW");
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(800, 600, "Shader Player", nullptr, nullptr);
if (!window) {
glfwTerminate();
throw std::runtime_error("Failed to create GLFW window");
}
glfwMakeContextCurrent(window);
if (glewInit() != GLEW_OK) {
throw std::runtime_error("Failed to initialize GLEW");
}
startTime = std::chrono::steady_clock::now();
}
void loadShader(const std::string& jsonPath) {
std::ifstream file(jsonPath);
json shaderData = json::parse(file);
// Load common section if exists
std::string commonCode;
if (shaderData.contains("common")) {
commonCode = shaderData["common"].get<std::string>();
}
// Parse and setup passes
const auto& renderPasses = shaderData["renderpass"];
for (const auto& pass : renderPasses) {
ShaderPass shaderPass;
shaderPass.type = pass["type"].get<std::string>();
// Combine common code with pass code
std::string fragmentCode = commonCode + "\n" + pass["code"].get<std::string>();
// Create simple vertex shader for quad rendering
const char* vertexShaderSource = R"(
#version 450
layout(location = 0) in vec2 position;
layout(location = 1) in vec2 texCoord;
out vec2 TexCoords;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
TexCoords = texCoord;
}
)";
shaderPass.program = createProgram(vertexShaderSource, fragmentCode);
shaderPass.width = 800; // Use actual window size
shaderPass.height = 600;
setupFramebuffer(shaderPass);
passes.push_back(std::move(shaderPass));
}
}
void render() {
auto currentTime = std::chrono::steady_clock::now();
float time = std::chrono::duration<float>(currentTime - startTime).count();
for (auto& pass : passes) {
glBindFramebuffer(GL_FRAMEBUFFER, pass.framebuffer);
glViewport(0, 0, pass.width, pass.height);
glUseProgram(pass.program);
// Set uniforms
glUniform1f(glGetUniformLocation(pass.program, "iTime"), time);
glUniform1f(glGetUniformLocation(pass.program, "iTimeDelta"), iTimeDelta);
glUniform1f(glGetUniformLocation(pass.program, "iFrame"), iFrame);
glUniform2f(glGetUniformLocation(pass.program, "iResolution"),
static_cast<float>(pass.width),
static_cast<float>(pass.height));
// Bind input textures from previous passes
for (size_t i = 0; i < pass.inputs.size(); ++i) {
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, pass.inputs[i]);
glUniform1i(glGetUniformLocation(pass.program,
("iChannel" + std::to_string(i)).c_str()), i);
}
// Render quad
renderQuad();
}
// Final pass to screen
glBindFramebuffer(GL_