From 49779754ff063a57d6b57b97aeb6f64a3be1967d Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 12 Jul 2017 15:50:06 +0200 Subject: [PATCH] Remove unecessary shaders and apply the custom shader pipeline to polyvox --- .../RenderableParticleEffectEntityItem.cpp | 10 +-- .../src/RenderablePolyVoxEntityItem.cpp | 68 ++++++++++++++++--- .../src/RenderablePolyVoxEntityItem.h | 17 ++++- .../src/untextured_particle.slf | 19 ------ .../src/untextured_particle.slv | 25 ------- tests/shaders/src/main.cpp | 3 - 6 files changed, 76 insertions(+), 66 deletions(-) delete mode 100644 libraries/entities-renderer/src/untextured_particle.slf delete mode 100644 libraries/entities-renderer/src/untextured_particle.slv diff --git a/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.cpp b/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.cpp index 276ffb279f..a3c0d9877e 100644 --- a/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.cpp @@ -20,8 +20,6 @@ #include "RenderableParticleEffectEntityItem.h" -#include "untextured_particle_vert.h" -#include "untextured_particle_frag.h" #include "textured_particle_vert.h" #include "textured_particle_frag.h" @@ -32,14 +30,14 @@ public: static uint8_t CUSTOM_PIPELINE_NUMBER; static render::ShapePipelinePointer shapePipelineFactory(const render::ShapePlumber& plumber, const render::ShapeKey& key); - static std::weak_ptr _texturedPipeline; - static void registerShapePipeline() { if (!CUSTOM_PIPELINE_NUMBER) { - ParticlePayloadData::CUSTOM_PIPELINE_NUMBER = render::ShapePipeline::registerCustomShapePipelineFactory(ParticlePayloadData::shapePipelineFactory); + CUSTOM_PIPELINE_NUMBER = render::ShapePipeline::registerCustomShapePipelineFactory(shapePipelineFactory); } } + static std::weak_ptr _texturedPipeline; + template struct InterpolationData { T start; @@ -100,7 +98,6 @@ public: void setVisibleFlag(bool visibleFlag) { _visibleFlag = visibleFlag; } void render(RenderArgs* args) const { - assert(_pipeline); gpu::Batch& batch = *args->_batch; @@ -122,7 +119,6 @@ public: protected: Transform _modelTransform; AABox _bound; - PipelinePointer _pipeline; FormatPointer _vertexFormat { std::make_shared() }; BufferPointer _particleBuffer { std::make_shared() }; BufferView _uniformBuffer; diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index 88a5d2b873..364195ae0b 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -59,8 +59,8 @@ #include "EntityEditPacketSender.h" #include "PhysicalEntitySimulation.h" -gpu::PipelinePointer RenderablePolyVoxEntityItem::_pipeline = nullptr; -gpu::PipelinePointer RenderablePolyVoxEntityItem::_wireframePipeline = nullptr; +//gpu::PipelinePointer RenderablePolyVoxEntityItem::_pipeline = nullptr; +//gpu::PipelinePointer RenderablePolyVoxEntityItem::_wireframePipeline = nullptr; const float MARCHING_CUBE_COLLISION_HULL_OFFSET = 0.5; @@ -116,6 +116,10 @@ EntityItemPointer RenderablePolyVoxEntityItem::factory(const EntityItemID& entit EntityItemPointer entity{ new RenderablePolyVoxEntityItem(entityID) }; entity->setProperties(properties); std::static_pointer_cast(entity)->initializePolyVox(); + + // As we create the first Polyvox entity, let's register its special shapePipeline factory: + PolyVoxPayload::registerShapePipeline(); + return entity; } @@ -733,7 +737,7 @@ void RenderablePolyVoxEntityItem::render(RenderArgs* args) { return; } - if (!_pipeline) { + /* if (!_pipeline) { gpu::ShaderPointer vertexShader = gpu::Shader::createVertex(std::string(polyvox_vert)); gpu::ShaderPointer pixelShader = gpu::Shader::createPixel(std::string(polyvox_frag)); @@ -760,7 +764,7 @@ void RenderablePolyVoxEntityItem::render(RenderArgs* args) { PrepareStencil::testMaskDrawShape(*wireframeState); _wireframePipeline = gpu::Pipeline::create(program, wireframeState); - } + }*/ if (!_vertexFormat) { auto vf = std::make_shared(); @@ -772,9 +776,9 @@ void RenderablePolyVoxEntityItem::render(RenderArgs* args) { gpu::Batch& batch = *args->_batch; // Pick correct Pipeline - bool wireframe = (render::ShapeKey(args->_globalShapeKey).isWireframe()); - auto pipeline = (wireframe ? _wireframePipeline : _pipeline); - batch.setPipeline(pipeline); + // bool wireframe = (render::ShapeKey(args->_globalShapeKey).isWireframe()); + // auto pipeline = (wireframe ? _wireframePipeline : _pipeline); + // batch.setPipeline(pipeline); Transform transform(voxelToWorldMatrix()); batch.setModelTransform(transform); @@ -817,7 +821,7 @@ void RenderablePolyVoxEntityItem::render(RenderArgs* args) { batch.setResourceTexture(2, DependencyManager::get()->getWhiteTexture()); } - int voxelVolumeSizeLocation = pipeline->getProgram()->getUniforms().findLocation("voxelVolumeSize"); + int voxelVolumeSizeLocation = args->_shapePipeline->pipeline->getProgram()->getUniforms().findLocation("voxelVolumeSize"); batch._glUniform3f(voxelVolumeSizeLocation, voxelVolumeSize.x, voxelVolumeSize.y, voxelVolumeSize.z); batch.drawIndexed(gpu::TRIANGLES, (gpu::uint32)mesh->getNumIndices(), 0); @@ -848,6 +852,48 @@ void RenderablePolyVoxEntityItem::removeFromScene(const EntityItemPointer& self, render::Item::clearID(_myItem); } +uint8_t PolyVoxPayload::CUSTOM_PIPELINE_NUMBER = 0; + +std::shared_ptr PolyVoxPayload::_pipeline; +std::shared_ptr PolyVoxPayload::_wireframePipeline; + +render::ShapePipelinePointer PolyVoxPayload::shapePipelineFactory(const render::ShapePlumber& plumber, const render::ShapeKey& key) { + if (!_pipeline) { + gpu::ShaderPointer vertexShader = gpu::Shader::createVertex(std::string(polyvox_vert)); + gpu::ShaderPointer pixelShader = gpu::Shader::createPixel(std::string(polyvox_frag)); + + gpu::Shader::BindingSet slotBindings; + slotBindings.insert(gpu::Shader::Binding(std::string("materialBuffer"), PolyVoxPayload::MATERIAL_GPU_SLOT)); + slotBindings.insert(gpu::Shader::Binding(std::string("xMap"), 0)); + slotBindings.insert(gpu::Shader::Binding(std::string("yMap"), 1)); + slotBindings.insert(gpu::Shader::Binding(std::string("zMap"), 2)); + + gpu::ShaderPointer program = gpu::Shader::createProgram(vertexShader, pixelShader); + gpu::Shader::makeProgram(*program, slotBindings); + + auto state = std::make_shared(); + state->setCullMode(gpu::State::CULL_BACK); + state->setDepthTest(true, true, gpu::LESS_EQUAL); + PrepareStencil::testMaskDrawShape(*state); + + _pipeline = gpu::Pipeline::create(program, state); + + auto wireframeState = std::make_shared(); + wireframeState->setCullMode(gpu::State::CULL_BACK); + wireframeState->setDepthTest(true, true, gpu::LESS_EQUAL); + wireframeState->setFillMode(gpu::State::FILL_LINE); + PrepareStencil::testMaskDrawShape(*wireframeState); + + _wireframePipeline = gpu::Pipeline::create(program, wireframeState); + } + + if (key.isWireframe()) { + return std::make_shared(_wireframePipeline, nullptr, nullptr, nullptr); + } else { + return std::make_shared(_pipeline, nullptr, nullptr, nullptr); + } +} + namespace render { template <> const ItemKey payloadGetKey(const PolyVoxPayload::Pointer& payload) { return ItemKey::Builder::opaqueShape(); @@ -871,6 +917,10 @@ namespace render { payload->_owner->getRenderableInterface()->render(args); } } + + template <> const ShapeKey shapeGetShapeKey(const PolyVoxPayload::Pointer& payload) { + return ShapeKey::Builder().withCustom(PolyVoxPayload::CUSTOM_PIPELINE_NUMBER).build(); + } } @@ -1619,7 +1669,7 @@ void RenderablePolyVoxEntityItem::bonkNeighbors() { void RenderablePolyVoxEntityItem::locationChanged(bool tellPhysics) { EntityItem::locationChanged(tellPhysics); - if (!_pipeline || !render::Item::isValidID(_myItem)) { + if (/*!_pipeline || */!render::Item::isValidID(_myItem)) { return; } render::ScenePointer scene = AbstractViewStateInterface::instance()->getMain3DScene(); diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h index 45625ada6d..8f20a7a298 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.h @@ -28,6 +28,19 @@ class PolyVoxPayload { public: + + static uint8_t CUSTOM_PIPELINE_NUMBER; + static render::ShapePipelinePointer shapePipelineFactory(const render::ShapePlumber& plumber, const render::ShapeKey& key); + static void registerShapePipeline() { + if (!CUSTOM_PIPELINE_NUMBER) { + CUSTOM_PIPELINE_NUMBER = render::ShapePipeline::registerCustomShapePipelineFactory(shapePipelineFactory); + } + } + + static const int MATERIAL_GPU_SLOT = 3; + static std::shared_ptr _pipeline; + static std::shared_ptr _wireframePipeline; + PolyVoxPayload(EntityItemPointer owner) : _owner(owner), _bounds(AABox()) { } typedef render::Payload Payload; typedef Payload::DataPointer Pointer; @@ -40,6 +53,7 @@ namespace render { template <> const ItemKey payloadGetKey(const PolyVoxPayload::Pointer& payload); template <> const Item::Bound payloadGetBound(const PolyVoxPayload::Pointer& payload); template <> void payloadRender(const PolyVoxPayload::Pointer& payload, RenderArgs* args); + template <> const ShapeKey shapeGetShapeKey(const PolyVoxPayload::Pointer& payload); } @@ -168,10 +182,7 @@ private: NetworkTexturePointer _yTexture; NetworkTexturePointer _zTexture; - const int MATERIAL_GPU_SLOT = 3; render::ItemID _myItem{ render::Item::INVALID_ITEM_ID }; - static gpu::PipelinePointer _pipeline; - static gpu::PipelinePointer _wireframePipeline; ShapeInfo _shapeInfo; diff --git a/libraries/entities-renderer/src/untextured_particle.slf b/libraries/entities-renderer/src/untextured_particle.slf deleted file mode 100644 index a8062f0024..0000000000 --- a/libraries/entities-renderer/src/untextured_particle.slf +++ /dev/null @@ -1,19 +0,0 @@ -<@include gpu/Config.slh@> -<$VERSION_HEADER$> -// Generated on <$_SCRIBE_DATE$> -// fragment shader -// -// Copyright 2015 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -in vec4 varColor; -in vec2 varTexcoord; - -out vec4 outFragColor; - -void main(void) { - outFragColor = varColor; -} diff --git a/libraries/entities-renderer/src/untextured_particle.slv b/libraries/entities-renderer/src/untextured_particle.slv deleted file mode 100644 index 85f9d438bf..0000000000 --- a/libraries/entities-renderer/src/untextured_particle.slv +++ /dev/null @@ -1,25 +0,0 @@ -<@include gpu/Config.slh@> -<$VERSION_HEADER$> -// Generated on <$_SCRIBE_DATE$> -// -// particle vertex shader -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -<@include gpu/Inputs.slh@> -<@include gpu/Color.slh@> -<@include gpu/Transform.slh@> -<$declareStandardTransform()$> - -out vec4 _color; - -void main(void) { - // pass along the color - _color = colorToLinearRGBA(inColor); - - TransformCamera cam = getTransformCamera(); - TransformObject obj = getTransformObject(); - <$transformModelToClipPos(cam, obj, inPosition, gl_Position)$> -} \ No newline at end of file diff --git a/tests/shaders/src/main.cpp b/tests/shaders/src/main.cpp index d10ab1ddbe..9847e9f7b9 100644 --- a/tests/shaders/src/main.cpp +++ b/tests/shaders/src/main.cpp @@ -57,8 +57,6 @@ #include #include -#include -#include #include #include @@ -172,7 +170,6 @@ void QTestWindow::draw() { testShaderBuild(skin_model_normal_map_vert, model_translucent_frag); testShaderBuild(model_shadow_vert, model_shadow_frag); - testShaderBuild(untextured_particle_vert, untextured_particle_frag); testShaderBuild(textured_particle_vert, textured_particle_frag); /* FIXME: Bring back the ssao shader tests testShaderBuild(gaussian_blur_vertical_vert, gaussian_blur_frag);