Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 1 | // Copyright 2016 The SwiftShader Authors. All Rights Reserved. |
| 2 | // |
| 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 |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 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. |
| 14 | |
| 15 | #ifndef sw_Renderer_hpp |
| 16 | #define sw_Renderer_hpp |
| 17 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 18 | #include "Blitter.hpp" |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 19 | #include "PixelProcessor.hpp" |
Ben Clayton | cde4dd9 | 2019-08-27 13:15:50 +0100 | [diff] [blame] | 20 | #include "Primitive.hpp" |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 21 | #include "SetupProcessor.hpp" |
| 22 | #include "VertexProcessor.hpp" |
Nicolas Capens | 1d8c8db | 2018-11-05 16:30:42 -0500 | [diff] [blame] | 23 | #include "Device/Config.hpp" |
Ben Clayton | 225a130 | 2019-04-02 12:28:22 +0100 | [diff] [blame] | 24 | #include "Vulkan/VkDescriptorSet.hpp" |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 25 | |
Ben Clayton | e693b62 | 2019-09-05 12:48:37 +0100 | [diff] [blame] | 26 | #include "marl/finally.h" |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 27 | #include "marl/pool.h" |
Ben Clayton | e693b62 | 2019-09-05 12:48:37 +0100 | [diff] [blame] | 28 | #include "marl/ticket.h" |
Ben Clayton | cde4dd9 | 2019-08-27 13:15:50 +0100 | [diff] [blame] | 29 | |
Alexis Hetu | 463fab9 | 2019-06-12 13:24:48 -0400 | [diff] [blame] | 30 | #include <atomic> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 31 | #include <list> |
Ben Clayton | e29e7ba | 2019-05-21 13:19:32 +0100 | [diff] [blame] | 32 | #include <mutex> |
| 33 | #include <thread> |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 34 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 35 | namespace vk { |
| 36 | |
| 37 | class DescriptorSet; |
| 38 | class Device; |
| 39 | class Query; |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 40 | class PipelineLayout; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 41 | |
| 42 | } // namespace vk |
| 43 | |
| 44 | namespace sw { |
| 45 | |
| 46 | struct DrawCall; |
| 47 | class PixelShader; |
| 48 | class VertexShader; |
| 49 | struct Task; |
| 50 | class TaskEvents; |
| 51 | class Resource; |
| 52 | struct Constants; |
| 53 | |
| 54 | static constexpr int MaxBatchSize = 128; |
| 55 | static constexpr int MaxBatchCount = 16; |
| 56 | static constexpr int MaxClusterCount = 16; |
| 57 | static constexpr int MaxDrawCount = 16; |
| 58 | |
| 59 | using TriangleBatch = std::array<Triangle, MaxBatchSize>; |
| 60 | using PrimitiveBatch = std::array<Primitive, MaxBatchSize>; |
| 61 | |
| 62 | struct DrawData |
Chris Forbes | c296806 | 2019-03-19 16:48:03 -0700 | [diff] [blame] | 63 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 64 | const Constants *constants; |
Chris Forbes | c296806 | 2019-03-19 16:48:03 -0700 | [diff] [blame] | 65 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 66 | vk::DescriptorSet::Bindings descriptorSets = {}; |
| 67 | vk::DescriptorSet::DynamicOffsets descriptorDynamicOffsets = {}; |
| 68 | |
| 69 | const void *input[MAX_INTERFACE_COMPONENTS / 4]; |
| 70 | unsigned int robustnessSize[MAX_INTERFACE_COMPONENTS / 4]; |
| 71 | unsigned int stride[MAX_INTERFACE_COMPONENTS / 4]; |
| 72 | const void *indices; |
| 73 | |
| 74 | int instanceID; |
| 75 | int baseVertex; |
| 76 | float lineWidth; |
| 77 | int viewID; |
| 78 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 79 | PixelProcessor::Stencil stencil[2]; // clockwise, counterclockwise |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 80 | PixelProcessor::Factor factor; |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 81 | unsigned int occlusion[MaxClusterCount]; // Number of pixels passing depth test |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 82 | |
| 83 | float4 WxF; |
| 84 | float4 HxF; |
| 85 | float4 X0xF; |
| 86 | float4 Y0xF; |
| 87 | float4 halfPixelX; |
| 88 | float4 halfPixelY; |
| 89 | float viewportHeight; |
| 90 | float slopeDepthBias; |
| 91 | float depthRange; |
| 92 | float depthNear; |
| 93 | |
| 94 | unsigned int *colorBuffer[RENDERTARGETS]; |
| 95 | int colorPitchB[RENDERTARGETS]; |
| 96 | int colorSliceB[RENDERTARGETS]; |
| 97 | float *depthBuffer; |
| 98 | int depthPitchB; |
| 99 | int depthSliceB; |
| 100 | unsigned char *stencilBuffer; |
| 101 | int stencilPitchB; |
| 102 | int stencilSliceB; |
| 103 | |
| 104 | int scissorX0; |
| 105 | int scissorX1; |
| 106 | int scissorY0; |
| 107 | int scissorY1; |
| 108 | |
| 109 | float4 a2c0; |
| 110 | float4 a2c1; |
| 111 | float4 a2c2; |
| 112 | float4 a2c3; |
| 113 | |
| 114 | PushConstantStorage pushConstants; |
| 115 | }; |
| 116 | |
| 117 | struct DrawCall |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 118 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 119 | struct BatchData |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 120 | { |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 121 | using Pool = marl::BoundedPool<BatchData, MaxBatchCount, marl::PoolPolicy::Preserve>; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 122 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 123 | TriangleBatch triangles; |
| 124 | PrimitiveBatch primitives; |
| 125 | VertexTask vertexTask; |
| 126 | unsigned int id; |
| 127 | unsigned int firstPrimitive; |
Ben Clayton | cde4dd9 | 2019-08-27 13:15:50 +0100 | [diff] [blame] | 128 | unsigned int numPrimitives; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 129 | int numVisible; |
| 130 | marl::Ticket clusterTickets[MaxClusterCount]; |
Ben Clayton | cde4dd9 | 2019-08-27 13:15:50 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 133 | using Pool = marl::BoundedPool<DrawCall, MaxDrawCount, marl::PoolPolicy::Preserve>; |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 134 | using SetupFunction = int (*)(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 135 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 136 | DrawCall(); |
| 137 | ~DrawCall(); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 138 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 139 | static void run(const marl::Loan<DrawCall> &draw, marl::Ticket::Queue *tickets, marl::Ticket::Queue clusterQueues[MaxClusterCount]); |
| 140 | static void processVertices(DrawCall *draw, BatchData *batch); |
| 141 | static void processPrimitives(DrawCall *draw, BatchData *batch); |
| 142 | static void processPixels(const marl::Loan<DrawCall> &draw, const marl::Loan<BatchData> &batch, const std::shared_ptr<marl::Finally> &finally); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 143 | void setup(); |
| 144 | void teardown(); |
Alexis Hetu | 3084768 | 2019-09-11 17:18:46 -0400 | [diff] [blame] | 145 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 146 | int id; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 147 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 148 | BatchData::Pool *batchDataPool; |
| 149 | unsigned int numPrimitives; |
| 150 | unsigned int numPrimitivesPerBatch; |
| 151 | unsigned int numBatches; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 152 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 153 | VkPrimitiveTopology topology; |
| 154 | VkProvokingVertexModeEXT provokingVertexMode; |
| 155 | VkIndexType indexType; |
| 156 | VkLineRasterizationModeEXT lineRasterizationMode; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 157 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 158 | VertexProcessor::RoutineType vertexRoutine; |
| 159 | SetupProcessor::RoutineType setupRoutine; |
| 160 | PixelProcessor::RoutineType pixelRoutine; |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 161 | bool containsImageWrite; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 162 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 163 | SetupFunction setupPrimitives; |
| 164 | SetupProcessor::State setupState; |
Chris Forbes | e1cf863 | 2019-03-08 18:17:35 -0800 | [diff] [blame] | 165 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 166 | vk::ImageView *renderTarget[RENDERTARGETS]; |
| 167 | vk::ImageView *depthBuffer; |
| 168 | vk::ImageView *stencilBuffer; |
Alexis Hetu | 4f438a5 | 2020-06-15 16:13:51 -0400 | [diff] [blame] | 169 | vk::DescriptorSet::Array descriptorSetObjects; |
| 170 | const vk::PipelineLayout *pipelineLayout; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 171 | TaskEvents *events; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 172 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 173 | vk::Query *occlusionQuery; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 174 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 175 | DrawData *data; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 176 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 177 | static void processPrimitiveVertices( |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 178 | unsigned int triangleIndicesOut[MaxBatchSize + 1][3], |
| 179 | const void *primitiveIndices, |
| 180 | VkIndexType indexType, |
| 181 | unsigned int start, |
| 182 | unsigned int triangleCount, |
| 183 | VkPrimitiveTopology topology, |
| 184 | VkProvokingVertexModeEXT provokingVertexMode); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 185 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 186 | static int setupSolidTriangles(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count); |
| 187 | static int setupWireframeTriangles(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count); |
| 188 | static int setupPointTriangles(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 189 | static int setupLines(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count); |
| 190 | static int setupPoints(Triangle *triangles, Primitive *primitives, const DrawCall *drawCall, int count); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 191 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 192 | static bool setupLine(Primitive &primitive, Triangle &triangle, const DrawCall &draw); |
| 193 | static bool setupPoint(Primitive &primitive, Triangle &triangle, const DrawCall &draw); |
| 194 | }; |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 195 | |
Ben Clayton | bc2af94 | 2020-04-14 09:22:14 +0100 | [diff] [blame] | 196 | class alignas(16) Renderer |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 197 | { |
| 198 | public: |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 199 | Renderer(vk::Device *device); |
Alexis Hetu | 3575550 | 2019-07-22 13:51:49 -0400 | [diff] [blame] | 200 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 201 | virtual ~Renderer(); |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 202 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 203 | void *operator new(size_t size); |
| 204 | void operator delete(void *mem); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 205 | |
| 206 | bool hasOcclusionQuery() const { return occlusionQuery != nullptr; } |
| 207 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 208 | void draw(const sw::Context *context, VkIndexType indexType, unsigned int count, int baseVertex, |
| 209 | TaskEvents *events, int instanceID, int viewID, void *indexBuffer, const VkExtent3D &framebufferExtent, |
| 210 | PushConstantStorage const &pushConstants, bool update = true); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 211 | |
| 212 | // Viewport & Clipper |
| 213 | void setViewport(const VkViewport &viewport); |
| 214 | void setScissor(const VkRect2D &scissor); |
| 215 | |
Ben Clayton | bc2af94 | 2020-04-14 09:22:14 +0100 | [diff] [blame] | 216 | void setBlendConstant(const float4 &blendConstant); |
| 217 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 218 | void addQuery(vk::Query *query); |
| 219 | void removeQuery(vk::Query *query); |
| 220 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 221 | void advanceInstanceAttributes(Stream *inputs); |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 222 | |
| 223 | void synchronize(); |
| 224 | |
| 225 | private: |
| 226 | VkViewport viewport; |
| 227 | VkRect2D scissor; |
| 228 | |
| 229 | DrawCall::Pool drawCallPool; |
| 230 | DrawCall::BatchData::Pool batchDataPool; |
| 231 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 232 | std::atomic<int> nextDrawID = { 0 }; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 233 | |
| 234 | vk::Query *occlusionQuery = nullptr; |
| 235 | marl::Ticket::Queue drawTickets; |
| 236 | marl::Ticket::Queue clusterQueues[MaxClusterCount]; |
| 237 | |
Ben Clayton | bc2af94 | 2020-04-14 09:22:14 +0100 | [diff] [blame] | 238 | VertexProcessor vertexProcessor; |
| 239 | PixelProcessor pixelProcessor; |
| 240 | SetupProcessor setupProcessor; |
| 241 | |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 242 | VertexProcessor::State vertexState; |
| 243 | SetupProcessor::State setupState; |
| 244 | PixelProcessor::State pixelState; |
| 245 | |
| 246 | VertexProcessor::RoutineType vertexRoutine; |
| 247 | SetupProcessor::RoutineType setupRoutine; |
| 248 | PixelProcessor::RoutineType pixelRoutine; |
| 249 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 250 | vk::Device *device; |
Nicolas Capens | 157ba26 | 2019-12-10 17:49:14 -0500 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | } // namespace sw |
Nicolas Capens | 68a8238 | 2018-10-02 13:16:55 -0400 | [diff] [blame] | 254 | |
Ben Clayton | fccfc56 | 2019-12-17 20:37:31 +0000 | [diff] [blame] | 255 | #endif // sw_Renderer_hpp |