History

Fixed fading on skinned objects. Added initialiazeShapePipelines on GeometryCache to postpone simple pipeline creation after initializeGL

Olivier Prat committed 9 years ago

Commit 20d4fcb1 parent de143d08 files · +151 −73Browse files

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

Unified · Split

libraries/render-utils/src/GeometryCache.cpp+80−39
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424
2525 #include "TextureCache.h"
2626 #include "RenderUtilsLogging.h"
27+#include "FadeEffect.h"
2728
2829 #include "gpu/StandardShaderLib.h"
2930
@@ -35,6 +36,9 @@ · 5 unmodified lines
3536 #include "simple_vert.h"
3637 #include "simple_textured_frag.h"
3738 #include "simple_textured_unlit_frag.h"
39+#include "simple_fade_vert.h"
40+#include "simple_textured_fade_frag.h"
41+#include "simple_textured_unlit_fade_frag.h"
3842 #include "simple_opaque_web_browser_frag.h"
3943 #include "simple_opaque_web_browser_overlay_frag.h"
4044 #include "simple_transparent_web_browser_frag.h"
@@ -403,30 +407,13 @@ gpu::Stream::FormatPointer& getInstancedSolidStreamFormat() { · 362 unmodified lines
403407
404408 render::ShapePipelinePointer GeometryCache::_simpleOpaquePipeline;
405409 render::ShapePipelinePointer GeometryCache::_simpleTransparentPipeline;
410+render::ShapePipelinePointer GeometryCache::_simpleOpaqueFadePipeline;
411+render::ShapePipelinePointer GeometryCache::_simpleTransparentFadePipeline;
406412 render::ShapePipelinePointer GeometryCache::_simpleWirePipeline;
407413
408414 GeometryCache::GeometryCache() :
409415 _nextID(0) {
410416 buildShapes();
411− GeometryCache::_simpleOpaquePipeline =
412− std::make_shared<render::ShapePipeline>(getSimplePipeline(false, false, true, false), nullptr,
413− [](const render::ShapePipeline&, gpu::Batch& batch) {
414− // Set the defaults needed for a simple program
415− batch.setResourceTexture(render::ShapePipeline::Slot::MAP::ALBEDO,
416− DependencyManager::get<TextureCache>()->getWhiteTexture());
417− }
418− );
419− GeometryCache::_simpleTransparentPipeline =
420− std::make_shared<render::ShapePipeline>(getSimplePipeline(false, true, true, false), nullptr,
421− [](const render::ShapePipeline&, gpu::Batch& batch) {
422− // Set the defaults needed for a simple program
423− batch.setResourceTexture(render::ShapePipeline::Slot::MAP::ALBEDO,
424− DependencyManager::get<TextureCache>()->getWhiteTexture());
425− }
426− );
427− GeometryCache::_simpleWirePipeline =
428− std::make_shared<render::ShapePipeline>(getSimplePipeline(false, false, true, true), nullptr,
429− [](const render::ShapePipeline&, gpu::Batch& batch) {});
430417 }
431418
432419 GeometryCache::~GeometryCache() {
@@ -471,6 +458,35 @@ void setupBatchInstance(gpu::Batch& batch, gpu::BufferPointer colorBuffer) { · 38 unmodified lines
471458 batch.setInputBuffer(gpu::Stream::COLOR, colorView);
472459 }
473460
461+void GeometryCache::initializeShapePipelines() {
462+ GeometryCache::_simpleOpaquePipeline = getShapePipeline(false, false, true, false);
463+ GeometryCache::_simpleTransparentPipeline = getShapePipeline(false, true, true, false);
464+ GeometryCache::_simpleOpaqueFadePipeline = getShapePipeline(false, false, true, false, false, true);
465+ GeometryCache::_simpleTransparentFadePipeline = getShapePipeline(false, true, true, false, false, true);
466+ GeometryCache::_simpleWirePipeline = getShapePipeline(false, false, true, true);
467+}
468+
469+render::ShapePipelinePointer GeometryCache::getShapePipeline(bool textured, bool transparent, bool culled,
470+ bool unlit, bool depthBias, bool fading) {
471+ return std::make_shared<render::ShapePipeline>(getSimplePipeline(textured, transparent, culled, unlit, depthBias, fading), nullptr,
472+ [](const render::ShapePipeline&, gpu::Batch& batch) {
473+ // Set the defaults needed for a simple program
474+ batch.setResourceTexture(render::ShapePipeline::Slot::MAP::ALBEDO,
475+ DependencyManager::get<TextureCache>()->getWhiteTexture());
476+ }
477+ );
478+}
479+
480+render::ShapePipelinePointer GeometryCache::getOpaqueShapePipeline(bool isFading) {
481+ isFading = isFading || DependencyManager::get<FadeEffect>()->isDebugEnabled();
482+ return isFading ? _simpleOpaqueFadePipeline : _simpleOpaquePipeline;
483+}
484+
485+render::ShapePipelinePointer GeometryCache::getTransparentShapePipeline(bool isFading) {
486+ isFading = isFading || DependencyManager::get<FadeEffect>()->isDebugEnabled();
487+ return isFading ? _simpleTransparentFadePipeline : _simpleTransparentPipeline;
488+}
489+
474490 void GeometryCache::renderShape(gpu::Batch& batch, Shape shape) {
475491 batch.setInputFormat(getSolidStreamFormat());
476492 _shapes[shape].draw(batch);
@@ -1714,6 +1730,7 @@ public: · 1237 unmodified lines
17141730 IS_CULLED_FLAG,
17151731 IS_UNLIT_FLAG,
17161732 HAS_DEPTH_BIAS_FLAG,
1733+ IS_FADING_FLAG,
17171734
17181735 NUM_FLAGS,
17191736 };
@@ -1724,6 +1741,7 @@ public: · 4 unmodified lines
17241741 IS_CULLED = (1 << IS_CULLED_FLAG),
17251742 IS_UNLIT = (1 << IS_UNLIT_FLAG),
17261743 HAS_DEPTH_BIAS = (1 << HAS_DEPTH_BIAS_FLAG),
1744+ IS_FADING = (1 << IS_FADING_FLAG),
17271745 };
17281746 typedef unsigned short Flags;
17291747
@@ -1734,6 +1752,7 @@ public: · 4 unmodified lines
17341752 bool isCulled() const { return isFlag(IS_CULLED); }
17351753 bool isUnlit() const { return isFlag(IS_UNLIT); }
17361754 bool hasDepthBias() const { return isFlag(HAS_DEPTH_BIAS); }
1755+ bool isFading() const { return isFlag(IS_FADING); }
17371756
17381757 Flags _flags = 0;
17391758 short _spare = 0;
@@ -1742,9 +1761,9 @@ public: · 2 unmodified lines
17421761
17431762
17441763 SimpleProgramKey(bool textured = false, bool transparent = false, bool culled = true,
1745− bool unlit = false, bool depthBias = false) {
1764+ bool unlit = false, bool depthBias = false, bool fading = false) {
17461765 _flags = (textured ? IS_TEXTURED : 0) | (transparent ? IS_TRANSPARENT : 0) | (culled ? IS_CULLED : 0) |
1747− (unlit ? IS_UNLIT : 0) | (depthBias ? HAS_DEPTH_BIAS : 0);
1766+ (unlit ? IS_UNLIT : 0) | (depthBias ? HAS_DEPTH_BIAS : 0) | (fading ? IS_FADING : 0);
17481767 }
17491768
17501769 SimpleProgramKey(int bitmask) : _flags(bitmask) {}
@@ -1818,23 +1837,8 @@ void GeometryCache::bindSimpleProgram(gpu::Batch& batch, bool textured, bool tra · 67 unmodified lines
18181837 }
18191838 }
18201839
1821−gpu::PipelinePointer GeometryCache::getSimplePipeline(bool textured, bool transparent, bool culled, bool unlit, bool depthBiased) {
1822− SimpleProgramKey config { textured, transparent, culled, unlit, depthBiased };
1823−
1824− // Compile the shaders
1825− static std::once_flag once;
1826− std::call_once(once, [&]() {
1827− auto VS = gpu::Shader::createVertex(std::string(simple_vert));
1828− auto PS = gpu::Shader::createPixel(std::string(simple_textured_frag));
1829− auto PSUnlit = gpu::Shader::createPixel(std::string(simple_textured_unlit_frag));
1830−
1831− _simpleShader = gpu::Shader::createProgram(VS, PS);
1832− _unlitShader = gpu::Shader::createProgram(VS, PSUnlit);
1833−
1834− gpu::Shader::BindingSet slotBindings;
1835− gpu::Shader::makeProgram(*_simpleShader, slotBindings);
1836− gpu::Shader::makeProgram(*_unlitShader, slotBindings);
1837− });
1840+gpu::PipelinePointer GeometryCache::getSimplePipeline(bool textured, bool transparent, bool culled, bool unlit, bool depthBiased, bool fading) {
1841+ SimpleProgramKey config { textured, transparent, culled, unlit, depthBiased, fading };
18381842
18391843 // If the pipeline already exists, return it
18401844 auto it = _simplePrograms.find(config);
@@ -1842,6 +1846,37 @@ gpu::PipelinePointer GeometryCache::getSimplePipeline(bool textured, bool transp · 1 unmodified line
18421846 return it.value();
18431847 }
18441848
1849+ // Compile the shaders
1850+ if (!fading) {
1851+ static std::once_flag once;
1852+ std::call_once(once, [&]() {
1853+ auto VS = gpu::Shader::createVertex(std::string(simple_vert));
1854+ auto PS = gpu::Shader::createPixel(std::string(simple_textured_frag));
1855+ auto PSUnlit = gpu::Shader::createPixel(std::string(simple_textured_unlit_frag));
1856+
1857+ _simpleShader = gpu::Shader::createProgram(VS, PS);
1858+ _unlitShader = gpu::Shader::createProgram(VS, PSUnlit);
1859+
1860+ gpu::Shader::BindingSet slotBindings;
1861+ gpu::Shader::makeProgram(*_simpleShader, slotBindings);
1862+ gpu::Shader::makeProgram(*_unlitShader, slotBindings);
1863+ });
1864+ } else {
1865+ static std::once_flag once;
1866+ std::call_once(once, [&]() {
1867+ auto VS = gpu::Shader::createVertex(std::string(simple_fade_vert));
1868+ auto PS = gpu::Shader::createPixel(std::string(simple_textured_fade_frag));
1869+ auto PSUnlit = gpu::Shader::createPixel(std::string(simple_textured_unlit_fade_frag));
1870+
1871+ _simpleFadeShader = gpu::Shader::createProgram(VS, PS);
1872+ _unlitFadeShader = gpu::Shader::createProgram(VS, PSUnlit);
1873+
1874+ gpu::Shader::BindingSet slotBindings;
1875+ gpu::Shader::makeProgram(*_simpleFadeShader, slotBindings);
1876+ gpu::Shader::makeProgram(*_unlitFadeShader, slotBindings);
1877+ });
1878+ }
1879+
18451880 // If the pipeline did not exist, make it
18461881 auto state = std::make_shared<gpu::State>();
18471882 if (config.isCulled()) {
@@ -1858,7 +1893,7 @@ gpu::PipelinePointer GeometryCache::getSimplePipeline(bool textured, bool transp · 10 unmodified lines
18581893 gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA,
18591894 gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
18601895
1861− gpu::ShaderPointer program = (config.isUnlit()) ? _unlitShader : _simpleShader;
1896+ gpu::ShaderPointer program = (config.isUnlit()) ? (config.isFading() ? _unlitFadeShader : _unlitShader) : (config.isFading() ? _simpleFadeShader : _simpleShader);
18621897 gpu::PipelinePointer pipeline = gpu::Pipeline::create(program, state);
18631898 _simplePrograms.insert(config, pipeline);
18641899 return pipeline;
@@ -1900,19 +1935,23 @@ void renderInstances(gpu::Batch& batch, const glm::vec4& color, bool isWire, · 35 unmodified lines
19001935 }
19011936
19021937 void GeometryCache::renderSolidShapeInstance(gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
1938+ assert(pipeline != nullptr);
19031939 renderInstances(batch, color, false, pipeline, shape);
19041940 }
19051941
19061942 void GeometryCache::renderWireShapeInstance(gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
1943+ assert(pipeline != nullptr);
19071944 renderInstances(batch, color, true, pipeline, shape);
19081945 }
19091946
19101947
19111948 void GeometryCache::renderSolidSphereInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
1949+ assert(pipeline != nullptr);
19121950 renderInstances(batch, color, false, pipeline, GeometryCache::Sphere);
19131951 }
19141952
19151953 void GeometryCache::renderWireSphereInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
1954+ assert(pipeline != nullptr);
19161955 renderInstances(batch, color, true, pipeline, GeometryCache::Sphere);
19171956 }
19181957
@@ -1921,6 +1960,7 @@ void GeometryCache::renderWireSphereInstance(gpu::Batch& batch, const glm::vec4& · 2 unmodified lines
19211960 //#define DEBUG_SHAPES
19221961
19231962 void GeometryCache::renderSolidCubeInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
1963+ assert(pipeline != nullptr);
19241964 #ifdef DEBUG_SHAPES
19251965 static auto startTime = usecTimestampNow();
19261966 renderInstances(INSTANCE_NAME, batch, color, pipeline, [](gpu::Batch& batch, gpu::Batch::NamedBatchData& data) {
@@ -1960,5 +2000,6 @@ void GeometryCache::renderSolidCubeInstance(gpu::Batch& batch, const glm::vec4& · 33 unmodified lines
19602000
19612001 void GeometryCache::renderWireCubeInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
19622002 static const std::string INSTANCE_NAME = __FUNCTION__;
2003+ assert(pipeline != nullptr);
19632004 renderInstances(batch, color, true, pipeline, GeometryCache::Cube);
19642005 }