History

More custom debug work

Atlante45 committed 10 years ago

Commit f41ca9f1 parent d8a389f2 files · +111 −25Browse files

Showing the changes to libraries/render-utils/src/DebugDeferredBuffer.cpp (1 of 2 files). Show all

Unified · Split

libraries/render-utils/src/DebugDeferredBuffer.cpp+96−19
@@ -34,56 +34,119 @@ enum Slots { · 33 unmodified lines
3434 Lighting
3535 };
3636
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+
3778 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());
3980 if (customFile.open(QIODevice::ReadOnly)) {
4081 return customFile.readAll().toStdString();
4182 }
83+ qWarning() << "DebugDeferredBuffer::getFileContent(): Could not open"
84+ << QUrl(QString::fromStdString(fileName)).toLocalFile();
4285 return defaultContent;
4386 }
4487
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) {
4699 switch (mode) {
47100 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;
49102 case AlphaMode:
50− return "vec4 getFragmentColor() { return vec4(vec3(texture(diffuseMap, uv).a), 1.0); }";
103+ return DEEFAULT_ALPHA_SHADER;
51104 case SpecularMode:
52− return "vec4 getFragmentColor() { return vec4(texture(specularMap, uv).xyz, 1.0); }";
105+ return DEEFAULT_SPECULAR_SHADER;
53106 case RoughnessMode:
54− return "vec4 getFragmentColor() { return vec4(vec3(texture(specularMap, uv).a), 1.0); }";
107+ return DEEFAULT_ROUGHNESS_SHADER;
55108 case NormalMode:
56− return "vec4 getFragmentColor() { return vec4(texture(normalMap, uv).xyz, 1.0); }";
109+ return DEEFAULT_NORMAL_SHADER;
57110 case DepthMode:
58− return "vec4 getFragmentColor() { return vec4(vec3(texture(depthMap, uv).x), 1.0); }";
111+ return DEEFAULT_DEPTH_SHADER;
59112 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;
61114 case CustomMode:
62− return getFileContent(CUSTOM_FILE, "vec4 getFragmentColor() { return vec4(1.0); }");
115+ return getFileContent(customFile, DEEFAULT_CUSTOM_SHADER);
63116 }
64117 }
65118
66−bool DebugDeferredBuffer::pipelineNeedsUpdate(Modes mode) const {
119+bool DebugDeferredBuffer::pipelineNeedsUpdate(Modes mode, std::string customFile) const {
67120 if (mode != CustomMode) {
68121 return !_pipelines[mode];
69122 }
70123
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+
71133 return true;
72134 }
73135
74−const gpu::PipelinePointer& DebugDeferredBuffer::getPipeline(Modes mode) {
75− if (pipelineNeedsUpdate(mode)) {
136+const gpu::PipelinePointer& DebugDeferredBuffer::getPipeline(Modes mode, std::string customFile) {
137+ if (pipelineNeedsUpdate(mode, customFile)) {
76138 static const std::string VERTEX_SHADER { debug_deferred_buffer_vert };
77139 static const std::string FRAGMENT_SHADER { debug_deferred_buffer_frag };
78140 static const std::string SOURCE_PLACEHOLDER { "//SOURCE_PLACEHOLDER" };
79141 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");
81144
82145 auto bakedFragmentShader = FRAGMENT_SHADER;
83146 bakedFragmentShader.replace(SOURCE_PLACEHOLDER_INDEX, SOURCE_PLACEHOLDER.size(),
84− getShaderSourceCode(mode));
147+ getShaderSourceCode(mode, customFile));
85148
86− const auto vs = gpu::Shader::createVertex(VERTEX_SHADER);
149+ static const auto vs = gpu::Shader::createVertex(VERTEX_SHADER);
87150 const auto ps = gpu::Shader::createPixel(bakedFragmentShader);
88151 const auto program = gpu::Shader::createProgram(vs, ps);
89152
@@ -95,10 +158,21 @@ const gpu::PipelinePointer& DebugDeferredBuffer::getPipeline(Modes mode) { · 5 unmodified lines
95158 slotBindings.insert(gpu::Shader::Binding("lightingMap", Lighting));
96159 gpu::Shader::makeProgram(*program, slotBindings);
97160
161+ auto pipeline = gpu::Pipeline::create(program, std::make_shared<gpu::State>());
162+
98163 // 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;
100175 }
101− return _pipelines[mode];
102176 }
103177
104178
@@ -118,8 +192,11 @@ void DebugDeferredBuffer::run(const SceneContextPointer& sceneContext, const Ren · 13 unmodified lines
118192 batch.setProjectionTransform(projMat);
119193 batch.setViewTransform(viewMat);
120194 batch.setModelTransform(Transform());
195+
196+ // TODO REMOVE: Temporary until UI
197+ auto first = _customPipelines.begin()->first;
121198
122− batch.setPipeline(getPipeline(Modes(renderContext->_deferredDebugMode)));
199+ batch.setPipeline(getPipeline(Modes(renderContext->_deferredDebugMode), first));
123200
124201 batch.setResourceTexture(Diffuse, framebufferCache->getDeferredColorTexture());
125202 batch.setResourceTexture(Normal, framebufferCache->getDeferredNormalTexture());