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
6060 static const gpu::Element POSITION_ELEMENT { gpu::VEC3, gpu::FLOAT, gpu::XYZ };
6161 static const gpu::Element NORMAL_ELEMENT { gpu::VEC3, gpu::FLOAT, gpu::XYZ };
6262 static const gpu::Element COLOR_ELEMENT { gpu::VEC4, gpu::NUINT8, gpu::RGBA };
63+static const gpu::Element TEXCOORD4_ELEMENT { gpu::VEC4, gpu::HALF, gpu::XYZW };
6364
6465 static gpu::Stream::FormatPointer SOLID_STREAM_FORMAT;
6566 static gpu::Stream::FormatPointer INSTANCED_SOLID_STREAM_FORMAT;
67+static gpu::Stream::FormatPointer INSTANCED_SOLID_FADE_STREAM_FORMAT;
6668
6769 static const uint SHAPE_VERTEX_STRIDE = sizeof(glm::vec3) * 2; // vertices and normals
6870 static const uint SHAPE_NORMALS_OFFSET = sizeof(glm::vec3);
@@ -451,6 +453,19 @@ gpu::Stream::FormatPointer& getInstancedSolidStreamFormat() { · 382 unmodified lines
451453 return INSTANCED_SOLID_STREAM_FORMAT;
452454 }
453455
456+gpu::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+
454469 QHash<SimpleProgramKey, gpu::PipelinePointer> GeometryCache::_simplePrograms;
455470
456471 gpu::ShaderPointer GeometryCache::_simpleShader;
@@ -535,11 +550,6 @@ void GeometryCache::releaseID(int id) { · 78 unmodified lines
535550 _registeredGridBuffers.remove(id);
536551 }
537552
538−void setupBatchInstance(gpu::Batch& batch, gpu::BufferPointer colorBuffer) {
539− gpu::BufferView colorView(colorBuffer, COLOR_ELEMENT);
540− batch.setInputBuffer(gpu::Stream::COLOR, colorView);
541−}
542−
543553 void GeometryCache::initializeShapePipelines() {
544554 if (!_simpleOpaquePipeline) {
545555 _simpleOpaquePipeline = getShapePipeline(false, false, true, false);
@@ -564,7 +574,7 @@ render::ShapePipelinePointer GeometryCache::getFadingShapePipeline(bool textured · 18 unmodified lines
564574 bool unlit, bool depthBias) {
565575 auto fadeEffect = DependencyManager::get<FadeEffect>();
566576 auto fadeBatchSetter = fadeEffect->getBatchSetter();
567− auto fadeItemSetter = fadeEffect->getItemSetter();
577+ auto fadeItemSetter = fadeEffect->getItemStoredSetter();
568578 return std::make_shared<render::ShapePipeline>(getSimplePipeline(textured, transparent, culled, unlit, depthBias, true), nullptr,
569579 [fadeBatchSetter, fadeItemSetter](const render::ShapePipeline& shapePipeline, gpu::Batch& batch, render::Args* args) {
570580 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
592602 _shapes[shape].drawWire(batch);
593603 }
594604
605+void setupBatchInstance(gpu::Batch& batch, gpu::BufferPointer colorBuffer) {
606+ gpu::BufferView colorView(colorBuffer, COLOR_ELEMENT);
607+ batch.setInputBuffer(gpu::Stream::COLOR, colorView);
608+}
609+
595610 void GeometryCache::renderShapeInstances(gpu::Batch& batch, Shape shape, size_t count, gpu::BufferPointer& colorBuffer) {
596611 batch.setInputFormat(getInstancedSolidStreamFormat());
597612 setupBatchInstance(batch, colorBuffer);
@@ -604,6 +619,32 @@ void GeometryCache::renderWireShapeInstances(gpu::Batch& batch, Shape shape, siz · 6 unmodified lines
604619 _shapes[shape].drawWireInstances(batch, count);
605620 }
606621
622+void 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+
634+void 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+
641+void 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+
607648 void GeometryCache::renderCube(gpu::Batch& batch) {
608649 renderShape(batch, Cube);
609650 }
@@ -2046,12 +2087,62 @@ void renderInstances(RenderArgs* args, gpu::Batch& batch, const glm::vec4& color · 1436 unmodified lines
20462087
20472088 if (isWire) {
20482089 DependencyManager::get<GeometryCache>()->renderWireShapeInstances(batch, shape, data.count(), data.buffers[INSTANCE_COLOR_BUFFER]);
2049− } else {
2090+ }
2091+ else {
20502092 DependencyManager::get<GeometryCache>()->renderShapeInstances(batch, shape, data.count(), data.buffers[INSTANCE_COLOR_BUFFER]);
20512093 }
20522094 });
20532095 }
20542096
2097+static const size_t INSTANCE_FADE_BUFFER1 = 1;
2098+static const size_t INSTANCE_FADE_BUFFER2 = 2;
2099+static const size_t INSTANCE_FADE_BUFFER3 = 3;
2100+
2101+void 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+
20552146 void GeometryCache::renderSolidShapeInstance(RenderArgs* args, gpu::Batch& batch, GeometryCache::Shape shape, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
20562147 assert(pipeline != nullptr);
20572148 renderInstances(args, batch, color, false, pipeline, shape);
@@ -2062,6 +2153,20 @@ void GeometryCache::renderWireShapeInstance(RenderArgs* args, gpu::Batch& batch, · 4 unmodified lines
20622153 renderInstances(args, batch, color, true, pipeline, shape);
20632154 }
20642155
2156+void 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+
2163+void 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+
20652170 void GeometryCache::renderSolidSphereInstance(RenderArgs* args, gpu::Batch& batch, const glm::vec4& color, const render::ShapePipelinePointer& pipeline) {
20662171 assert(pipeline != nullptr);
20672172 renderInstances(args, batch, color, false, pipeline, GeometryCache::Sphere);