swr: [rasterizer] remove use of BYTE type
[mesa.git] / src / gallium / drivers / swr / rasterizer / core / context.h
1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * @file context.h
24 *
25 * @brief Definitions for SWR_CONTEXT and DRAW_CONTEXT
26 * The SWR_CONTEXT is our global context and contains the DC ring,
27 * thread state, etc.
28 *
29 * The DRAW_CONTEXT contains all state associated with a draw operation.
30 *
31 ******************************************************************************/
32 #pragma once
33
34 #include <condition_variable>
35 #include <algorithm>
36
37 #include "core/api.h"
38 #include "core/utils.h"
39 #include "core/arena.h"
40 #include "core/fifo.hpp"
41 #include "core/knobs.h"
42 #include "common/simdintrin.h"
43 #include "core/threads.h"
44 #include "ringbuffer.h"
45
46 // x.8 fixed point precision values
47 #define FIXED_POINT_SHIFT 8
48 #define FIXED_POINT_SCALE 256
49
50 // x.16 fixed point precision values
51 #define FIXED_POINT16_SHIFT 16
52 #define FIXED_POINT16_SCALE 65536
53
54 struct SWR_CONTEXT;
55 struct DRAW_CONTEXT;
56
57 struct TRI_FLAGS
58 {
59 uint32_t frontFacing : 1;
60 uint32_t yMajor : 1;
61 uint32_t coverageMask : (SIMD_TILE_X_DIM * SIMD_TILE_Y_DIM);
62 uint32_t reserved : 32 - 1 - 1 - (SIMD_TILE_X_DIM * SIMD_TILE_Y_DIM);
63 float pointSize;
64 uint32_t primID;
65 uint32_t renderTargetArrayIndex;
66 };
67
68 //////////////////////////////////////////////////////////////////////////
69 /// SWR_TRIANGLE_DESC
70 /////////////////////////////////////////////////////////////////////////
71 struct SWR_TRIANGLE_DESC
72 {
73 float I[3];
74 float J[3];
75 float Z[3];
76 float OneOverW[3];
77 float recipDet;
78
79 float *pRecipW;
80 float *pAttribs;
81 float *pPerspAttribs;
82 float *pSamplePos;
83 float *pUserClipBuffer;
84
85 uint64_t coverageMask[SWR_MAX_NUM_MULTISAMPLES];
86 uint64_t anyCoveredSamples;
87
88 TRI_FLAGS triFlags;
89 };
90
91 struct TRIANGLE_WORK_DESC
92 {
93 float *pTriBuffer;
94 float *pAttribs;
95 float *pUserClipBuffer;
96 uint32_t numAttribs;
97 TRI_FLAGS triFlags;
98 };
99
100 union CLEAR_FLAGS
101 {
102 struct
103 {
104 uint32_t mask : 3;
105 };
106 uint32_t bits;
107 };
108
109 struct CLEAR_DESC
110 {
111 CLEAR_FLAGS flags;
112 float clearRTColor[4]; // RGBA_32F
113 float clearDepth; // [0..1]
114 uint8_t clearStencil;
115 };
116
117 struct INVALIDATE_TILES_DESC
118 {
119 uint32_t attachmentMask;
120 };
121
122 struct SYNC_DESC
123 {
124 PFN_CALLBACK_FUNC pfnCallbackFunc;
125 uint64_t userData;
126 uint64_t userData2;
127 uint64_t userData3;
128 };
129
130 struct QUERY_DESC
131 {
132 SWR_STATS* pStats;
133 };
134
135 struct STORE_TILES_DESC
136 {
137 SWR_RENDERTARGET_ATTACHMENT attachment;
138 SWR_TILE_STATE postStoreTileState;
139 };
140
141 struct COMPUTE_DESC
142 {
143 uint32_t threadGroupCountX;
144 uint32_t threadGroupCountY;
145 uint32_t threadGroupCountZ;
146 };
147
148 typedef void(*PFN_WORK_FUNC)(DRAW_CONTEXT* pDC, uint32_t workerId, uint32_t macroTile, void* pDesc);
149
150 enum WORK_TYPE
151 {
152 SYNC,
153 DRAW,
154 CLEAR,
155 INVALIDATETILES,
156 STORETILES,
157 QUERYSTATS,
158 };
159
160 struct BE_WORK
161 {
162 WORK_TYPE type;
163 PFN_WORK_FUNC pfnWork;
164 union
165 {
166 SYNC_DESC sync;
167 TRIANGLE_WORK_DESC tri;
168 CLEAR_DESC clear;
169 INVALIDATE_TILES_DESC invalidateTiles;
170 STORE_TILES_DESC storeTiles;
171 QUERY_DESC queryStats;
172 } desc;
173 };
174
175 struct DRAW_WORK
176 {
177 DRAW_CONTEXT* pDC;
178 union
179 {
180 uint32_t numIndices; // DrawIndexed: Number of indices for draw.
181 uint32_t numVerts; // Draw: Number of verts (triangles, lines, etc)
182 };
183 union
184 {
185 const int32_t* pIB; // DrawIndexed: App supplied indices
186 uint32_t startVertex; // Draw: Starting vertex in VB to render from.
187 };
188 int32_t baseVertex;
189 uint32_t numInstances; // Number of instances
190 uint32_t startInstance; // Instance offset
191 uint32_t startPrimID; // starting primitiveID for this draw batch
192 uint32_t startVertexID; // starting VertexID for this draw batch (only needed for non-indexed draws)
193 SWR_FORMAT type; // index buffer type
194 };
195
196 typedef void(*PFN_FE_WORK_FUNC)(SWR_CONTEXT* pContext, DRAW_CONTEXT* pDC, uint32_t workerId, void* pDesc);
197 struct FE_WORK
198 {
199 WORK_TYPE type;
200 PFN_FE_WORK_FUNC pfnWork;
201 union
202 {
203 SYNC_DESC sync;
204 DRAW_WORK draw;
205 CLEAR_DESC clear;
206 INVALIDATE_TILES_DESC invalidateTiles;
207 STORE_TILES_DESC storeTiles;
208 QUERY_DESC queryStats;
209 } desc;
210 };
211
212 struct GUARDBAND
213 {
214 float left, right, top, bottom;
215 };
216
217 struct PA_STATE;
218
219 // function signature for pipeline stages that execute after primitive assembly
220 typedef void(*PFN_PROCESS_PRIMS)(DRAW_CONTEXT *pDC, PA_STATE& pa, uint32_t workerId, simdvector prims[],
221 uint32_t primMask, simdscalari primID);
222
223 OSALIGNLINE(struct) API_STATE
224 {
225 // Vertex Buffers
226 SWR_VERTEX_BUFFER_STATE vertexBuffers[KNOB_NUM_STREAMS];
227
228 // Index Buffer
229 SWR_INDEX_BUFFER_STATE indexBuffer;
230
231 // FS - Fetch Shader State
232 PFN_FETCH_FUNC pfnFetchFunc;
233
234 // VS - Vertex Shader State
235 PFN_VERTEX_FUNC pfnVertexFunc;
236
237 // GS - Geometry Shader State
238 PFN_GS_FUNC pfnGsFunc;
239 SWR_GS_STATE gsState;
240
241 // CS - Compute Shader
242 PFN_CS_FUNC pfnCsFunc;
243 uint32_t totalThreadsInGroup;
244
245 // FE - Frontend State
246 SWR_FRONTEND_STATE frontendState;
247
248 // SOS - Streamout Shader State
249 PFN_SO_FUNC pfnSoFunc[MAX_SO_STREAMS];
250
251 // Streamout state
252 SWR_STREAMOUT_STATE soState;
253 mutable SWR_STREAMOUT_BUFFER soBuffer[MAX_SO_STREAMS];
254
255 // Tessellation State
256 PFN_HS_FUNC pfnHsFunc;
257 PFN_DS_FUNC pfnDsFunc;
258 SWR_TS_STATE tsState;
259
260 // Specifies which VS outputs are sent to PS.
261 // Does not include position
262 uint32_t linkageMask;
263 uint32_t linkageCount;
264 uint8_t linkageMap[MAX_ATTRIBUTES];
265
266 // attrib mask, specifies the total set of attributes used
267 // by the frontend (vs, so, gs)
268 uint32_t feAttribMask;
269
270 PRIMITIVE_TOPOLOGY topology;
271 bool forceFront;
272
273 // RS - Rasterizer State
274 SWR_RASTSTATE rastState;
275 // floating point multisample offsets
276 float samplePos[SWR_MAX_NUM_MULTISAMPLES * 2];
277
278 GUARDBAND gbState;
279
280 SWR_VIEWPORT vp[KNOB_NUM_VIEWPORTS_SCISSORS];
281 SWR_VIEWPORT_MATRIX vpMatrix[KNOB_NUM_VIEWPORTS_SCISSORS];
282
283 BBOX scissorRects[KNOB_NUM_VIEWPORTS_SCISSORS];
284 BBOX scissorInFixedPoint;
285
286 // Backend state
287 SWR_BACKEND_STATE backendState;
288
289 // PS - Pixel shader state
290 SWR_PS_STATE psState;
291
292 SWR_DEPTH_STENCIL_STATE depthStencilState;
293
294 // OM - Output Merger State
295 SWR_BLEND_STATE blendState;
296 PFN_BLEND_JIT_FUNC pfnBlendFunc[SWR_NUM_RENDERTARGETS];
297
298 // Stats are incremented when this is true.
299 bool enableStats;
300
301 struct
302 {
303 uint32_t colorHottileEnable : 8;
304 uint32_t depthHottileEnable: 1;
305 uint32_t stencilHottileEnable : 1;
306 };
307 };
308
309 class MacroTileMgr;
310 class DispatchQueue;
311
312 struct RenderOutputBuffers
313 {
314 uint8_t* pColor[SWR_NUM_RENDERTARGETS];
315 uint8_t* pDepth;
316 uint8_t* pStencil;
317 };
318
319 // Plane equation A/B/C coeffs used to evaluate I/J barycentric coords
320 struct BarycentricCoeffs
321 {
322 simdscalar vIa;
323 simdscalar vIb;
324 simdscalar vIc;
325
326 simdscalar vJa;
327 simdscalar vJb;
328 simdscalar vJc;
329
330 simdscalar vZa;
331 simdscalar vZb;
332 simdscalar vZc;
333
334 simdscalar vRecipDet;
335
336 simdscalar vAOneOverW;
337 simdscalar vBOneOverW;
338 simdscalar vCOneOverW;
339 };
340
341 // pipeline function pointer types
342 typedef void(*PFN_BACKEND_FUNC)(DRAW_CONTEXT*, uint32_t, uint32_t, uint32_t, SWR_TRIANGLE_DESC&, RenderOutputBuffers&);
343 typedef void(*PFN_OUTPUT_MERGER)(SWR_PS_CONTEXT &, uint8_t* (&)[SWR_NUM_RENDERTARGETS], uint32_t, const SWR_BLEND_STATE*,
344 const PFN_BLEND_JIT_FUNC (&)[SWR_NUM_RENDERTARGETS], simdscalar&, simdscalar);
345 typedef void(*PFN_CALC_PIXEL_BARYCENTRICS)(const BarycentricCoeffs&, SWR_PS_CONTEXT &);
346 typedef void(*PFN_CALC_SAMPLE_BARYCENTRICS)(const BarycentricCoeffs&, SWR_PS_CONTEXT&);
347 typedef void(*PFN_CALC_CENTROID_BARYCENTRICS)(const BarycentricCoeffs&, SWR_PS_CONTEXT &, const uint64_t *const, const uint32_t,
348 const simdscalar, const simdscalar);
349
350 struct BACKEND_FUNCS
351 {
352 PFN_BACKEND_FUNC pfnBackend;
353 PFN_CALC_PIXEL_BARYCENTRICS pfnCalcPixelBarycentrics;
354 PFN_CALC_SAMPLE_BARYCENTRICS pfnCalcSampleBarycentrics;
355 PFN_CALC_CENTROID_BARYCENTRICS pfnCalcCentroidBarycentrics;
356 PFN_OUTPUT_MERGER pfnOutputMerger;
357 };
358
359 // Draw State
360 struct DRAW_STATE
361 {
362 API_STATE state;
363
364 void* pPrivateState; // Its required the driver sets this up for each draw.
365
366 // pipeline function pointers, filled in by API thread when setting up the draw
367 BACKEND_FUNCS backendFuncs;
368 PFN_PROCESS_PRIMS pfnProcessPrims;
369
370 Arena* pArena; // This should only be used by API thread.
371 };
372
373 // Draw Context
374 // The api thread sets up a draw context that exists for the life of the draw.
375 // This draw context maintains all of the state needed for the draw operation.
376 struct DRAW_CONTEXT
377 {
378 SWR_CONTEXT *pContext;
379
380 uint64_t drawId;
381
382 bool isCompute; // Is this DC a compute context?
383
384 FE_WORK FeWork;
385 volatile OSALIGNLINE(uint32_t) FeLock;
386 volatile OSALIGNLINE(bool) doneFE; // Is FE work done for this draw?
387 volatile OSALIGNLINE(int64_t) threadsDone;
388
389 uint64_t dependency;
390
391 MacroTileMgr* pTileMgr;
392
393 // The following fields are valid if isCompute is true.
394 DispatchQueue* pDispatch; // Queue for thread groups. (isCompute)
395
396 DRAW_STATE* pState;
397 Arena* pArena;
398
399 uint8_t* pSpillFill[KNOB_MAX_NUM_THREADS]; // Scratch space used for spill fills.
400 };
401
402 INLINE const API_STATE& GetApiState(const DRAW_CONTEXT* pDC)
403 {
404 SWR_ASSERT(pDC != nullptr);
405 SWR_ASSERT(pDC->pState != nullptr);
406
407 return pDC->pState->state;
408 }
409
410 INLINE void* GetPrivateState(const DRAW_CONTEXT* pDC)
411 {
412 SWR_ASSERT(pDC != nullptr);
413 SWR_ASSERT(pDC->pState != nullptr);
414
415 return pDC->pState->pPrivateState;
416 }
417
418 class HotTileMgr;
419
420 struct SWR_CONTEXT
421 {
422 // Draw Context Ring
423 // Each draw needs its own state in order to support mulitple draws in flight across multiple threads.
424 // We maintain N draw contexts configured as a ring. The size of the ring limits the maximum number
425 // of draws that can be in flight at any given time.
426 //
427 // Description:
428 // 1. State - When an application first sets state we'll request a new draw context to use.
429 // a. If there are no available draw contexts then we'll have to wait until one becomes free.
430 // b. If one is available then set pCurDrawContext to point to it and mark it in use.
431 // c. All state calls set state on pCurDrawContext.
432 // 2. Draw - Creates submits a work item that is associated with current draw context.
433 // a. Set pPrevDrawContext = pCurDrawContext
434 // b. Set pCurDrawContext to NULL.
435 // 3. State - When an applications sets state after draw
436 // a. Same as step 1.
437 // b. State is copied from prev draw context to current.
438 RingBuffer<DRAW_CONTEXT> dcRing;
439
440 DRAW_CONTEXT *pCurDrawContext; // This points to DC entry in ring for an unsubmitted draw.
441 DRAW_CONTEXT *pPrevDrawContext; // This points to DC entry for the previous context submitted that we can copy state from.
442
443 // Draw State Ring
444 // When draw are very large (lots of primitives) then the API thread will break these up.
445 // These split draws all have identical state. So instead of storing the state directly
446 // in the Draw Context (DC) we instead store it in a Draw State (DS). This allows multiple DCs
447 // to reference a single entry in the DS ring.
448 RingBuffer<DRAW_STATE> dsRing;
449
450 uint32_t curStateId; // Current index to the next available entry in the DS ring.
451
452 uint32_t NumWorkerThreads;
453
454 THREAD_POOL threadPool; // Thread pool associated with this context
455
456 std::condition_variable FifosNotEmpty;
457 std::mutex WaitLock;
458
459 DRIVER_TYPE driverType;
460
461 uint32_t privateStateSize;
462
463 HotTileMgr *pHotTileMgr;
464
465 // tile load/store functions, passed in at create context time
466 PFN_LOAD_TILE pfnLoadTile;
467 PFN_STORE_TILE pfnStoreTile;
468 PFN_CLEAR_TILE pfnClearTile;
469
470 // Global Stats
471 SWR_STATS stats[KNOB_MAX_NUM_THREADS];
472
473 // Scratch space for workers.
474 uint8_t* pScratch[KNOB_MAX_NUM_THREADS];
475 };
476
477 void WaitForDependencies(SWR_CONTEXT *pContext, uint64_t drawId);
478 void WakeAllThreads(SWR_CONTEXT *pContext);
479
480 #define UPDATE_STAT(name, count) if (GetApiState(pDC).enableStats) { pContext->stats[workerId].name += count; }
481 #define SET_STAT(name, count) if (GetApiState(pDC).enableStats) { pContext->stats[workerId].name = count; }