Showing the changes to libraries/render-utils/src/DebugDeferredBuffer.cpp (1 of 2 files). Show all
Unified · Split
| @@ -34,56 +34,119 @@ enum Slots { · 33 unmodified lines | |||
| 34 | Lighting | 34 | Lighting |
| 35 | }; | 35 | }; |
| 36 | 36 | ||
| 37 | static const std::string DEEFAULT_DIFFUSE_SHADER { | ||
| 38 | "vec4 getFragmentColor() {" | ||
| 39 | " return vec4(pow(texture(diffuseMap, uv).xyz, vec3(1.0 / 2.2)), 1.0);" | ||
| 40 | " }" | ||
| 41 | }; | ||
| 42 | static const std::string DEEFAULT_ALPHA_SHADER { | ||
| 43 | "vec4 getFragmentColor() {" | ||
| 44 | " return vec4(vec3(texture(diffuseMap, uv).a), 1.0);" | ||
| 45 | " }" | ||
| 46 | }; | ||
| 47 | static const std::string DEEFAULT_SPECULAR_SHADER { | ||
| 48 | "vec4 getFragmentColor() {" | ||
| 49 | " return vec4(texture(specularMap, uv).xyz, 1.0);" | ||
| 50 | " }" | ||
| 51 | }; | ||
| 52 | static const std::string DEEFAULT_ROUGHNESS_SHADER { | ||
| 53 | "vec4 getFragmentColor() {" | ||
| 54 | " return vec4(vec3(texture(specularMap, uv).a), 1.0);" | ||
| 55 | " }" | ||
| 56 | }; | ||
| 57 | static const std::string DEEFAULT_NORMAL_SHADER { | ||
| 58 | "vec4 getFragmentColor() {" | ||
| 59 | " return vec4(texture(normalMap, uv).xyz, 1.0);" | ||
| 60 | " }" | ||
| 61 | }; | ||
| 62 | static const std::string DEEFAULT_DEPTH_SHADER { | ||
| 63 | "vec4 getFragmentColor() {" | ||
| 64 | " return vec4(vec3(texture(depthMap, uv).x), 1.0);" | ||
| 65 | " }" | ||
| 66 | }; | ||
| 67 | static const std::string DEEFAULT_LIGHTING_SHADER { | ||
| 68 | "vec4 getFragmentColor() {" | ||
| 69 | " return vec4(pow(texture(lightingMap, uv).xyz, vec3(1.0 / 2.2)), 1.0);" | ||
| 70 | " }" | ||
| 71 | }; | ||
| 72 | static const std::string DEEFAULT_CUSTOM_SHADER { | ||
| 73 | "vec4 getFragmentColor() {" | ||
| 74 | " return vec4(1.0, 0.0, 0.0, 1.0);" | ||
| 75 | " }" | ||
| 76 | }; | ||
| 77 | |||
| 37 | static std::string getFileContent(std::string fileName, std::string defaultContent = std::string()) { | 78 | static std::string getFileContent(std::string fileName, std::string defaultContent = std::string()) { |
| 38 | QFile customFile(QString::fromStdString(fileName)); | 79 | QFile customFile(QUrl(QString::fromStdString(fileName)).toLocalFile()); |
| 39 | if (customFile.open(QIODevice::ReadOnly)) { | 80 | if (customFile.open(QIODevice::ReadOnly)) { |
| 40 | return customFile.readAll().toStdString(); | 81 | return customFile.readAll().toStdString(); |
| 41 | } | 82 | } |
| 83 | qWarning() << "DebugDeferredBuffer::getFileContent(): Could not open" | ||
| 84 | << QUrl(QString::fromStdString(fileName)).toLocalFile(); | ||
| 42 | return defaultContent; | 85 | return defaultContent; |
| 43 | } | 86 | } |
| 44 | 87 | ||
| 45 | std::string DebugDeferredBuffer::getShaderSourceCode(Modes mode) { | 88 | #include <QStandardPaths> // TODO REMOVE: Temporary until UI |
| 89 | DebugDeferredBuffer::DebugDeferredBuffer() { | ||
| 90 | // TODO REMOVE: Temporary until UI | ||
| 91 | static const auto DESKTOP_PATH = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); | ||
| 92 | static const auto CUSTOM_FILE = "file://" + DESKTOP_PATH.toStdString() + "/custom.slh"; | ||
| 93 | CustomPipeline pipeline; | ||
| 94 | pipeline.info = QFileInfo(DESKTOP_PATH + "/custom.slh"); | ||
| 95 | _customPipelines.emplace(CUSTOM_FILE, pipeline); | ||
| 96 | } | ||
| 97 | |||
| 98 | std::string DebugDeferredBuffer::getShaderSourceCode(Modes mode, std::string customFile) { | ||
| 46 | switch (mode) { | 99 | switch (mode) { |
| 47 | case DiffuseMode: | 100 | case DiffuseMode: |
| 48 | return "vec4 getFragmentColor() { return vec4(pow(texture(diffuseMap, uv).xyz, vec3(1.0 / 2.2)), 1.0); }"; | 101 | return DEEFAULT_DIFFUSE_SHADER; |
| 49 | case AlphaMode: | 102 | case AlphaMode: |
| 50 | return "vec4 getFragmentColor() { return vec4(vec3(texture(diffuseMap, uv).a), 1.0); }"; | 103 | return DEEFAULT_ALPHA_SHADER; |
| 51 | case SpecularMode: | 104 | case SpecularMode: |
| 52 | return "vec4 getFragmentColor() { return vec4(texture(specularMap, uv).xyz, 1.0); }"; | 105 | return DEEFAULT_SPECULAR_SHADER; |
| 53 | case RoughnessMode: | 106 | case RoughnessMode: |
| 54 | return "vec4 getFragmentColor() { return vec4(vec3(texture(specularMap, uv).a), 1.0); }"; | 107 | return DEEFAULT_ROUGHNESS_SHADER; |
| 55 | case NormalMode: | 108 | case NormalMode: |
| 56 | return "vec4 getFragmentColor() { return vec4(texture(normalMap, uv).xyz, 1.0); }"; | 109 | return DEEFAULT_NORMAL_SHADER; |
| 57 | case DepthMode: | 110 | case DepthMode: |
| 58 | return "vec4 getFragmentColor() { return vec4(vec3(texture(depthMap, uv).x), 1.0); }"; | 111 | return DEEFAULT_DEPTH_SHADER; |
| 59 | case LightingMode: | 112 | case LightingMode: |
| 60 | return "vec4 getFragmentColor() { return vec4(pow(texture(lightingMap, uv).xyz, vec3(1.0 / 2.2)), 1.0); }"; | 113 | return DEEFAULT_LIGHTING_SHADER; |
| 61 | case CustomMode: | 114 | case CustomMode: |
| 62 | return getFileContent(CUSTOM_FILE, "vec4 getFragmentColor() { return vec4(1.0); }"); | 115 | return getFileContent(customFile, DEEFAULT_CUSTOM_SHADER); |
| 63 | } | 116 | } |
| 64 | } | 117 | } |
| 65 | 118 | ||
| 66 | bool DebugDeferredBuffer::pipelineNeedsUpdate(Modes mode) const { | 119 | bool DebugDeferredBuffer::pipelineNeedsUpdate(Modes mode, std::string customFile) const { |
| 67 | if (mode != CustomMode) { | 120 | if (mode != CustomMode) { |
| 68 | return !_pipelines[mode]; | 121 | return !_pipelines[mode]; |
| 69 | } | 122 | } |
| 70 | 123 | ||
| 124 | auto it = _customPipelines.find(customFile); | ||
| 125 | if (it != _customPipelines.end() && it->second.pipeline) { | ||
| 126 | auto& info = it->second.info; | ||
| 127 | |||
| 128 | auto lastModified = info.lastModified(); | ||
| 129 | info.refresh(); | ||
| 130 | return lastModified != info.lastModified(); | ||
| 131 | } | ||
| 132 | |||
| 71 | return true; | 133 | return true; |
| 72 | } | 134 | } |
| 73 | 135 | ||
| 74 | const gpu::PipelinePointer& DebugDeferredBuffer::getPipeline(Modes mode) { | 136 | const gpu::PipelinePointer& DebugDeferredBuffer::getPipeline(Modes mode, std::string customFile) { |
| 75 | if (pipelineNeedsUpdate(mode)) { | 137 | if (pipelineNeedsUpdate(mode, customFile)) { |
| 76 | static const std::string VERTEX_SHADER { debug_deferred_buffer_vert }; | 138 | static const std::string VERTEX_SHADER { debug_deferred_buffer_vert }; |
| 77 | static const std::string FRAGMENT_SHADER { debug_deferred_buffer_frag }; | 139 | static const std::string FRAGMENT_SHADER { debug_deferred_buffer_frag }; |
| 78 | static const std::string SOURCE_PLACEHOLDER { "//SOURCE_PLACEHOLDER" }; | 140 | static const std::string SOURCE_PLACEHOLDER { "//SOURCE_PLACEHOLDER" }; |
| 79 | static const auto SOURCE_PLACEHOLDER_INDEX = FRAGMENT_SHADER.find(SOURCE_PLACEHOLDER); | 141 | static const auto SOURCE_PLACEHOLDER_INDEX = FRAGMENT_SHADER.find(SOURCE_PLACEHOLDER); |
| 80 | Q_ASSERT_X(SOURCE_PLACEHOLDER_INDEX != std::string::npos, Q_FUNC_INFO, "Could not find source placeholder"); | 142 | Q_ASSERT_X(SOURCE_PLACEHOLDER_INDEX != std::string::npos, Q_FUNC_INFO, |
| 143 | "Could not find source placeholder"); | ||
| 81 | 144 | ||
| 82 | auto bakedFragmentShader = FRAGMENT_SHADER; | 145 | auto bakedFragmentShader = FRAGMENT_SHADER; |
| 83 | bakedFragmentShader.replace(SOURCE_PLACEHOLDER_INDEX, SOURCE_PLACEHOLDER.size(), | 146 | bakedFragmentShader.replace(SOURCE_PLACEHOLDER_INDEX, SOURCE_PLACEHOLDER.size(), |
| 84 | getShaderSourceCode(mode)); | 147 | getShaderSourceCode(mode, customFile)); |
| 85 | 148 | ||
| 86 | const auto vs = gpu::Shader::createVertex(VERTEX_SHADER); | 149 | static const auto vs = gpu::Shader::createVertex(VERTEX_SHADER); |
| 87 | const auto ps = gpu::Shader::createPixel(bakedFragmentShader); | 150 | const auto ps = gpu::Shader::createPixel(bakedFragmentShader); |
| 88 | const auto program = gpu::Shader::createProgram(vs, ps); | 151 | const auto program = gpu::Shader::createProgram(vs, ps); |
| 89 | 152 | ||
| @@ -95,10 +158,21 @@ const gpu::PipelinePointer& DebugDeferredBuffer::getPipeline(Modes mode) { · 5 unmodified lines | |||
| 95 | slotBindings.insert(gpu::Shader::Binding("lightingMap", Lighting)); | 158 | slotBindings.insert(gpu::Shader::Binding("lightingMap", Lighting)); |
| 96 | gpu::Shader::makeProgram(*program, slotBindings); | 159 | gpu::Shader::makeProgram(*program, slotBindings); |
| 97 | 160 | ||
| 161 | auto pipeline = gpu::Pipeline::create(program, std::make_shared<gpu::State>()); | ||
| 162 | |||
| 98 | // Good to go add the brand new pipeline | 163 | // Good to go add the brand new pipeline |
| 99 | _pipelines[mode] = gpu::Pipeline::create(program, std::make_shared<gpu::State>()); | 164 | if (mode != CustomMode) { |
| 165 | _pipelines[mode] = pipeline; | ||
| 166 | } else { | ||
| 167 | _customPipelines[customFile].pipeline = pipeline; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | if (mode != CustomMode) { | ||
| 172 | return _pipelines[mode]; | ||
| 173 | } else { | ||
| 174 | return _customPipelines[customFile].pipeline; | ||
| 100 | } | 175 | } |
| 101 | return _pipelines[mode]; | ||
| 102 | } | 176 | } |
| 103 | 177 | ||
| 104 | 178 | ||
| @@ -118,8 +192,11 @@ void DebugDeferredBuffer::run(const SceneContextPointer& sceneContext, const Ren · 13 unmodified lines | |||
| 118 | batch.setProjectionTransform(projMat); | 192 | batch.setProjectionTransform(projMat); |
| 119 | batch.setViewTransform(viewMat); | 193 | batch.setViewTransform(viewMat); |
| 120 | batch.setModelTransform(Transform()); | 194 | batch.setModelTransform(Transform()); |
| 195 | |||
| 196 | // TODO REMOVE: Temporary until UI | ||
| 197 | auto first = _customPipelines.begin()->first; | ||
| 121 | 198 | ||
| 122 | batch.setPipeline(getPipeline(Modes(renderContext->_deferredDebugMode))); | 199 | batch.setPipeline(getPipeline(Modes(renderContext->_deferredDebugMode), first)); |
| 123 | 200 | ||
| 124 | batch.setResourceTexture(Diffuse, framebufferCache->getDeferredColorTexture()); | 201 | batch.setResourceTexture(Diffuse, framebufferCache->getDeferredColorTexture()); |
| 125 | batch.setResourceTexture(Normal, framebufferCache->getDeferredNormalTexture()); | 202 | batch.setResourceTexture(Normal, framebufferCache->getDeferredNormalTexture()); |