1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "Renderer.hpp"
17 #include "Clipper.hpp"
18 #include "Polygon.hpp"
19 #include "Primitive.hpp"
21 #include "Pipeline/Constants.hpp"
22 #include "Pipeline/SpirvShader.hpp"
23 #include "Reactor/Reactor.hpp"
24 #include "System/Debug.hpp"
25 #include "System/Half.hpp"
26 #include "System/Math.hpp"
27 #include "System/Memory.hpp"
28 #include "System/Timer.hpp"
29 #include "Vulkan/VkConfig.h"
30 #include "Vulkan/VkDevice.hpp"
31 #include "Vulkan/VkFence.hpp"
32 #include "Vulkan/VkImageView.hpp"
33 #include "Vulkan/VkQueryPool.hpp"
35 #include "marl/containers.h"
36 #include "marl/defer.h"
37 #include "marl/trace.h"
42 unsigned int minPrimitives = 1;
43 unsigned int maxPrimitives = 1 << 21;
49 inline bool setBatchIndices(unsigned int batch[128][3], VkPrimitiveTopology topology, VkProvokingVertexModeEXT provokingVertexMode, T indices, unsigned int start, unsigned int triangleCount)
51 bool provokeFirst = (provokingVertexMode == VK_PROVOKING_VERTEX_MODE_FIRST_VERTEX_EXT);
55 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
58 auto pointBatch = &(batch[0][0]);
59 for(unsigned int i = 0; i < triangleCount; i++)
61 *pointBatch++ = indices[index++];
64 // Repeat the last index to allow for SIMD width overrun.
66 for(unsigned int i = 0; i < 3; i++)
68 *pointBatch++ = indices[index];
72 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
74 auto index = 2 * start;
75 for(unsigned int i = 0; i < triangleCount; i++)
77 batch[i][0] = indices[index + (provokeFirst ? 0 : 1)];
78 batch[i][1] = indices[index + (provokeFirst ? 1 : 0)];
79 batch[i][2] = indices[index + 1];
85 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
88 for(unsigned int i = 0; i < triangleCount; i++)
90 batch[i][0] = indices[index + (provokeFirst ? 0 : 1)];
91 batch[i][1] = indices[index + (provokeFirst ? 1 : 0)];
92 batch[i][2] = indices[index + 1];
98 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
100 auto index = 3 * start;
101 for(unsigned int i = 0; i < triangleCount; i++)
103 batch[i][0] = indices[index + (provokeFirst ? 0 : 2)];
104 batch[i][1] = indices[index + (provokeFirst ? 1 : 0)];
105 batch[i][2] = indices[index + (provokeFirst ? 2 : 1)];
111 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
114 for(unsigned int i = 0; i < triangleCount; i++)
116 batch[i][0] = indices[index + (provokeFirst ? 0 : 2)];
117 batch[i][1] = indices[index + ((start + i) & 1) + (provokeFirst ? 1 : 0)];
118 batch[i][2] = indices[index + (~(start + i) & 1) + (provokeFirst ? 1 : 0)];
124 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
126 auto index = start + 1;
127 for(unsigned int i = 0; i < triangleCount; i++)
129 batch[i][provokeFirst ? 0 : 2] = indices[index + 0];
130 batch[i][provokeFirst ? 1 : 0] = indices[index + 1];
131 batch[i][provokeFirst ? 2 : 1] = indices[0];
147 data = (DrawData *)allocate(sizeof(DrawData));
148 data->constants = &constants;
151 DrawCall::~DrawCall()
156 Renderer::Renderer(vk::Device *device)
159 VertexProcessor::setRoutineCacheSize(1024);
160 PixelProcessor::setRoutineCacheSize(1024);
161 SetupProcessor::setRoutineCacheSize(1024);
164 Renderer::~Renderer()
166 drawTickets.take().wait();
169 // Renderer objects have to be mem aligned to the alignment provided in the class declaration
170 void *Renderer::operator new(size_t size)
172 ASSERT(size == sizeof(Renderer)); // This operator can't be called from a derived class
173 return vk::allocate(sizeof(Renderer), alignof(Renderer), vk::DEVICE_MEMORY, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
176 void Renderer::operator delete(void *mem)
178 vk::deallocate(mem, vk::DEVICE_MEMORY);
181 void Renderer::draw(const sw::Context *context, VkIndexType indexType, unsigned int count, int baseVertex,
182 TaskEvents *events, int instanceID, int viewID, void *indexBuffer, const VkExtent3D &framebufferExtent,
183 PushConstantStorage const &pushConstants, bool update)
185 if(count == 0) { return; }
187 auto id = nextDrawID++;
188 MARL_SCOPED_EVENT("draw %d", id);
192 unsigned int minPrimitives = 1;
193 unsigned int maxPrimitives = 1 << 21;
194 if(count < minPrimitives || count > maxPrimitives)
201 int ms = context->sampleCount;
203 if(!context->multiSampleMask)
208 marl::Pool<sw::DrawCall>::Loan draw;
210 MARL_SCOPED_EVENT("drawCallPool.borrow()");
211 draw = drawCallPool.borrow();
217 MARL_SCOPED_EVENT("update");
218 vertexState = VertexProcessor::update(context);
219 setupState = SetupProcessor::update(context);
220 pixelState = PixelProcessor::update(context);
222 vertexRoutine = VertexProcessor::routine(vertexState, context->pipelineLayout, context->vertexShader, context->descriptorSets);
223 setupRoutine = SetupProcessor::routine(setupState);
224 pixelRoutine = PixelProcessor::routine(pixelState, context->pipelineLayout, context->pixelShader, context->descriptorSets);
227 DrawCall::SetupFunction setupPrimitives = nullptr;
228 unsigned int numPrimitivesPerBatch = MaxBatchSize / ms;
230 if(context->isDrawTriangle(false))
232 switch(context->polygonMode)
234 case VK_POLYGON_MODE_FILL:
235 setupPrimitives = &DrawCall::setupSolidTriangles;
237 case VK_POLYGON_MODE_LINE:
238 setupPrimitives = &DrawCall::setupWireframeTriangles;
239 numPrimitivesPerBatch /= 3;
241 case VK_POLYGON_MODE_POINT:
242 setupPrimitives = &DrawCall::setupPointTriangles;
243 numPrimitivesPerBatch /= 3;
246 UNSUPPORTED("polygon mode: %d", int(context->polygonMode));
250 else if(context->isDrawLine(false))
252 setupPrimitives = &DrawCall::setupLines;
254 else // Point primitive topology
256 setupPrimitives = &DrawCall::setupPoints;
259 DrawData *data = draw->data;
260 draw->occlusionQuery = occlusionQuery;
261 draw->batchDataPool = &batchDataPool;
262 draw->numPrimitives = count;
263 draw->numPrimitivesPerBatch = numPrimitivesPerBatch;
264 draw->numBatches = (count + draw->numPrimitivesPerBatch - 1) / draw->numPrimitivesPerBatch;
265 draw->topology = context->topology;
266 draw->provokingVertexMode = context->provokingVertexMode;
267 draw->indexType = indexType;
268 draw->lineRasterizationMode = context->lineRasterizationMode;
270 draw->vertexRoutine = vertexRoutine;
271 draw->setupRoutine = setupRoutine;
272 draw->pixelRoutine = pixelRoutine;
273 draw->setupPrimitives = setupPrimitives;
274 draw->setupState = setupState;
276 data->descriptorSets = context->descriptorSets;
277 data->descriptorDynamicOffsets = context->descriptorDynamicOffsets;
279 for(int i = 0; i < MAX_INTERFACE_COMPONENTS / 4; i++)
281 data->input[i] = context->input[i].buffer;
282 data->robustnessSize[i] = context->input[i].robustnessSize;
283 data->stride[i] = context->input[i].vertexStride;
286 data->indices = indexBuffer;
287 data->viewID = viewID;
288 data->instanceID = instanceID;
289 data->baseVertex = baseVertex;
291 if(pixelState.stencilActive)
293 data->stencil[0].set(context->frontStencil.reference, context->frontStencil.compareMask, context->frontStencil.writeMask);
294 data->stencil[1].set(context->backStencil.reference, context->backStencil.compareMask, context->backStencil.writeMask);
297 data->lineWidth = context->lineWidth;
299 data->factor = factor;
301 if(pixelState.alphaToCoverage)
305 data->a2c0 = float4(0.2f);
306 data->a2c1 = float4(0.4f);
307 data->a2c2 = float4(0.6f);
308 data->a2c3 = float4(0.8f);
312 data->a2c0 = float4(0.25f);
313 data->a2c1 = float4(0.75f);
319 if(pixelState.occlusionEnabled)
321 for(int cluster = 0; cluster < MaxClusterCount; cluster++)
323 data->occlusion[cluster] = 0;
329 float W = 0.5f * viewport.width;
330 float H = 0.5f * viewport.height;
331 float X0 = viewport.x + W;
332 float Y0 = viewport.y + H;
333 float N = viewport.minDepth;
334 float F = viewport.maxDepth;
336 constexpr float subPixF = vk::SUBPIXEL_PRECISION_FACTOR;
338 if(context->isDrawTriangle(false))
340 N += context->depthBias;
343 data->WxF = float4(W * subPixF);
344 data->HxF = float4(H * subPixF);
345 data->X0xF = float4(X0 * subPixF - subPixF / 2);
346 data->Y0xF = float4(Y0 * subPixF - subPixF / 2);
347 data->halfPixelX = float4(0.5f / W);
348 data->halfPixelY = float4(0.5f / H);
349 data->viewportHeight = abs(viewport.height);
350 data->slopeDepthBias = context->slopeDepthBias;
351 data->depthRange = Z;
357 for(int index = 0; index < RENDERTARGETS; index++)
359 draw->renderTarget[index] = context->renderTarget[index];
361 if(draw->renderTarget[index])
363 data->colorBuffer[index] = (unsigned int *)context->renderTarget[index]->getOffsetPointer({ 0, 0, 0 }, VK_IMAGE_ASPECT_COLOR_BIT, 0, data->viewID);
364 data->colorPitchB[index] = context->renderTarget[index]->rowPitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
365 data->colorSliceB[index] = context->renderTarget[index]->slicePitchBytes(VK_IMAGE_ASPECT_COLOR_BIT, 0);
369 draw->depthBuffer = context->depthBuffer;
370 draw->stencilBuffer = context->stencilBuffer;
372 if(draw->depthBuffer)
374 data->depthBuffer = (float *)context->depthBuffer->getOffsetPointer({ 0, 0, 0 }, VK_IMAGE_ASPECT_DEPTH_BIT, 0, data->viewID);
375 data->depthPitchB = context->depthBuffer->rowPitchBytes(VK_IMAGE_ASPECT_DEPTH_BIT, 0);
376 data->depthSliceB = context->depthBuffer->slicePitchBytes(VK_IMAGE_ASPECT_DEPTH_BIT, 0);
379 if(draw->stencilBuffer)
381 data->stencilBuffer = (unsigned char *)context->stencilBuffer->getOffsetPointer({ 0, 0, 0 }, VK_IMAGE_ASPECT_STENCIL_BIT, 0, data->viewID);
382 data->stencilPitchB = context->stencilBuffer->rowPitchBytes(VK_IMAGE_ASPECT_STENCIL_BIT, 0);
383 data->stencilSliceB = context->stencilBuffer->slicePitchBytes(VK_IMAGE_ASPECT_STENCIL_BIT, 0);
389 data->scissorX0 = clamp<int>(scissor.offset.x, 0, framebufferExtent.width);
390 data->scissorX1 = clamp<int>(scissor.offset.x + scissor.extent.width, 0, framebufferExtent.width);
391 data->scissorY0 = clamp<int>(scissor.offset.y, 0, framebufferExtent.height);
392 data->scissorY1 = clamp<int>(scissor.offset.y + scissor.extent.height, 0, framebufferExtent.height);
397 data->pushConstants = pushConstants;
400 draw->events = events;
402 DrawCall::run(draw, &drawTickets, clusterQueues);
405 void DrawCall::setup()
407 if(occlusionQuery != nullptr)
409 occlusionQuery->start();
418 void DrawCall::teardown()
426 if(occlusionQuery != nullptr)
428 for(int cluster = 0; cluster < MaxClusterCount; cluster++)
430 occlusionQuery->add(data->occlusion[cluster]);
432 occlusionQuery->finish();
440 void DrawCall::run(const marl::Loan<DrawCall> &draw, marl::Ticket::Queue *tickets, marl::Ticket::Queue clusterQueues[MaxClusterCount])
444 auto const numPrimitives = draw->numPrimitives;
445 auto const numPrimitivesPerBatch = draw->numPrimitivesPerBatch;
446 auto const numBatches = draw->numBatches;
448 auto ticket = tickets->take();
449 auto finally = marl::make_shared_finally([draw, ticket] {
450 MARL_SCOPED_EVENT("FINISH draw %d", draw->id);
455 for(unsigned int batchId = 0; batchId < numBatches; batchId++)
457 auto batch = draw->batchDataPool->borrow();
459 batch->firstPrimitive = batch->id * numPrimitivesPerBatch;
460 batch->numPrimitives = std::min(batch->firstPrimitive + numPrimitivesPerBatch, numPrimitives) - batch->firstPrimitive;
462 for(int cluster = 0; cluster < MaxClusterCount; cluster++)
464 batch->clusterTickets[cluster] = std::move(clusterQueues[cluster].take());
467 marl::schedule([draw, batch, finally] {
468 processVertices(draw.get(), batch.get());
470 if(!draw->setupState.rasterizerDiscard)
472 processPrimitives(draw.get(), batch.get());
474 if(batch->numVisible > 0)
476 processPixels(draw, batch, finally);
481 for(int cluster = 0; cluster < MaxClusterCount; cluster++)
483 batch->clusterTickets[cluster].done();
489 void DrawCall::processVertices(DrawCall *draw, BatchData *batch)
491 MARL_SCOPED_EVENT("VERTEX draw %d, batch %d", draw->id, batch->id);
493 unsigned int triangleIndices[MaxBatchSize + 1][3]; // One extra for SIMD width overrun. TODO: Adjust to dynamic batch size.
495 MARL_SCOPED_EVENT("processPrimitiveVertices");
496 processPrimitiveVertices(
500 batch->firstPrimitive,
501 batch->numPrimitives,
503 draw->provokingVertexMode);
506 auto &vertexTask = batch->vertexTask;
507 vertexTask.primitiveStart = batch->firstPrimitive;
508 // We're only using batch compaction for points, not lines
509 vertexTask.vertexCount = batch->numPrimitives * ((draw->topology == VK_PRIMITIVE_TOPOLOGY_POINT_LIST) ? 1 : 3);
510 if(vertexTask.vertexCache.drawCall != draw->id)
512 vertexTask.vertexCache.clear();
513 vertexTask.vertexCache.drawCall = draw->id;
516 draw->vertexRoutine(&batch->triangles.front().v0, &triangleIndices[0][0], &vertexTask, draw->data);
519 void DrawCall::processPrimitives(DrawCall *draw, BatchData *batch)
521 MARL_SCOPED_EVENT("PRIMITIVES draw %d batch %d", draw->id, batch->id);
522 auto triangles = &batch->triangles[0];
523 auto primitives = &batch->primitives[0];
524 batch->numVisible = draw->setupPrimitives(triangles, primitives, draw, batch->numPrimitives);
527 void DrawCall::processPixels(const marl::Loan<DrawCall> &draw, const marl::Loan<BatchData> &batch, const std::shared_ptr<marl::Finally> &finally)
531 Data(const marl::Loan<DrawCall> &draw, const marl::Loan<BatchData> &batch, const std::shared_ptr<marl::Finally> &finally)
536 marl::Loan<DrawCall> draw;
537 marl::Loan<BatchData> batch;
538 std::shared_ptr<marl::Finally> finally;
540 auto data = std::make_shared<Data>(draw, batch, finally);
541 for(int cluster = 0; cluster < MaxClusterCount; cluster++)
543 batch->clusterTickets[cluster].onCall([data, cluster] {
544 auto &draw = data->draw;
545 auto &batch = data->batch;
546 MARL_SCOPED_EVENT("PIXEL draw %d, batch %d, cluster %d", draw->id, batch->id, cluster);
547 draw->pixelRoutine(&batch->primitives.front(), batch->numVisible, cluster, MaxClusterCount, draw->data);
548 batch->clusterTickets[cluster].done();
553 void Renderer::synchronize()
555 MARL_SCOPED_EVENT("synchronize");
556 auto ticket = drawTickets.take();
558 device->updateSamplingRoutineConstCache();
562 void DrawCall::processPrimitiveVertices(
563 unsigned int triangleIndicesOut[MaxBatchSize + 1][3],
564 const void *primitiveIndices,
565 VkIndexType indexType,
567 unsigned int triangleCount,
568 VkPrimitiveTopology topology,
569 VkProvokingVertexModeEXT provokingVertexMode)
571 if(!primitiveIndices)
575 unsigned int operator[](unsigned int i) { return i; }
578 if(!setBatchIndices(triangleIndicesOut, topology, provokingVertexMode, LinearIndex(), start, triangleCount))
587 case VK_INDEX_TYPE_UINT16:
588 if(!setBatchIndices(triangleIndicesOut, topology, provokingVertexMode, static_cast<const uint16_t *>(primitiveIndices), start, triangleCount))
593 case VK_INDEX_TYPE_UINT32:
594 if(!setBatchIndices(triangleIndicesOut, topology, provokingVertexMode, static_cast<const uint32_t *>(primitiveIndices), start, triangleCount))
606 // setBatchIndices() takes care of the point case, since it's different due to the compaction
607 if(topology != VK_PRIMITIVE_TOPOLOGY_POINT_LIST)
609 // Repeat the last index to allow for SIMD width overrun.
610 triangleIndicesOut[triangleCount][0] = triangleIndicesOut[triangleCount - 1][2];
611 triangleIndicesOut[triangleCount][1] = triangleIndicesOut[triangleCount - 1][2];
612 triangleIndicesOut[triangleCount][2] = triangleIndicesOut[triangleCount - 1][2];
616 int DrawCall::setupSolidTriangles(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count)
618 auto &state = drawCall->setupState;
620 int ms = state.multiSampleCount;
621 const DrawData *data = drawCall->data;
624 for(int i = 0; i < count; i++, triangles++)
626 Vertex &v0 = triangles->v0;
627 Vertex &v1 = triangles->v1;
628 Vertex &v2 = triangles->v2;
630 Polygon polygon(&v0.position, &v1.position, &v2.position);
632 if((v0.cullMask | v1.cullMask | v2.cullMask) == 0)
637 if((v0.clipFlags & v1.clipFlags & v2.clipFlags) != Clipper::CLIP_FINITE)
642 int clipFlagsOr = v0.clipFlags | v1.clipFlags | v2.clipFlags;
643 if(clipFlagsOr != Clipper::CLIP_FINITE)
645 if(!Clipper::Clip(polygon, clipFlagsOr, *drawCall))
651 if(drawCall->setupRoutine(primitives, triangles, &polygon, data))
661 int DrawCall::setupWireframeTriangles(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count)
663 auto &state = drawCall->setupState;
665 int ms = state.multiSampleCount;
668 for(int i = 0; i < count; i++)
670 const Vertex &v0 = triangles[i].v0;
671 const Vertex &v1 = triangles[i].v1;
672 const Vertex &v2 = triangles[i].v2;
674 float d = (v0.y * v1.x - v0.x * v1.y) * v2.w +
675 (v0.x * v2.y - v0.y * v2.x) * v1.w +
676 (v2.x * v1.y - v1.x * v2.y) * v0.w;
678 bool frontFacing = (state.frontFace == VK_FRONT_FACE_COUNTER_CLOCKWISE) ? (d > 0) : (d < 0);
679 if(state.cullMode & VK_CULL_MODE_FRONT_BIT)
681 if(frontFacing) continue;
683 if(state.cullMode & VK_CULL_MODE_BACK_BIT)
685 if(!frontFacing) continue;
696 for(int i = 0; i < 3; i++)
698 if(setupLine(*primitives, lines[i], *drawCall))
709 int DrawCall::setupPointTriangles(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count)
711 auto &state = drawCall->setupState;
713 int ms = state.multiSampleCount;
716 for(int i = 0; i < count; i++)
718 const Vertex &v0 = triangles[i].v0;
719 const Vertex &v1 = triangles[i].v1;
720 const Vertex &v2 = triangles[i].v2;
722 float d = (v0.y * v1.x - v0.x * v1.y) * v2.w +
723 (v0.x * v2.y - v0.y * v2.x) * v1.w +
724 (v2.x * v1.y - v1.x * v2.y) * v0.w;
726 bool frontFacing = (state.frontFace == VK_FRONT_FACE_COUNTER_CLOCKWISE) ? (d > 0) : (d < 0);
727 if(state.cullMode & VK_CULL_MODE_FRONT_BIT)
729 if(frontFacing) continue;
731 if(state.cullMode & VK_CULL_MODE_BACK_BIT)
733 if(!frontFacing) continue;
741 for(int i = 0; i < 3; i++)
743 if(setupPoint(*primitives, points[i], *drawCall))
754 int DrawCall::setupLines(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count)
756 auto &state = drawCall->setupState;
759 int ms = state.multiSampleCount;
761 for(int i = 0; i < count; i++)
763 if(setupLine(*primitives, *triangles, *drawCall))
775 int DrawCall::setupPoints(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count)
777 auto &state = drawCall->setupState;
780 int ms = state.multiSampleCount;
782 for(int i = 0; i < count; i++)
784 if(setupPoint(*primitives, *triangles, *drawCall))
796 bool DrawCall::setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw)
798 const DrawData &data = *draw.data;
800 float lineWidth = data.lineWidth;
802 Vertex &v0 = triangle.v0;
803 Vertex &v1 = triangle.v1;
805 if((v0.cullMask | v1.cullMask) == 0)
810 const float4 &P0 = v0.position;
811 const float4 &P1 = v1.position;
813 if(P0.w <= 0 && P1.w <= 0)
818 constexpr float subPixF = vk::SUBPIXEL_PRECISION_FACTOR;
820 const float W = data.WxF[0] * (1.0f / subPixF);
821 const float H = data.HxF[0] * (1.0f / subPixF);
823 float dx = W * (P1.x / P1.w - P0.x / P0.w);
824 float dy = H * (P1.y / P1.w - P0.y / P0.w);
826 if(dx == 0 && dy == 0)
831 if(draw.lineRasterizationMode != VK_LINE_RASTERIZATION_MODE_BRESENHAM_EXT)
833 // Rectangle centered on the line segment
843 float scale = lineWidth * 0.5f / sqrt(dx * dx + dy * dy);
848 float dx0h = dx * P0.w / H;
849 float dy0w = dy * P0.w / W;
851 float dx1h = dx * P1.w / H;
852 float dy1w = dy * P1.w / W;
856 C[0] = Clipper::ComputeClipFlags(P[0]);
860 C[1] = Clipper::ComputeClipFlags(P[1]);
864 C[2] = Clipper::ComputeClipFlags(P[2]);
868 C[3] = Clipper::ComputeClipFlags(P[3]);
870 if((C[0] & C[1] & C[2] & C[3]) == Clipper::CLIP_FINITE)
872 Polygon polygon(P, 4);
874 int clipFlagsOr = C[0] | C[1] | C[2] | C[3];
876 if(clipFlagsOr != Clipper::CLIP_FINITE)
878 if(!Clipper::Clip(polygon, clipFlagsOr, draw))
884 return draw.setupRoutine(&primitive, &triangle, &polygon, &data);
887 else if(false) // TODO(b/80135519): Deprecate
889 // Connecting diamonds polygon
890 // This shape satisfies the diamond test convention, except for the exit rule part.
891 // Line segments with overlapping endpoints have duplicate fragments.
892 // The ideal algorithm requires half-open line rasterization (b/80135519).
906 float dx0 = lineWidth * 0.5f * P0.w / W;
907 float dy0 = lineWidth * 0.5f * P0.w / H;
909 float dx1 = lineWidth * 0.5f * P1.w / W;
910 float dy1 = lineWidth * 0.5f * P1.w / H;
913 C[0] = Clipper::ComputeClipFlags(P[0]);
916 C[1] = Clipper::ComputeClipFlags(P[1]);
919 C[2] = Clipper::ComputeClipFlags(P[2]);
922 C[3] = Clipper::ComputeClipFlags(P[3]);
925 C[4] = Clipper::ComputeClipFlags(P[4]);
928 C[5] = Clipper::ComputeClipFlags(P[5]);
931 C[6] = Clipper::ComputeClipFlags(P[6]);
934 C[7] = Clipper::ComputeClipFlags(P[7]);
936 if((C[0] & C[1] & C[2] & C[3] & C[4] & C[5] & C[6] & C[7]) == Clipper::CLIP_FINITE)
983 Polygon polygon(L, 6);
985 int clipFlagsOr = C[0] | C[1] | C[2] | C[3] | C[4] | C[5] | C[6] | C[7];
987 if(clipFlagsOr != Clipper::CLIP_FINITE)
989 if(!Clipper::Clip(polygon, clipFlagsOr, draw))
995 return draw.setupRoutine(&primitive, &triangle, &polygon, &data);
1000 // Parallelogram approximating Bresenham line
1001 // This algorithm does not satisfy the ideal diamond-exit rule, but does avoid the
1002 // duplicate fragment rasterization problem and satisfies all of Vulkan's minimum
1003 // requirements for Bresenham line segment rasterization.
1015 float dx0 = lineWidth * 0.5f * P0.w / W;
1016 float dy0 = lineWidth * 0.5f * P0.w / H;
1018 float dx1 = lineWidth * 0.5f * P1.w / W;
1019 float dy1 = lineWidth * 0.5f * P1.w / H;
1034 if(dx > dy) // Right
1067 int C0 = Clipper::ComputeClipFlags(L[0]);
1068 int C1 = Clipper::ComputeClipFlags(L[1]);
1069 int C2 = Clipper::ComputeClipFlags(L[2]);
1070 int C3 = Clipper::ComputeClipFlags(L[3]);
1072 if((C0 & C1 & C2 & C3) == Clipper::CLIP_FINITE)
1074 Polygon polygon(L, 4);
1076 int clipFlagsOr = C0 | C1 | C2 | C3;
1078 if(clipFlagsOr != Clipper::CLIP_FINITE)
1080 if(!Clipper::Clip(polygon, clipFlagsOr, draw))
1086 return draw.setupRoutine(&primitive, &triangle, &polygon, &data);
1093 bool DrawCall::setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw)
1095 const DrawData &data = *draw.data;
1097 Vertex &v = triangle.v0;
1104 float pSize = v.pointSize;
1106 pSize = clamp(pSize, 1.0f, static_cast<float>(vk::MAX_POINT_SIZE));
1116 const float X = pSize * P[0].w * data.halfPixelX[0];
1117 const float Y = pSize * P[0].w * data.halfPixelY[0];
1121 C[0] = Clipper::ComputeClipFlags(P[0]);
1125 C[1] = Clipper::ComputeClipFlags(P[1]);
1129 C[2] = Clipper::ComputeClipFlags(P[2]);
1133 C[3] = Clipper::ComputeClipFlags(P[3]);
1135 Polygon polygon(P, 4);
1137 if((C[0] & C[1] & C[2] & C[3]) == Clipper::CLIP_FINITE)
1139 int clipFlagsOr = C[0] | C[1] | C[2] | C[3];
1141 if(clipFlagsOr != Clipper::CLIP_FINITE)
1143 if(!Clipper::Clip(polygon, clipFlagsOr, draw))
1149 primitive.pointSizeInv = 1.0f / pSize;
1151 return draw.setupRoutine(&primitive, &triangle, &polygon, &data);
1157 void Renderer::addQuery(vk::Query *query)
1159 ASSERT(query->getType() == VK_QUERY_TYPE_OCCLUSION);
1160 ASSERT(!occlusionQuery);
1162 occlusionQuery = query;
1165 void Renderer::removeQuery(vk::Query *query)
1167 ASSERT(query->getType() == VK_QUERY_TYPE_OCCLUSION);
1168 ASSERT(occlusionQuery == query);
1170 occlusionQuery = nullptr;
1173 // TODO(b/137740918): Optimize instancing to use a single draw call.
1174 void Renderer::advanceInstanceAttributes(Stream *inputs)
1176 for(uint32_t i = 0; i < vk::MAX_VERTEX_INPUT_BINDINGS; i++)
1178 auto &attrib = inputs[i];
1179 if((attrib.format != VK_FORMAT_UNDEFINED) && attrib.instanceStride && (attrib.instanceStride < attrib.robustnessSize))
1181 // Under the casts: attrib.buffer += attrib.instanceStride
1182 attrib.buffer = (void const *)((uintptr_t)attrib.buffer + attrib.instanceStride);
1183 attrib.robustnessSize -= attrib.instanceStride;
1188 void Renderer::setViewport(const VkViewport &viewport)
1190 this->viewport = viewport;
1193 void Renderer::setScissor(const VkRect2D &scissor)
1195 this->scissor = scissor;