History

Refactored Fade.slh to take into account differences between instanced draw and normal draw

Olivier Prat committed 9 years ago

Commit dbf0b641 parent 10a6ad228 files · +378 −85Browse files

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

Unified · Split

libraries/render-utils/src/GeometryCache.cpp+112−7
@@ -60,9 +60,11 @@ static const int VERTICES_PER_TRIANGLE = 3; · 59 unmodified lines
60static const gpu::Element POSITION_ELEMENT { gpu::VEC3, gpu::FLOAT, gpu::XYZ };60static const gpu::Element POSITION_ELEMENT { gpu::VEC3, gpu::FLOAT, gpu::XYZ };
61static const gpu::Element NORMAL_ELEMENT { gpu::VEC3, gpu::FLOAT, gpu::XYZ };61static const gpu::Element NORMAL_ELEMENT { gpu::VEC3, gpu::FLOAT, gpu::XYZ };
62static const gpu::Element COLOR_ELEMENT { gpu::VEC4, gpu::NUINT8, gpu::RGBA };62static const gpu::Element COLOR_ELEMENT { gpu::VEC4, gpu::NUINT8, gpu::RGBA };
63static const gpu::Element TEXCOORD4_ELEMENT { gpu::VEC4, gpu::HALF, gpu::XYZW };
63 64
64static gpu::Stream::FormatPointer SOLID_STREAM_FORMAT;65static gpu::Stream::FormatPointer SOLID_STREAM_FORMAT;
65static gpu::Stream::FormatPointer INSTANCED_SOLID_STREAM_FORMAT;66static gpu::Stream::FormatPointer INSTANCED_SOLID_STREAM_FORMAT;
67static gpu::Stream::FormatPointer INSTANCED_SOLID_FADE_STREAM_FORMAT;
66 68
67static const uint SHAPE_VERTEX_STRIDE = sizeof(glm::vec3) * 2; // vertices and normals69static const uint SHAPE_VERTEX_STRIDE = sizeof(glm::vec3) * 2; // vertices and normals
68static const uint SHAPE_NORMALS_OFFSET = sizeof(glm::vec3);70static const uint SHAPE_NORMALS_OFFSET = sizeof(glm::vec3);
@@ -451,6 +453,19 @@ gpu::Stream::FormatPointer& getInstancedSolidStreamFormat() { · 382 unmodified lines
451 return INSTANCED_SOLID_STREAM_FORMAT;453 return INSTANCED_SOLID_STREAM_FORMAT;
452}454}
453 455
456gpu::Stream::FormatPointer& getInstancedSolidFadeStreamFormat() {
457 if (!INSTANCED_SOLID_FADE_STREAM_FORMAT) {
458 INSTANCED_SOLID_FADE_STREAM_FORMAT = std::make_shared<gpu::Stream::Format>(); // 1 for everyone
459 INSTANCED_SOLID_FADE_STREAM_FORMAT->setAttribute(gpu::Stream::POSITION, gpu::Stream::POSITION, POSITION_ELEMENT);
460 INSTANCED_SOLID_FADE_STREAM_FORMAT->setAttribute(gpu::Stream::NORMAL, gpu::Stream::NORMAL, NORMAL_ELEMENT);
461 INSTANCED_SOLID_FADE_STREAM_FORMAT->setAttribute(gpu::Stream::COLOR, gpu::Stream::COLOR, COLOR_ELEMENT, 0, gpu::Stream::PER_INSTANCE);
462 INSTANCED_SOLID_FADE_STREAM_FORMAT->setAttribute(gpu::Stream::TEXCOORD2, gpu::Stream::TEXCOORD2, TEXCOORD4_ELEMENT, 0, gpu::Stream::PER_INSTANCE);
463 INSTANCED_SOLID_FADE_STREAM_FORMAT->setAttribute(gpu::Stream::TEXCOORD3, gpu::Stream::TEXCOORD3, TEXCOORD4_ELEMENT, 0, gpu::Stream::PER_INSTANCE);
464 INSTANCED_SOLID_FADE_STREAM_FORMAT->setAttribute(gpu::Stream::TEXCOORD4, gpu::Stream::TEXCOORD4, TEXCOORD4_ELEMENT, 0, gpu::Stream::PER_INSTANCE);
465 }
466 return INSTANCED_SOLID_FADE_STREAM_FORMAT;
467}
468
454QHash<SimpleProgramKey, gpu::PipelinePointer> GeometryCache::_simplePrograms;469QHash<SimpleProgramKey, gpu::PipelinePointer> GeometryCache::_simplePrograms;
455 470
456gpu::ShaderPointer GeometryCache::_simpleShader;471gpu::ShaderPointer GeometryCache::_simpleShader;
@@ -535,11 +550,6 @@ void GeometryCache::releaseID(int id) { · 78 unmodified lines
535 _registeredGridBuffers.remove(id);550 _registeredGridBuffers.remove(id);
536}551}
537 552
538void setupBatchInstance(gpu::Batch& batch, gpu::BufferPointer colorBuffer) {
539 gpu::BufferView colorView(colorBuffer, COLOR_ELEMENT);
540 batch.setInputBuffer(gpu::Stream::COLOR, colorView);
541}
542
543void GeometryCache::initializeShapePipelines() {553void GeometryCache::initializeShapePipelines() {
544 if (!_simpleOpaquePipeline) {554 if (!_simpleOpaquePipeline) {
545 _simpleOpaquePipeline = getShapePipeline(false, false, true, false);555 _simpleOpaquePipeline = getShapePipeline(false, false, true, false);
@@ -564,7 +574,7 @@ render::ShapePipelinePointer GeometryCache::getFadingShapePipeline(bool textured · 18 unmodified lines
564 bool unlit, bool depthBias) {574 bool unlit, bool depthBias) {
565 auto fadeEffect = DependencyManager::get<FadeEffect>();575 auto fadeEffect = DependencyManager::get<FadeEffect>();
566 auto fadeBatchSetter = fadeEffect->getBatchSetter();576 auto fadeBatchSetter = fadeEffect->getBatchSetter();
567 auto fadeItemSetter = fadeEffect->getItemSetter();577 auto fadeItemSetter = fadeEffect->getItemStoredSetter();
568 return std::make_shared<render::ShapePipeline>(getSimplePipeline(textured, transparent, culled, unlit, depthBias, true), nullptr,578 return std::make_shared<render::ShapePipeline>(getSimplePipeline(textured, transparent, culled, unlit, depthBias, true), nullptr,
569 [fadeBatchSetter, fadeItemSetter](const render::ShapePipeline& shapePipeline, gpu::Batch& batch, render::Args* args) {579 [fadeBatchSetter, fadeItemSetter](const render::ShapePipeline& shapePipeline, gpu::Batch& batch, render::Args* args) {
570 batch.setResourceTexture(render::ShapePipeline::Slot::MAP::ALBEDO, DependencyManager::get<TextureCache>()->getWhiteTexture());580 batch.setResourceTexture(render::ShapePipeline::Slot::MAP::ALBEDO, DependencyManager::get<TextureCache>()->getWhiteTexture());
@@ -592,6 +602,11 @@ void GeometryCache::renderWireShape(gpu::Batch& batch, Shape shape) { · 21 unmodified lines
592 _shapes[shape].drawWire(batch);602 _shapes[shape].drawWire(batch);
593}603}
594 604
605void setupBatchInstance(gpu::Batch& batch, gpu::BufferPointer colorBuffer) {
606 gpu::BufferView colorView(colorBuffer, COLOR_ELEMENT);
607 batch.setInputBuffer(gpu::Stream::COLOR, colorView);
608}
609
595void GeometryCache::renderShapeInstances(gpu::Batch& batch, Shape shape, size_t count, gpu::BufferPointer& colorBuffer) {610void GeometryCache::renderShapeInstances(gpu::Batch& batch, Shape shape, size_t count, gpu::BufferPointer& colorBuffer) {
596 batch.setInputFormat(getInstancedSolidStreamFormat());611 batch.setInputFormat(getInstancedSolidStreamFormat());
597 setupBatchInstance(batch, colorBuffer);612 setupBatchInstance(batch, colorBuffer);
@@ -604,6 +619,32 @@ void GeometryCache::renderWireShapeInstances(gpu::Batch& batch, Shape shape, siz · 6 unmodified lines
604 _shapes[shape].drawWireInstances(batch, count);619 _shapes[shape].drawWireInstances(batch, count);
605}620}
606 621
622void setupBatchFadeInstance(gpu::Batch& batch, gpu::BufferPointer colorBuffer,
623 gpu::BufferPointer& fadeBuffer1, gpu::BufferPointer& fadeBuffer2, gpu::BufferPointer& fadeBuffer3) {
624 gpu::BufferView colorView(colorBuffer, COLOR_ELEMENT);
625 gpu::BufferView texCoord2View(fadeBuffer1, TEXCOORD4_ELEMENT);
626 gpu::BufferView texCoord3View(fadeBuffer2, TEXCOORD4_ELEMENT);
627 gpu::BufferView texCoord4View(fadeBuffer3, TEXCOORD4_ELEMENT);
628 batch.setInputBuffer(gpu::Stream::COLOR, colorView);
629 batch.setInputBuffer(gpu::Stream::TEXCOORD2, texCoord2View);
630 batch.setInputBuffer(gpu::Stream::TEXCOORD3, texCoord3View);
631 batch.setInputBuffer(gpu::Stream::TEXCOORD4, texCoord4View);
632}
633
634void GeometryCache::renderFadeShapeInstances(gpu::Batch& batch, Shape shape, size_t count, gpu::BufferPointer& colorBuffer,
635 gpu::BufferPointer& fadeBuffer1, gpu::BufferPointer& fadeBuffer2, gpu::BufferPointer& fadeBuffer3) {
636 batch.setInputFormat(getInstancedSolidFadeStreamFormat());
637 setupBatchFadeInstance(batch, colorBuffer, fadeBuffer1, fadeBuffer2, fadeBuffer3);
638 _shapes[shape].drawInstances(batch, count);
639}
640
641void GeometryCache::renderWireFadeShapeInstances(gpu::Batch& batch, Shape shape, size_t count, gpu::BufferPointer& colorBuffer,
642 gpu::BufferPointer& fadeBuffer1, gpu::BufferPointer& fadeBuffer2, gpu::BufferPointer& fadeBuffer3) {
643 batch.setInputFormat(getInstancedSolidFadeStreamFormat());
644 setupBatchFadeInstance(batch, colorBuffer, fadeBuffer1, fadeBuffer2, fadeBuffer3);
645 _shapes[shape].drawWireInstances(batch, count);
646}
647
607void GeometryCache::renderCube(gpu::Batch& batch) {648void GeometryCache::renderCube(gpu::Batch& batch) {
608 renderShape(batch, Cube);649 renderShape(batch, Cube);
609}650}
@@ -2046,12 +2087,62 @@ void renderInstances(RenderArgs* args, gpu::Batch& batch, const glm::vec4& color · 1436 unmodified lines
2046 2087
2047 if (isWire) {2088 if (isWire) {
2048 DependencyManager::get<GeometryCache>()->renderWireShapeInstances(batch, shape, data.count(), data.buffers[INSTANCE_COLOR_BUFFER]);2089 DependencyManager::get<GeometryCache>()->renderWireShapeInstances(batch, shape, data.count(), data.buffers[INSTANCE_COLOR_BUFFER]);
2049 } else {2090 }
2091 else {
2050 DependencyManager::get<GeometryCache>()->renderShapeInstances(batch, shape, data.count(), data.buffers[INSTANCE_COLOR_BUFFER]);2092 DependencyManager::get<GeometryCache>()->renderShapeInstances(batch, shape, data.count(), data.buffers[INSTANCE_COLOR_BUFFER]);
2051 }2093 }
2052 });2094 });
2053}2095}
2054 2096
2097static const size_t INSTANCE_FADE_BUFFER1 = 1;
2098static const size_t INSTANCE_FADE_BUFFER2 = 2;
2099static const size_t INSTANCE_FADE_BUFFER3 = 3;
2100
2101void renderFadeInstances(RenderArgs* args, gpu::Batch& batch, const glm::vec4& color, int fadeCategory, float fadeThreshold,
2102 const glm::vec3& fadeNoiseOffset, const glm::vec3& fadeBaseOffset, const glm::vec3& fadeBaseInvSize, bool isWire,
2103 const render::ShapePipelinePointer& pipeline, GeometryCache::Shape shape) {
2104 // Add pipeline to name
2105 std::string instanceName = (isWire ? "wire_fade_shapes_" : "solid_fade_shapes_") + std::to_string(shape) + "_" + std::to_string(std::hash<render::ShapePipelinePointer>()(pipeline));
2106
2107 // Add color to named buffer
2108 {
2109 gpu::BufferPointer instanceColorBuffer = batch.getNamedBuffer(instanceName, INSTANCE_COLOR_BUFFER);
2110 auto compactColor = toCompactColor(color);
2111 instanceColorBuffer->append(compactColor);
2112 }
2113 // Add fade parameters to named buffers
2114 {
2115 gpu::BufferPointer fadeBuffer1 = batch.getNamedBuffer(instanceName, INSTANCE_FADE_BUFFER1);
2116 gpu::BufferPointer fadeBuffer2 = batch.getNamedBuffer(instanceName, INSTANCE_FADE_BUFFER2);
2117 gpu::BufferPointer fadeBuffer3 = batch.getNamedBuffer(instanceName, INSTANCE_FADE_BUFFER3);
2118 // Pack parameters in 3 vec4s
2119 glm::vec4 fadeData1;
2120 glm::vec4 fadeData2;
2121 glm::vec4 fadeData3;
2122 FadeEffect::packToAttributes(fadeCategory, fadeThreshold, fadeNoiseOffset, fadeBaseOffset, fadeBaseInvSize,
2123 fadeData1, fadeData2, fadeData3);
2124 fadeBuffer1->append(fadeData1);
2125 fadeBuffer2->append(fadeData2);
2126 fadeBuffer3->append(fadeData3);
2127 }
2128
2129 // Add call to named buffer
2130 batch.setupNamedCalls(instanceName, [args, isWire, pipeline, shape](gpu::Batch& batch, gpu::Batch::NamedBatchData& data) {
2131 auto& buffers = data.buffers;
2132 batch.setPipeline(pipeline->pipeline);
2133 pipeline->prepare(batch, args);
2134
2135 if (isWire) {
2136 DependencyManager::get<GeometryCache>()->renderWireFadeShapeInstances(batch, shape, data.count(),
2137 buffers[INSTANCE_COLOR_BUFFER], buffers[INSTANCE_FADE_BUFFER1], buffers[INSTANCE_FADE_BUFFER2], buffers[INSTANCE_FADE_BUFFER3]);
2138 }
2139 else {
2140 DependencyManager::get<GeometryCache>()->renderFadeShapeInstances(batch, shape, data.count(),
2141 buffers[INSTANCE_COLOR_BUFFER], buffers[INSTANCE_FADE_BUFFER1], buffers[INSTANCE_FADE_BUFFER2], buffers[INSTANCE_FADE_BUFFER3]);
2142 }
2143 });
2144}
2145
2055void GeometryCache::renderSolidShapeInstance(RenderArgs* args, gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {2146void GeometryCache::renderSolidShapeInstance(RenderArgs* args, gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
2056 assert(pipeline != nullptr);2147 assert(pipeline != nullptr);
2057 renderInstances(args, batch, color, false, pipeline, shape);2148 renderInstances(args, batch, color, false, pipeline, shape);
@@ -2062,6 +2153,20 @@ void GeometryCache::renderWireShapeInstance(RenderArgs* args, gpu::Batch& batch, · 4 unmodified lines
2062 renderInstances(args, batch, color, true, pipeline, shape);2153 renderInstances(args, batch, color, true, pipeline, shape);
2063}2154}
2064 2155
2156void GeometryCache::renderSolidFadeShapeInstance(RenderArgs* args, gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color,
2157 int fadeCategory, float fadeThreshold, const glm::vec3& fadeNoiseOffset, const glm::vec3& fadeBaseOffset, const glm::vec3& fadeBaseInvSize,
2158 const render::ShapePipelinePointer& pipeline) {
2159 assert(pipeline != nullptr);
2160 renderFadeInstances(args, batch, color, fadeCategory, fadeThreshold, fadeNoiseOffset, fadeBaseOffset, fadeBaseInvSize, false, pipeline, shape);
2161}
2162
2163void GeometryCache::renderWireFadeShapeInstance(RenderArgs* args, gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color,
2164 int fadeCategory, float fadeThreshold, const glm::vec3& fadeNoiseOffset, const glm::vec3& fadeBaseOffset, const glm::vec3& fadeBaseInvSize,
2165 const render::ShapePipelinePointer& pipeline) {
2166 assert(pipeline != nullptr);
2167 renderFadeInstances(args, batch, color, fadeCategory, fadeThreshold, fadeNoiseOffset, fadeBaseOffset, fadeBaseInvSize, true, pipeline, shape);
2168}
2169
2065void GeometryCache::renderSolidSphereInstance(RenderArgs* args, gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {2170void GeometryCache::renderSolidSphereInstance(RenderArgs* args, gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
2066 assert(pipeline != nullptr);2171 assert(pipeline != nullptr);
2067 renderInstances(args, batch, color, false, pipeline, GeometryCache::Sphere);2172 renderInstances(args, batch, color, false, pipeline, GeometryCache::Sphere);