553e384c9bc3f2a2c317feb7735a6c0556c97e10
[mesa.git] / src / gallium / drivers / swr / rasterizer / core / tilemgr.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 tilemgr.h
24 *
25 * @brief Definitions for Macro Tile Manager which provides the facilities
26 * for threads to work on an macro tile.
27 *
28 ******************************************************************************/
29 #pragma once
30
31 #include <set>
32 #include <unordered_map>
33 #include "common/formats.h"
34 #include "fifo.hpp"
35 #include "context.h"
36 #include "format_traits.h"
37
38 //////////////////////////////////////////////////////////////////////////
39 /// MacroTile - work queue for a tile.
40 //////////////////////////////////////////////////////////////////////////
41 struct MacroTileQueue
42 {
43 MacroTileQueue() { }
44 ~MacroTileQueue() { }
45
46 //////////////////////////////////////////////////////////////////////////
47 /// @brief Returns number of work items queued for this tile.
48 uint32_t getNumQueued()
49 {
50 return mFifo.getNumQueued();
51 }
52
53 //////////////////////////////////////////////////////////////////////////
54 /// @brief Attempt to lock the work fifo. If already locked then return false.
55 bool tryLock()
56 {
57 return mFifo.tryLock();
58 }
59
60 //////////////////////////////////////////////////////////////////////////
61 /// @brief Clear fifo and unlock it.
62 void clear(Arena& arena)
63 {
64 mFifo.clear(arena);
65 }
66
67 //////////////////////////////////////////////////////////////////////////
68 /// @brief Peek at work sitting at the front of the fifo.
69 BE_WORK* peek()
70 {
71 return mFifo.peek();
72 }
73
74 bool enqueue_try_nosync(Arena& arena, const BE_WORK* entry)
75 {
76 return mFifo.enqueue_try_nosync(arena, entry);
77 }
78
79 //////////////////////////////////////////////////////////////////////////
80 /// @brief Move to next work item
81 void dequeue()
82 {
83 mFifo.dequeue_noinc();
84 }
85
86 //////////////////////////////////////////////////////////////////////////
87 /// @brief Destroy fifo
88 void destroy()
89 {
90 mFifo.destroy();
91 }
92
93 ///@todo This will all be private.
94 uint32_t mWorkItemsFE = 0;
95 uint32_t mWorkItemsBE = 0;
96
97 private:
98 QUEUE<BE_WORK> mFifo;
99 };
100
101 //////////////////////////////////////////////////////////////////////////
102 /// MacroTileMgr - Manages macrotiles for a draw.
103 //////////////////////////////////////////////////////////////////////////
104 class MacroTileMgr
105 {
106 public:
107 MacroTileMgr(Arena& arena);
108 ~MacroTileMgr()
109 {
110 for (auto &tile : mTiles)
111 {
112 tile.second.destroy();
113 }
114 }
115
116 void initialize();
117 INLINE std::vector<uint32_t>& getDirtyTiles() { return mDirtyTiles; }
118 INLINE MacroTileQueue& getMacroTileQueue(uint32_t id) { return mTiles[id]; }
119 void markTileComplete(uint32_t id);
120
121 INLINE bool isWorkComplete()
122 {
123 return mWorkItemsProduced == mWorkItemsConsumed;
124 }
125
126 void enqueue(uint32_t x, uint32_t y, BE_WORK *pWork);
127
128 static INLINE void getTileIndices(uint32_t tileID, uint32_t &x, uint32_t &y)
129 {
130 y = tileID & 0xffff;
131 x = (tileID >> 16) & 0xffff;
132 }
133
134 void *operator new(size_t size);
135 void operator delete (void *p);
136
137 private:
138 Arena& mArena;
139 std::unordered_map<uint32_t, MacroTileQueue> mTiles;
140
141 // Any tile that has work queued to it is a dirty tile.
142 std::vector<uint32_t> mDirtyTiles;
143
144 OSALIGNLINE(LONG) mWorkItemsProduced { 0 };
145 OSALIGNLINE(volatile LONG) mWorkItemsConsumed { 0 };
146 };
147
148 //////////////////////////////////////////////////////////////////////////
149 /// DispatchQueue - work queue for dispatch
150 //////////////////////////////////////////////////////////////////////////
151 class DispatchQueue
152 {
153 public:
154 DispatchQueue() {}
155
156 //////////////////////////////////////////////////////////////////////////
157 /// @brief Setup the producer consumer counts.
158 void initialize(uint32_t totalTasks, void* pTaskData)
159 {
160 // The available and outstanding counts start with total tasks.
161 // At the start there are N tasks available and outstanding.
162 // When both the available and outstanding counts have reached 0 then all work has completed.
163 // When a worker starts on a threadgroup then it decrements the available count.
164 // When a worker completes a threadgroup then it decrements the outstanding count.
165
166 mTasksAvailable = totalTasks;
167 mTasksOutstanding = totalTasks;
168
169 mpTaskData = pTaskData;
170 }
171
172 //////////////////////////////////////////////////////////////////////////
173 /// @brief Returns number of tasks available for this dispatch.
174 uint32_t getNumQueued()
175 {
176 return (mTasksAvailable > 0) ? mTasksAvailable : 0;
177 }
178
179 //////////////////////////////////////////////////////////////////////////
180 /// @brief Atomically decrement the work available count. If the result
181 // is greater than 0 then we can on the associated thread group.
182 // Otherwise, there is no more work to do.
183 bool getWork(uint32_t& groupId)
184 {
185 LONG result = InterlockedDecrement(&mTasksAvailable);
186
187 if (result >= 0)
188 {
189 groupId = result;
190 return true;
191 }
192
193 return false;
194 }
195
196 //////////////////////////////////////////////////////////////////////////
197 /// @brief Atomically decrement the outstanding count. A worker is notifying
198 /// us that he just finished some work. Also, return true if we're
199 /// the last worker to complete this dispatch.
200 bool finishedWork()
201 {
202 LONG result = InterlockedDecrement(&mTasksOutstanding);
203 SWR_ASSERT(result >= 0, "Should never oversubscribe work");
204
205 return (result == 0) ? true : false;
206 }
207
208 //////////////////////////////////////////////////////////////////////////
209 /// @brief Work is complete once both the available/outstanding counts have reached 0.
210 bool isWorkComplete()
211 {
212 return ((mTasksAvailable <= 0) &&
213 (mTasksOutstanding <= 0));
214 }
215
216 //////////////////////////////////////////////////////////////////////////
217 /// @brief Return pointer to task data.
218 const void* GetTasksData()
219 {
220 return mpTaskData;
221 }
222
223 void *operator new(size_t size);
224 void operator delete (void *p);
225
226 void* mpTaskData{ nullptr }; // The API thread will set this up and the callback task function will interpet this.
227
228 OSALIGNLINE(volatile LONG) mTasksAvailable{ 0 };
229 OSALIGNLINE(volatile LONG) mTasksOutstanding{ 0 };
230 };
231
232
233 enum HOTTILE_STATE
234 {
235 HOTTILE_INVALID, // tile is in unitialized state and should be loaded with surface contents before rendering
236 HOTTILE_CLEAR, // tile should be cleared
237 HOTTILE_DIRTY, // tile has been rendered to
238 HOTTILE_RESOLVED, // tile has been stored to memory
239 };
240
241 struct HOTTILE
242 {
243 BYTE *pBuffer;
244 HOTTILE_STATE state;
245 DWORD clearData[4]; // May need to change based on pfnClearTile implementation. Reorder for alignment?
246 uint32_t numSamples;
247 uint32_t renderTargetArrayIndex; // current render target array index loaded
248 };
249
250 union HotTileSet
251 {
252 struct
253 {
254 HOTTILE Color[SWR_NUM_RENDERTARGETS];
255 HOTTILE Depth;
256 HOTTILE Stencil;
257 };
258 HOTTILE Attachment[SWR_NUM_ATTACHMENTS];
259 };
260
261 class HotTileMgr
262 {
263 public:
264 HotTileMgr()
265 {
266 memset(&mHotTiles[0][0], 0, sizeof(mHotTiles));
267
268 // cache hottile size
269 for (uint32_t i = SWR_ATTACHMENT_COLOR0; i <= SWR_ATTACHMENT_COLOR7; ++i)
270 {
271 mHotTileSize[i] = KNOB_MACROTILE_X_DIM * KNOB_MACROTILE_Y_DIM * FormatTraits<KNOB_COLOR_HOT_TILE_FORMAT>::bpp / 8;
272 }
273 mHotTileSize[SWR_ATTACHMENT_DEPTH] = KNOB_MACROTILE_X_DIM * KNOB_MACROTILE_Y_DIM * FormatTraits<KNOB_DEPTH_HOT_TILE_FORMAT>::bpp / 8;
274 mHotTileSize[SWR_ATTACHMENT_STENCIL] = KNOB_MACROTILE_X_DIM * KNOB_MACROTILE_Y_DIM * FormatTraits<KNOB_STENCIL_HOT_TILE_FORMAT>::bpp / 8;
275 }
276
277 ~HotTileMgr()
278 {
279 for (int x = 0; x < KNOB_NUM_HOT_TILES_X; ++x)
280 {
281 for (int y = 0; y < KNOB_NUM_HOT_TILES_Y; ++y)
282 {
283 for (int a = 0; a < SWR_NUM_ATTACHMENTS; ++a)
284 {
285 if (mHotTiles[x][y].Attachment[a].pBuffer != NULL)
286 {
287 _aligned_free(mHotTiles[x][y].Attachment[a].pBuffer);
288 mHotTiles[x][y].Attachment[a].pBuffer = NULL;
289 }
290 }
291 }
292 }
293 }
294
295 void InitializeHotTiles(SWR_CONTEXT* pContext, DRAW_CONTEXT* pDC, uint32_t macroID);
296
297 HOTTILE *GetHotTile(SWR_CONTEXT* pContext, DRAW_CONTEXT* pDC, uint32_t macroID, SWR_RENDERTARGET_ATTACHMENT attachment, bool create, uint32_t numSamples = 1,
298 uint32_t renderTargetArrayIndex = 0);
299
300 static void ClearColorHotTile(const HOTTILE* pHotTile);
301 static void ClearDepthHotTile(const HOTTILE* pHotTile);
302 static void ClearStencilHotTile(const HOTTILE* pHotTile);
303
304 private:
305 HotTileSet mHotTiles[KNOB_NUM_HOT_TILES_X][KNOB_NUM_HOT_TILES_Y];
306 uint32_t mHotTileSize[SWR_NUM_ATTACHMENTS];
307 };
308