Archived
1
Fork 0

Add configurable graphics presets

This commit is contained in:
Joshua Goins 2018-11-08 12:51:32 -05:00
parent 066a784f99
commit f0bb00b251
9 changed files with 135 additions and 52 deletions

View file

@ -52,7 +52,8 @@ add_executable(Graph
src/dofpass.cpp src/dofpass.cpp
src/imguipass.cpp src/imguipass.cpp
src/skypass.cpp src/skypass.cpp
src/shadowpass.cpp) src/shadowpass.cpp
src/config.cpp)
target_compile_options(Graph target_compile_options(Graph
PUBLIC PUBLIC
-fno-exceptions -fno-exceptions
@ -90,4 +91,5 @@ add_data(Graph
data/test.cim data/test.cim
data/bokeh.png data/bokeh.png
data/scene.obj data/scene.obj
data/tile.jpg) data/tile.jpg
data/graphics_presets.cfg)

View file

@ -0,0 +1,8 @@
[Low]
shadowResolution=128
[Medium]
shadowResolution=256
[High]
shadowResolution=512

16
include/config.h Normal file
View file

@ -0,0 +1,16 @@
#pragma once
#include <map>
#include <string>
class Config {
public:
bool load(const char* from);
void save(const char* to);
void set(const char* category, const char* key, const std::string& value);
std::string get(const char* category, const char* key);
private:
std::map<std::string, std::map<std::string, std::string>> categories;
};

View file

@ -68,6 +68,10 @@ struct RenderTarget {
VkDescriptorSet* dofSets = nullptr; VkDescriptorSet* dofSets = nullptr;
}; };
struct GraphicsConfig {
int shadowResolution;
};
class World; class World;
class Mesh; class Mesh;
class Camera; class Camera;
@ -75,7 +79,7 @@ class Material;
class Renderer { class Renderer {
public: public:
Renderer(); Renderer(GraphicsConfig config);
~Renderer(); ~Renderer();
void render(World& world, Camera& camera, RenderTarget* target); void render(World& world, Camera& camera, RenderTarget* target);
@ -97,6 +101,10 @@ public:
void fillMaterialBuffers(Material* material); void fillMaterialBuffers(Material* material);
void destroyMaterialBuffers(Material* material); void destroyMaterialBuffers(Material* material);
GraphicsConfig getConfig() const {
return config_;
}
VkInstance getInstance() const { VkInstance getInstance() const {
return instance_; return instance_;
} }
@ -144,6 +152,8 @@ private:
void createDescriptorPool(); void createDescriptorPool();
void createMaterialSetLayout(); void createMaterialSetLayout();
GraphicsConfig config_;
VkInstance instance_ = nullptr; VkInstance instance_ = nullptr;
#ifdef DEBUG #ifdef DEBUG

View file

@ -37,7 +37,7 @@ float filterPCF(vec4 sc) {
const float scale = 1.5; const float scale = 1.5;
const float dx = scale * 1.0 / float(texDim.x); const float dx = scale * 1.0 / float(texDim.x);
const float dy = scale * 1.0 / float(texDim.y); const float dy = scale * 1.0 / float(texDim.y);
const int range = 2; const int range = 1;
float shadowFactor = 0.0; float shadowFactor = 0.0;
int count = 0; int count = 0;

53
src/config.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "config.h"
#include <fstream>
#include <iostream>
bool Config::load(const char* from) {
std::ifstream file(from);
if(!file)
return false;
std::string line, currentCategory;
while(std::getline(file, line)) {
//category
if(line[0] == '[')
currentCategory = line.substr(1, line.length() - 2);
//key/value
if(line.find('=') != std::string::npos) {
std::string key, value;
key = line.substr(0, line.find('='));
value = line.substr(line.find('=') + 1, line.length());
categories[currentCategory][key] = value;
}
}
return true;
}
void Config::save(const char* to) {
std::ofstream file(to);
if(!file)
return;
std::string currentCategory;
for(auto itr : categories) {
file << "[" << itr.first << "]\n";
for(auto itr2 : itr.second) {
file << itr2.first << "=" << itr2.second << "\n";
}
file << "\n";
}
}
void Config::set(const char* category, const char* key, const std::string& value) {
categories[category][key] = value;
}
std::string Config::get(const char* category, const char* key) {
return categories[category][key];
}

View file

@ -17,6 +17,7 @@
#include "camera.h" #include "camera.h"
#include "cinematic.h" #include "cinematic.h"
#include "material.h" #include "material.h"
#include "config.h"
SDL_Window* window = nullptr; SDL_Window* window = nullptr;
Renderer* renderer = nullptr; Renderer* renderer = nullptr;
@ -40,6 +41,8 @@ int windowY = SDL_WINDOWPOS_CENTERED;
int windowWidth = 640; int windowWidth = 640;
int windowHeight = 480; int windowHeight = 480;
int windowFullscreen = 0; int windowFullscreen = 0;
std::string currentGraphicsPreset = "Medium";
GraphicsConfig graphicsConfig;
int toInt(const std::string &str) { int toInt(const std::string &str) {
std::stringstream ss(str); std::stringstream ss(str);
@ -51,51 +54,42 @@ int toInt(const std::string &str) {
return num; return num;
} }
void readConfig() { void loadGraphicsConfig() {
std::ifstream file("config.txt"); Config config;
if(!file) if(!config.load("data/graphics_presets.cfg"))
return; return;
std::string line; graphicsConfig.shadowResolution = toInt(config.get(currentGraphicsPreset.c_str(), "shadowResolution"));
while(std::getline(file, line)) { }
if(line.find('=') != std::string::npos) {
std::string key, value;
key = line.substr(0, line.find('='));
value = line.substr(line.find('=') + 1, line.length());
if(key == "x") { void readConfig() {
windowX = toInt(value); Config config;
} else if(key == "y") { if(!config.load("user.cfg"))
windowY = toInt(value); return;
} else if(key == "width") {
windowWidth = toInt(value); windowX = toInt(config.get("Window", "x"));
} else if(key == "height") { windowY = toInt(config.get("Window", "y"));
windowHeight = toInt(value); windowWidth = toInt(config.get("Window", "width"));
} else if(key == "fullscreen") { windowHeight = toInt(config.get("Window", "height"));
windowFullscreen = toInt(value); windowFullscreen = toInt(config.get("Window", "fullscreen"));
}
} currentGraphicsPreset = config.get("Graphics", "preset");
}
loadGraphicsConfig();
} }
void writeConfig() { void writeConfig() {
std::ofstream file("config.txt"); Config config;
if(!file)
return;
int x, y; config.set("Window", "x", std::to_string(windowX));
SDL_GetWindowPosition(window, &x, &y); config.set("Window", "y", std::to_string(windowY));
config.set("Window", "width", std::to_string(windowWidth));
config.set("Window", "height", std::to_string(windowHeight));
config.set("Window", "fullscreen", std::to_string(windowFullscreen));
file << "x=" << x << "\n"; config.set("Graphics", "preset", currentGraphicsPreset);
file << "y=" << y << "\n";
int w, h; config.save("user.cfg");
SDL_GetWindowSize(window, &w, &h);
file << "width=" << w << "\n";
file << "height=" << h << "\n";
file << "fullscreen=" << windowFullscreen << "\n";
} }
Mesh* loadMesh(const char* path) { Mesh* loadMesh(const char* path) {
@ -228,7 +222,7 @@ int main(int argc, char* argv[]) {
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
renderer = new Renderer(); renderer = new Renderer(graphicsConfig);
VkSurfaceKHR surface = nullptr; VkSurfaceKHR surface = nullptr;
SDL_Vulkan_CreateSurface(window, renderer->getInstance(), &surface); SDL_Vulkan_CreateSurface(window, renderer->getInstance(), &surface);

View file

@ -14,7 +14,7 @@
#include "camera.h" #include "camera.h"
#include "material.h" #include "material.h"
Renderer::Renderer() { Renderer::Renderer(GraphicsConfig config) : config_(config) {
createInstance(); createInstance();
#ifdef DEBUG #ifdef DEBUG
if(enableDebug) if(enableDebug)

View file

@ -29,15 +29,15 @@ ShadowPass::~ShadowPass() {
void ShadowPass::render(VkCommandBuffer commandBuffer, World& world) { void ShadowPass::render(VkCommandBuffer commandBuffer, World& world) {
VkViewport viewport = {}; VkViewport viewport = {};
viewport.width = 512; viewport.width = renderer_.getConfig().shadowResolution;
viewport.height = 512; viewport.height = renderer_.getConfig().shadowResolution;
viewport.maxDepth = 1.0f; viewport.maxDepth = 1.0f;
vkCmdSetViewport(commandBuffer, 0, 1, &viewport); vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
VkRect2D scissor = {}; VkRect2D scissor = {};
scissor.extent.width = 512; scissor.extent.width = renderer_.getConfig().shadowResolution;
scissor.extent.height = 512; scissor.extent.height = renderer_.getConfig().shadowResolution;
vkCmdSetScissor(commandBuffer, 0, 1, &scissor); vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
@ -48,8 +48,8 @@ void ShadowPass::render(VkCommandBuffer commandBuffer, World& world) {
renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBeginInfo.framebuffer = shadowFramebuffer_; renderPassBeginInfo.framebuffer = shadowFramebuffer_;
renderPassBeginInfo.renderPass = renderPass_; renderPassBeginInfo.renderPass = renderPass_;
renderPassBeginInfo.renderArea.extent.width = 512; renderPassBeginInfo.renderArea.extent.width = renderer_.getConfig().shadowResolution;
renderPassBeginInfo.renderArea.extent.height = 512; renderPassBeginInfo.renderArea.extent.height = renderer_.getConfig().shadowResolution;
renderPassBeginInfo.clearValueCount = 1; renderPassBeginInfo.clearValueCount = 1;
renderPassBeginInfo.pClearValues = &clearColor; renderPassBeginInfo.pClearValues = &clearColor;
@ -113,8 +113,8 @@ void ShadowPass::createFramebuffer() {
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D; imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imageCreateInfo.format = VK_FORMAT_D32_SFLOAT; imageCreateInfo.format = VK_FORMAT_D32_SFLOAT;
imageCreateInfo.extent.width = 512; imageCreateInfo.extent.width = renderer_.getConfig().shadowResolution;
imageCreateInfo.extent.height = 512; imageCreateInfo.extent.height = renderer_.getConfig().shadowResolution;
imageCreateInfo.extent.depth = 1; imageCreateInfo.extent.depth = 1;
imageCreateInfo.mipLevels = 1; imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = 1; imageCreateInfo.arrayLayers = 1;
@ -151,8 +151,8 @@ void ShadowPass::createFramebuffer() {
framebufferInfo.renderPass = renderPass_; framebufferInfo.renderPass = renderPass_;
framebufferInfo.attachmentCount = 1; framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = &shadowImageView_; framebufferInfo.pAttachments = &shadowImageView_;
framebufferInfo.width = 512; framebufferInfo.width = renderer_.getConfig().shadowResolution;
framebufferInfo.height = 512; framebufferInfo.height = renderer_.getConfig().shadowResolution;
framebufferInfo.layers = 1; framebufferInfo.layers = 1;
vkCreateFramebuffer(renderer_.getDevice(), &framebufferInfo, nullptr, &shadowFramebuffer_); vkCreateFramebuffer(renderer_.getDevice(), &framebufferInfo, nullptr, &shadowFramebuffer_);