llvmpipe: use single swizzled tile
[mesa.git] / src / gallium / drivers / llvmpipe / lp_rast_priv.h
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef LP_RAST_PRIV_H
29 #define LP_RAST_PRIV_H
30
31 #include "os/os_thread.h"
32 #include "util/u_format.h"
33 #include "gallivm/lp_bld_debug.h"
34 #include "lp_memory.h"
35 #include "lp_rast.h"
36 #include "lp_scene.h"
37 #include "lp_state.h"
38 #include "lp_texture.h"
39 #include "lp_tile_soa.h"
40 #include "lp_limits.h"
41
42
43 struct lp_rasterizer;
44
45
46 /**
47 * Per-thread rasterization state
48 */
49 struct lp_rasterizer_task
50 {
51 unsigned x, y; /**< Pos of this tile in framebuffer, in pixels */
52
53 uint8_t *color_tiles[PIPE_MAX_COLOR_BUFS];
54 uint8_t *depth_tile;
55
56 /** "back" pointer */
57 struct lp_rasterizer *rast;
58
59 /** "my" index */
60 unsigned thread_index;
61
62 /* occlude counter for visiable pixels */
63 uint32_t vis_counter;
64
65 pipe_semaphore work_ready;
66 pipe_semaphore work_done;
67 };
68
69
70 /**
71 * This is the state required while rasterizing tiles.
72 * Note that this contains per-thread information too.
73 * The tile size is TILE_SIZE x TILE_SIZE pixels.
74 */
75 struct lp_rasterizer
76 {
77 boolean exit_flag;
78
79 /* Framebuffer stuff
80 */
81 struct {
82 uint8_t *map;
83 unsigned stride;
84 unsigned blocksize;
85 } zsbuf;
86
87 struct {
88 unsigned nr_cbufs;
89 unsigned clear_color;
90 unsigned clear_depth;
91 char clear_stencil;
92 } state;
93
94 /** The incoming queue of scenes ready to rasterize */
95 struct lp_scene_queue *full_scenes;
96
97 /**
98 * The outgoing queue of processed scenes to return to setup module
99 *
100 * XXX: while scenes are per-context but the rasterizer is
101 * (potentially) shared, these empty scenes should be returned to
102 * the context which created them rather than retained here.
103 */
104 /* struct lp_scene_queue *empty_scenes; */
105
106 /** The scene currently being rasterized by the threads */
107 struct lp_scene *curr_scene;
108
109 /** A task object for each rasterization thread */
110 struct lp_rasterizer_task tasks[LP_MAX_THREADS];
111
112 unsigned num_threads;
113 pipe_thread threads[LP_MAX_THREADS];
114
115 /** For synchronizing the rasterization threads */
116 pipe_barrier barrier;
117 };
118
119
120 void
121 lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,
122 const struct lp_rast_shader_inputs *inputs,
123 unsigned x, unsigned y,
124 unsigned mask);
125
126
127
128 /**
129 * Get the pointer to a 4x4 depth/stencil block.
130 * We'll map the z/stencil buffer on demand here.
131 * Note that this may be called even when there's no z/stencil buffer - return
132 * NULL in that case.
133 * \param x, y location of 4x4 block in window coords
134 */
135 static INLINE void *
136 lp_rast_get_depth_block_pointer(struct lp_rasterizer_task *task,
137 unsigned x, unsigned y)
138 {
139 const struct lp_rasterizer *rast = task->rast;
140 void *depth;
141
142 assert((x % TILE_VECTOR_WIDTH) == 0);
143 assert((y % TILE_VECTOR_HEIGHT) == 0);
144
145 if (!rast->zsbuf.map) {
146 /* Either out of memory or no zsbuf. Can't tell without access
147 * to the state. Just use dummy tile memory, but don't print
148 * the oom warning as this most likely because there is no
149 * zsbuf.
150 */
151 return lp_dummy_tile;
152 }
153
154 depth = (rast->zsbuf.map +
155 rast->zsbuf.stride * y +
156 rast->zsbuf.blocksize * x * TILE_VECTOR_HEIGHT);
157
158 assert(lp_check_alignment(depth, 16));
159 return depth;
160 }
161
162
163 /**
164 * Get pointer to the swizzled color tile
165 */
166 static INLINE uint8_t *
167 lp_rast_get_color_tile_pointer(struct lp_rasterizer_task *task,
168 unsigned buf, enum lp_texture_usage usage)
169 {
170 struct lp_rasterizer *rast = task->rast;
171
172 assert(task->x % TILE_SIZE == 0);
173 assert(task->y % TILE_SIZE == 0);
174 assert(buf < rast->state.nr_cbufs);
175
176 if (!task->color_tiles[buf]) {
177 struct pipe_surface *cbuf = rast->curr_scene->fb.cbufs[buf];
178 struct llvmpipe_resource *lpt;
179 assert(cbuf);
180 lpt = llvmpipe_resource(cbuf->texture);
181 task->color_tiles[buf] = lp_swizzled_cbuf[task->thread_index][buf];
182
183 if (usage != LP_TEX_USAGE_WRITE_ALL) {
184 llvmpipe_swizzle_cbuf_tile(lpt,
185 cbuf->face + cbuf->zslice,
186 cbuf->level,
187 task->x, task->y,
188 task->color_tiles[buf]);
189 }
190 }
191
192 return task->color_tiles[buf];
193 }
194
195
196 /**
197 * Get the pointer to a 4x4 color block (within a 64x64 tile).
198 * We'll map the color buffer on demand here.
199 * Note that this may be called even when there's no color buffers - return
200 * NULL in that case.
201 * \param x, y location of 4x4 block in window coords
202 */
203 static INLINE uint8_t *
204 lp_rast_get_color_block_pointer(struct lp_rasterizer_task *task,
205 unsigned buf, unsigned x, unsigned y)
206 {
207 unsigned px, py, pixel_offset;
208 uint8_t *color;
209
210 assert((x % TILE_VECTOR_WIDTH) == 0);
211 assert((y % TILE_VECTOR_HEIGHT) == 0);
212
213 color = lp_rast_get_color_tile_pointer(task, buf, LP_TEX_USAGE_READ_WRITE);
214 assert(color);
215
216 px = x % TILE_SIZE;
217 py = y % TILE_SIZE;
218 pixel_offset = tile_pixel_offset(px, py, 0);
219
220 color = color + pixel_offset;
221
222 assert(lp_check_alignment(color, 16));
223 return color;
224 }
225
226
227
228 /**
229 * Shade all pixels in a 4x4 block. The fragment code omits the
230 * triangle in/out tests.
231 * \param x, y location of 4x4 block in window coords
232 */
233 static INLINE void
234 lp_rast_shade_quads_all( struct lp_rasterizer_task *task,
235 const struct lp_rast_shader_inputs *inputs,
236 unsigned x, unsigned y )
237 {
238 const struct lp_rasterizer *rast = task->rast;
239 const struct lp_rast_state *state = inputs->state;
240 struct lp_fragment_shader_variant *variant = state->variant;
241 uint8_t *color[PIPE_MAX_COLOR_BUFS];
242 void *depth;
243 unsigned i;
244
245 /* color buffer */
246 for (i = 0; i < rast->state.nr_cbufs; i++)
247 color[i] = lp_rast_get_color_block_pointer(task, i, x, y);
248
249 depth = lp_rast_get_depth_block_pointer(task, x, y);
250
251 /* run shader on 4x4 block */
252 variant->jit_function[RAST_WHOLE]( &state->jit_context,
253 x, y,
254 inputs->facing,
255 inputs->a0,
256 inputs->dadx,
257 inputs->dady,
258 color,
259 depth,
260 0xffff,
261 &task->vis_counter );
262 }
263
264
265 #endif