Showing the changes to libraries/render-utils/src/GeometryCache.cpp (1 of 8 files). Show all
Unified · Split
| @@ -24,6 +24,7 @@ · 23 unmodified lines | ||
| 24 | 24 | |
| 25 | 25 | #include "TextureCache.h" |
| 26 | 26 | #include "RenderUtilsLogging.h" |
| 27 | +#include "FadeEffect.h" | |
| 27 | 28 | |
| 28 | 29 | #include "gpu/StandardShaderLib.h" |
| 29 | 30 | |
| @@ -35,6 +36,9 @@ · 5 unmodified lines | ||
| 35 | 36 | #include "simple_vert.h" |
| 36 | 37 | #include "simple_textured_frag.h" |
| 37 | 38 | #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" | |
| 38 | 42 | #include "simple_opaque_web_browser_frag.h" |
| 39 | 43 | #include "simple_opaque_web_browser_overlay_frag.h" |
| 40 | 44 | #include "simple_transparent_web_browser_frag.h" |
| @@ -403,30 +407,13 @@ gpu::Stream::FormatPointer& getInstancedSolidStreamFormat() { · 362 unmodified lines | ||
| 403 | 407 | |
| 404 | 408 | render::ShapePipelinePointer GeometryCache::_simpleOpaquePipeline; |
| 405 | 409 | render::ShapePipelinePointer GeometryCache::_simpleTransparentPipeline; |
| 410 | +render::ShapePipelinePointer GeometryCache::_simpleOpaqueFadePipeline; | |
| 411 | +render::ShapePipelinePointer GeometryCache::_simpleTransparentFadePipeline; | |
| 406 | 412 | render::ShapePipelinePointer GeometryCache::_simpleWirePipeline; |
| 407 | 413 | |
| 408 | 414 | GeometryCache::GeometryCache() : |
| 409 | 415 | _nextID(0) { |
| 410 | 416 | 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) {}); | |
| 430 | 417 | } |
| 431 | 418 | |
| 432 | 419 | GeometryCache::~GeometryCache() { |
| @@ -471,6 +458,35 @@ void setupBatchInstance(gpu::Batch& batch, gpu::BufferPointer colorBuffer) { · 38 unmodified lines | ||
| 471 | 458 | batch.setInputBuffer(gpu::Stream::COLOR, colorView); |
| 472 | 459 | } |
| 473 | 460 | |
| 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 | + | |
| 474 | 490 | void GeometryCache::renderShape(gpu::Batch& batch, Shape shape) { |
| 475 | 491 | batch.setInputFormat(getSolidStreamFormat()); |
| 476 | 492 | _shapes[shape].draw(batch); |
| @@ -1714,6 +1730,7 @@ public: · 1237 unmodified lines | ||
| 1714 | 1730 | IS_CULLED_FLAG, |
| 1715 | 1731 | IS_UNLIT_FLAG, |
| 1716 | 1732 | HAS_DEPTH_BIAS_FLAG, |
| 1733 | + IS_FADING_FLAG, | |
| 1717 | 1734 | |
| 1718 | 1735 | NUM_FLAGS, |
| 1719 | 1736 | }; |
| @@ -1724,6 +1741,7 @@ public: · 4 unmodified lines | ||
| 1724 | 1741 | IS_CULLED = (1 << IS_CULLED_FLAG), |
| 1725 | 1742 | IS_UNLIT = (1 << IS_UNLIT_FLAG), |
| 1726 | 1743 | HAS_DEPTH_BIAS = (1 << HAS_DEPTH_BIAS_FLAG), |
| 1744 | + IS_FADING = (1 << IS_FADING_FLAG), | |
| 1727 | 1745 | }; |
| 1728 | 1746 | typedef unsigned short Flags; |
| 1729 | 1747 | |
| @@ -1734,6 +1752,7 @@ public: · 4 unmodified lines | ||
| 1734 | 1752 | bool isCulled() const { return isFlag(IS_CULLED); } |
| 1735 | 1753 | bool isUnlit() const { return isFlag(IS_UNLIT); } |
| 1736 | 1754 | bool hasDepthBias() const { return isFlag(HAS_DEPTH_BIAS); } |
| 1755 | + bool isFading() const { return isFlag(IS_FADING); } | |
| 1737 | 1756 | |
| 1738 | 1757 | Flags _flags = 0; |
| 1739 | 1758 | short _spare = 0; |
| @@ -1742,9 +1761,9 @@ public: · 2 unmodified lines | ||
| 1742 | 1761 | |
| 1743 | 1762 | |
| 1744 | 1763 | 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) { | |
| 1746 | 1765 | _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); | |
| 1748 | 1767 | } |
| 1749 | 1768 | |
| 1750 | 1769 | SimpleProgramKey(int bitmask) : _flags(bitmask) {} |
| @@ -1818,23 +1837,8 @@ void GeometryCache::bindSimpleProgram(gpu::Batch& batch, bool textured, bool tra · 67 unmodified lines | ||
| 1818 | 1837 | } |
| 1819 | 1838 | } |
| 1820 | 1839 | |
| 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 }; | |
| 1838 | 1842 | |
| 1839 | 1843 | // If the pipeline already exists, return it |
| 1840 | 1844 | auto it = _simplePrograms.find(config); |
| @@ -1842,6 +1846,37 @@ gpu::PipelinePointer GeometryCache::getSimplePipeline(bool textured, bool transp · 1 unmodified line | ||
| 1842 | 1846 | return it.value(); |
| 1843 | 1847 | } |
| 1844 | 1848 | |
| 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 | + | |
| 1845 | 1880 | // If the pipeline did not exist, make it |
| 1846 | 1881 | auto state = std::make_shared<gpu::State>(); |
| 1847 | 1882 | if (config.isCulled()) { |
| @@ -1858,7 +1893,7 @@ gpu::PipelinePointer GeometryCache::getSimplePipeline(bool textured, bool transp · 10 unmodified lines | ||
| 1858 | 1893 | gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, |
| 1859 | 1894 | gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE); |
| 1860 | 1895 | |
| 1861 | − gpu::ShaderPointer program = (config.isUnlit()) ? _unlitShader : _simpleShader; | |
| 1896 | + gpu::ShaderPointer program = (config.isUnlit()) ? (config.isFading() ? _unlitFadeShader : _unlitShader) : (config.isFading() ? _simpleFadeShader : _simpleShader); | |
| 1862 | 1897 | gpu::PipelinePointer pipeline = gpu::Pipeline::create(program, state); |
| 1863 | 1898 | _simplePrograms.insert(config, pipeline); |
| 1864 | 1899 | return pipeline; |
| @@ -1900,19 +1935,23 @@ void renderInstances(gpu::Batch& batch, const glm::vec4& color, bool isWire, · 35 unmodified lines | ||
| 1900 | 1935 | } |
| 1901 | 1936 | |
| 1902 | 1937 | void GeometryCache::renderSolidShapeInstance(gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) { |
| 1938 | + assert(pipeline != nullptr); | |
| 1903 | 1939 | renderInstances(batch, color, false, pipeline, shape); |
| 1904 | 1940 | } |
| 1905 | 1941 | |
| 1906 | 1942 | void GeometryCache::renderWireShapeInstance(gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) { |
| 1943 | + assert(pipeline != nullptr); | |
| 1907 | 1944 | renderInstances(batch, color, true, pipeline, shape); |
| 1908 | 1945 | } |
| 1909 | 1946 | |
| 1910 | 1947 | |
| 1911 | 1948 | void GeometryCache::renderSolidSphereInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) { |
| 1949 | + assert(pipeline != nullptr); | |
| 1912 | 1950 | renderInstances(batch, color, false, pipeline, GeometryCache::Sphere); |
| 1913 | 1951 | } |
| 1914 | 1952 | |
| 1915 | 1953 | void GeometryCache::renderWireSphereInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) { |
| 1954 | + assert(pipeline != nullptr); | |
| 1916 | 1955 | renderInstances(batch, color, true, pipeline, GeometryCache::Sphere); |
| 1917 | 1956 | } |
| 1918 | 1957 | |
| @@ -1921,6 +1960,7 @@ void GeometryCache::renderWireSphereInstance(gpu::Batch& batch, const glm::vec4& · 2 unmodified lines | ||
| 1921 | 1960 | //#define DEBUG_SHAPES |
| 1922 | 1961 | |
| 1923 | 1962 | void GeometryCache::renderSolidCubeInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) { |
| 1963 | + assert(pipeline != nullptr); | |
| 1924 | 1964 | #ifdef DEBUG_SHAPES |
| 1925 | 1965 | static auto startTime = usecTimestampNow(); |
| 1926 | 1966 | 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 | ||
| 1960 | 2000 | |
| 1961 | 2001 | void GeometryCache::renderWireCubeInstance(gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) { |
| 1962 | 2002 | static const std::string INSTANCE_NAME = __FUNCTION__; |
| 2003 | + assert(pipeline != nullptr); | |
| 1963 | 2004 | renderInstances(batch, color, true, pipeline, GeometryCache::Cube); |
| 1964 | 2005 | } |