lp_rast_priv.h revision 18452c1e87f79327fbd5f27478028b481ee72a5d
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/* If we crash in a jitted function, we can examine jit_line and jit_state 44 * to get some info. This is not thread-safe, however. 45 */ 46#ifdef DEBUG 47 48extern int jit_line; 49extern const struct lp_rast_state *jit_state; 50 51#define BEGIN_JIT_CALL(state) \ 52 do { \ 53 jit_line = __LINE__; \ 54 jit_state = state; \ 55 } while (0) 56 57#define END_JIT_CALL() \ 58 do { \ 59 jit_line = 0; \ 60 jit_state = NULL; \ 61 } while (0) 62 63#else 64 65#define BEGIN_JIT_CALL(X) 66#define END_JIT_CALL() 67 68#endif 69 70 71struct lp_rasterizer; 72 73 74/** 75 * Per-thread rasterization state 76 */ 77struct lp_rasterizer_task 78{ 79 unsigned x, y; /**< Pos of this tile in framebuffer, in pixels */ 80 81 uint8_t *color_tiles[PIPE_MAX_COLOR_BUFS]; 82 uint8_t *depth_tile; 83 84 /** "back" pointer */ 85 struct lp_rasterizer *rast; 86 87 /** "my" index */ 88 unsigned thread_index; 89 90 /* occlude counter for visiable pixels */ 91 uint32_t vis_counter; 92 struct llvmpipe_query *query; 93 94 pipe_semaphore work_ready; 95 pipe_semaphore work_done; 96}; 97 98 99/** 100 * This is the state required while rasterizing tiles. 101 * Note that this contains per-thread information too. 102 * The tile size is TILE_SIZE x TILE_SIZE pixels. 103 */ 104struct lp_rasterizer 105{ 106 boolean exit_flag; 107 108 /* Framebuffer stuff 109 */ 110 struct { 111 uint8_t *map; 112 unsigned stride; 113 unsigned blocksize; 114 } zsbuf; 115 116 struct { 117 unsigned nr_cbufs; 118 unsigned clear_color; 119 unsigned clear_depth; 120 char clear_stencil; 121 } state; 122 123 /** The incoming queue of scenes ready to rasterize */ 124 struct lp_scene_queue *full_scenes; 125 126 /** 127 * The outgoing queue of processed scenes to return to setup module 128 * 129 * XXX: while scenes are per-context but the rasterizer is 130 * (potentially) shared, these empty scenes should be returned to 131 * the context which created them rather than retained here. 132 */ 133 /* struct lp_scene_queue *empty_scenes; */ 134 135 /** The scene currently being rasterized by the threads */ 136 struct lp_scene *curr_scene; 137 138 /** A task object for each rasterization thread */ 139 struct lp_rasterizer_task tasks[LP_MAX_THREADS]; 140 141 unsigned num_threads; 142 pipe_thread threads[LP_MAX_THREADS]; 143 144 /** For synchronizing the rasterization threads */ 145 pipe_barrier barrier; 146}; 147 148 149void 150lp_rast_shade_quads_mask(struct lp_rasterizer_task *task, 151 const struct lp_rast_shader_inputs *inputs, 152 unsigned x, unsigned y, 153 unsigned mask); 154 155 156 157/** 158 * Get the pointer to a 4x4 depth/stencil block. 159 * We'll map the z/stencil buffer on demand here. 160 * Note that this may be called even when there's no z/stencil buffer - return 161 * NULL in that case. 162 * \param x, y location of 4x4 block in window coords 163 */ 164static INLINE void * 165lp_rast_get_depth_block_pointer(struct lp_rasterizer_task *task, 166 unsigned x, unsigned y) 167{ 168 const struct lp_rasterizer *rast = task->rast; 169 void *depth; 170 171 assert((x % TILE_VECTOR_WIDTH) == 0); 172 assert((y % TILE_VECTOR_HEIGHT) == 0); 173 174 if (!rast->zsbuf.map) { 175 /* Either out of memory or no zsbuf. Can't tell without access 176 * to the state. Just use dummy tile memory, but don't print 177 * the oom warning as this most likely because there is no 178 * zsbuf. 179 */ 180 return lp_dummy_tile; 181 } 182 183 depth = (rast->zsbuf.map + 184 rast->zsbuf.stride * y + 185 rast->zsbuf.blocksize * x * TILE_VECTOR_HEIGHT); 186 187 assert(lp_check_alignment(depth, 16)); 188 return depth; 189} 190 191 192/** 193 * Get pointer to the swizzled color tile 194 */ 195static INLINE uint8_t * 196lp_rast_get_color_tile_pointer(struct lp_rasterizer_task *task, 197 unsigned buf, enum lp_texture_usage usage) 198{ 199 struct lp_rasterizer *rast = task->rast; 200 201 assert(task->x % TILE_SIZE == 0); 202 assert(task->y % TILE_SIZE == 0); 203 assert(buf < rast->state.nr_cbufs); 204 205 if (!task->color_tiles[buf]) { 206 struct pipe_surface *cbuf = rast->curr_scene->fb.cbufs[buf]; 207 struct llvmpipe_resource *lpt; 208 assert(cbuf); 209 lpt = llvmpipe_resource(cbuf->texture); 210 task->color_tiles[buf] = lp_swizzled_cbuf[task->thread_index][buf]; 211 212 if (usage != LP_TEX_USAGE_WRITE_ALL) { 213 llvmpipe_swizzle_cbuf_tile(lpt, 214 cbuf->face + cbuf->zslice, 215 cbuf->level, 216 task->x, task->y, 217 task->color_tiles[buf]); 218 } 219 } 220 221 return task->color_tiles[buf]; 222} 223 224 225/** 226 * Get the pointer to a 4x4 color block (within a 64x64 tile). 227 * We'll map the color buffer on demand here. 228 * Note that this may be called even when there's no color buffers - return 229 * NULL in that case. 230 * \param x, y location of 4x4 block in window coords 231 */ 232static INLINE uint8_t * 233lp_rast_get_color_block_pointer(struct lp_rasterizer_task *task, 234 unsigned buf, unsigned x, unsigned y) 235{ 236 unsigned px, py, pixel_offset; 237 uint8_t *color; 238 239 assert((x % TILE_VECTOR_WIDTH) == 0); 240 assert((y % TILE_VECTOR_HEIGHT) == 0); 241 242 color = lp_rast_get_color_tile_pointer(task, buf, LP_TEX_USAGE_READ_WRITE); 243 assert(color); 244 245 px = x % TILE_SIZE; 246 py = y % TILE_SIZE; 247 pixel_offset = tile_pixel_offset(px, py, 0); 248 249 color = color + pixel_offset; 250 251 assert(lp_check_alignment(color, 16)); 252 return color; 253} 254 255 256 257/** 258 * Shade all pixels in a 4x4 block. The fragment code omits the 259 * triangle in/out tests. 260 * \param x, y location of 4x4 block in window coords 261 */ 262static INLINE void 263lp_rast_shade_quads_all( struct lp_rasterizer_task *task, 264 const struct lp_rast_shader_inputs *inputs, 265 unsigned x, unsigned y ) 266{ 267 const struct lp_rasterizer *rast = task->rast; 268 const struct lp_rast_state *state = inputs->state; 269 struct lp_fragment_shader_variant *variant = state->variant; 270 uint8_t *color[PIPE_MAX_COLOR_BUFS]; 271 void *depth; 272 unsigned i; 273 274 /* color buffer */ 275 for (i = 0; i < rast->state.nr_cbufs; i++) 276 color[i] = lp_rast_get_color_block_pointer(task, i, x, y); 277 278 depth = lp_rast_get_depth_block_pointer(task, x, y); 279 280 /* run shader on 4x4 block */ 281 BEGIN_JIT_CALL(state); 282 variant->jit_function[RAST_WHOLE]( &state->jit_context, 283 x, y, 284 inputs->facing, 285 inputs->a0, 286 inputs->dadx, 287 inputs->dady, 288 color, 289 depth, 290 0xffff, 291 &task->vis_counter ); 292 END_JIT_CALL(); 293} 294 295 296#endif 297