Path: blob/21.2-virgl/src/gallium/drivers/llvmpipe/lp_rast.c
4570 views
/**************************************************************************1*2* Copyright 2009 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include <limits.h>28#include "util/u_memory.h"29#include "util/u_math.h"30#include "util/u_rect.h"31#include "util/u_surface.h"32#include "util/u_pack_color.h"33#include "util/u_string.h"34#include "util/u_thread.h"35#include "util/u_memset.h"36#include "util/os_time.h"3738#include "lp_scene_queue.h"39#include "lp_context.h"40#include "lp_debug.h"41#include "lp_fence.h"42#include "lp_perf.h"43#include "lp_query.h"44#include "lp_rast.h"45#include "lp_rast_priv.h"46#include "gallivm/lp_bld_format.h"47#include "gallivm/lp_bld_debug.h"48#include "lp_scene.h"49#include "lp_tex_sample.h"505152#ifdef DEBUG53int jit_line = 0;54const struct lp_rast_state *jit_state = NULL;55const struct lp_rasterizer_task *jit_task = NULL;56#endif5758const float lp_sample_pos_4x[4][2] = { { 0.375, 0.125 },59{ 0.875, 0.375 },60{ 0.125, 0.625 },61{ 0.625, 0.875 } };6263/**64* Begin rasterizing a scene.65* Called once per scene by one thread.66*/67static void68lp_rast_begin( struct lp_rasterizer *rast,69struct lp_scene *scene )70{71rast->curr_scene = scene;7273LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);7475lp_scene_begin_rasterization( scene );76lp_scene_bin_iter_begin( scene );77}787980static void81lp_rast_end( struct lp_rasterizer *rast )82{83lp_scene_end_rasterization( rast->curr_scene );8485rast->curr_scene = NULL;86}878889/**90* Beginning rasterization of a tile.91* \param x window X position of the tile, in pixels92* \param y window Y position of the tile, in pixels93*/94static void95lp_rast_tile_begin(struct lp_rasterizer_task *task,96const struct cmd_bin *bin,97int x, int y)98{99unsigned i;100struct lp_scene *scene = task->scene;101102LP_DBG(DEBUG_RAST, "%s %d,%d\n", __FUNCTION__, x, y);103104task->bin = bin;105task->x = x * TILE_SIZE;106task->y = y * TILE_SIZE;107task->width = TILE_SIZE + x * TILE_SIZE > task->scene->fb.width ?108task->scene->fb.width - x * TILE_SIZE : TILE_SIZE;109task->height = TILE_SIZE + y * TILE_SIZE > task->scene->fb.height ?110task->scene->fb.height - y * TILE_SIZE : TILE_SIZE;111112task->thread_data.vis_counter = 0;113task->thread_data.ps_invocations = 0;114115for (i = 0; i < task->scene->fb.nr_cbufs; i++) {116if (task->scene->fb.cbufs[i]) {117task->color_tiles[i] = scene->cbufs[i].map +118scene->cbufs[i].stride * task->y +119scene->cbufs[i].format_bytes * task->x;120}121}122if (task->scene->fb.zsbuf) {123task->depth_tile = scene->zsbuf.map +124scene->zsbuf.stride * task->y +125scene->zsbuf.format_bytes * task->x;126}127}128129130/**131* Clear the rasterizer's current color tile.132* This is a bin command called during bin processing.133* Clear commands always clear all bound layers.134*/135static void136lp_rast_clear_color(struct lp_rasterizer_task *task,137const union lp_rast_cmd_arg arg)138{139const struct lp_scene *scene = task->scene;140unsigned cbuf = arg.clear_rb->cbuf;141union util_color uc;142enum pipe_format format;143144/* we never bin clear commands for non-existing buffers */145assert(cbuf < scene->fb.nr_cbufs);146assert(scene->fb.cbufs[cbuf]);147148format = scene->fb.cbufs[cbuf]->format;149uc = arg.clear_rb->color_val;150151/*152* this is pretty rough since we have target format (bunch of bytes...) here.153* dump it as raw 4 dwords.154*/155LP_DBG(DEBUG_RAST, "%s clear value (target format %d) raw 0x%x,0x%x,0x%x,0x%x\n",156__FUNCTION__, format, uc.ui[0], uc.ui[1], uc.ui[2], uc.ui[3]);157158for (unsigned s = 0; s < scene->cbufs[cbuf].nr_samples; s++) {159void *map = (char *)scene->cbufs[cbuf].map + scene->cbufs[cbuf].sample_stride * s;160util_fill_box(map,161format,162scene->cbufs[cbuf].stride,163scene->cbufs[cbuf].layer_stride,164task->x,165task->y,1660,167task->width,168task->height,169scene->fb_max_layer + 1,170&uc);171}172173/* this will increase for each rb which probably doesn't mean much */174LP_COUNT(nr_color_tile_clear);175}176177178/**179* Clear the rasterizer's current z/stencil tile.180* This is a bin command called during bin processing.181* Clear commands always clear all bound layers.182*/183static void184lp_rast_clear_zstencil(struct lp_rasterizer_task *task,185const union lp_rast_cmd_arg arg)186{187const struct lp_scene *scene = task->scene;188uint64_t clear_value64 = arg.clear_zstencil.value;189uint64_t clear_mask64 = arg.clear_zstencil.mask;190uint32_t clear_value = (uint32_t) clear_value64;191uint32_t clear_mask = (uint32_t) clear_mask64;192const unsigned height = task->height;193const unsigned width = task->width;194const unsigned dst_stride = scene->zsbuf.stride;195uint8_t *dst;196unsigned i, j;197unsigned block_size;198199LP_DBG(DEBUG_RAST, "%s: value=0x%08x, mask=0x%08x\n",200__FUNCTION__, clear_value, clear_mask);201202/*203* Clear the area of the depth/depth buffer matching this tile.204*/205206if (scene->fb.zsbuf) {207unsigned layer;208209for (unsigned s = 0; s < scene->zsbuf.nr_samples; s++) {210uint8_t *dst_layer = task->depth_tile + (s * scene->zsbuf.sample_stride);211block_size = util_format_get_blocksize(scene->fb.zsbuf->format);212213clear_value &= clear_mask;214215for (layer = 0; layer <= scene->fb_max_layer; layer++) {216dst = dst_layer;217218switch (block_size) {219case 1:220assert(clear_mask == 0xff);221for (i = 0; i < height; i++) {222uint8_t *row = (uint8_t *)dst;223memset(row, (uint8_t) clear_value, width);224dst += dst_stride;225}226break;227case 2:228if (clear_mask == 0xffff) {229for (i = 0; i < height; i++) {230uint16_t *row = (uint16_t *)dst;231for (j = 0; j < width; j++)232*row++ = (uint16_t) clear_value;233dst += dst_stride;234}235}236else {237for (i = 0; i < height; i++) {238uint16_t *row = (uint16_t *)dst;239for (j = 0; j < width; j++) {240uint16_t tmp = ~clear_mask & *row;241*row++ = clear_value | tmp;242}243dst += dst_stride;244}245}246break;247case 4:248if (clear_mask == 0xffffffff) {249for (i = 0; i < height; i++) {250util_memset32(dst, clear_value, width);251dst += dst_stride;252}253}254else {255for (i = 0; i < height; i++) {256uint32_t *row = (uint32_t *)dst;257for (j = 0; j < width; j++) {258uint32_t tmp = ~clear_mask & *row;259*row++ = clear_value | tmp;260}261dst += dst_stride;262}263}264break;265case 8:266clear_value64 &= clear_mask64;267if (clear_mask64 == 0xffffffffffULL) {268for (i = 0; i < height; i++) {269util_memset64(dst, clear_value64, width);270dst += dst_stride;271}272}273else {274for (i = 0; i < height; i++) {275uint64_t *row = (uint64_t *)dst;276for (j = 0; j < width; j++) {277uint64_t tmp = ~clear_mask64 & *row;278*row++ = clear_value64 | tmp;279}280dst += dst_stride;281}282}283break;284285default:286assert(0);287break;288}289dst_layer += scene->zsbuf.layer_stride;290}291}292}293}294295296297/**298* Run the shader on all blocks in a tile. This is used when a tile is299* completely contained inside a triangle.300* This is a bin command called during bin processing.301*/302static void303lp_rast_shade_tile(struct lp_rasterizer_task *task,304const union lp_rast_cmd_arg arg)305{306const struct lp_scene *scene = task->scene;307const struct lp_rast_shader_inputs *inputs = arg.shade_tile;308const struct lp_rast_state *state;309struct lp_fragment_shader_variant *variant;310const unsigned tile_x = task->x, tile_y = task->y;311unsigned x, y;312313if (inputs->disable) {314/* This command was partially binned and has been disabled */315return;316}317318LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);319320state = task->state;321assert(state);322if (!state) {323return;324}325variant = state->variant;326327/* render the whole 64x64 tile in 4x4 chunks */328for (y = 0; y < task->height; y += 4){329for (x = 0; x < task->width; x += 4) {330uint8_t *color[PIPE_MAX_COLOR_BUFS];331unsigned stride[PIPE_MAX_COLOR_BUFS];332unsigned sample_stride[PIPE_MAX_COLOR_BUFS];333uint8_t *depth = NULL;334unsigned depth_stride = 0;335unsigned depth_sample_stride = 0;336unsigned i;337338/* color buffer */339for (i = 0; i < scene->fb.nr_cbufs; i++){340if (scene->fb.cbufs[i]) {341stride[i] = scene->cbufs[i].stride;342sample_stride[i] = scene->cbufs[i].sample_stride;343color[i] = lp_rast_get_color_block_pointer(task, i, tile_x + x,344tile_y + y, inputs->layer + inputs->view_index);345}346else {347stride[i] = 0;348sample_stride[i] = 0;349color[i] = NULL;350}351}352353/* depth buffer */354if (scene->zsbuf.map) {355depth = lp_rast_get_depth_block_pointer(task, tile_x + x,356tile_y + y, inputs->layer + inputs->view_index);357depth_stride = scene->zsbuf.stride;358depth_sample_stride = scene->zsbuf.sample_stride;359}360361uint64_t mask = 0;362for (unsigned i = 0; i < scene->fb_max_samples; i++)363mask |= (uint64_t)(0xffff) << (16 * i);364365/* Propagate non-interpolated raster state. */366task->thread_data.raster_state.viewport_index = inputs->viewport_index;367task->thread_data.raster_state.view_index = inputs->view_index;368369/* run shader on 4x4 block */370BEGIN_JIT_CALL(state, task);371variant->jit_function[RAST_WHOLE]( &state->jit_context,372tile_x + x, tile_y + y,373inputs->frontfacing,374GET_A0(inputs),375GET_DADX(inputs),376GET_DADY(inputs),377color,378depth,379mask,380&task->thread_data,381stride,382depth_stride,383sample_stride,384depth_sample_stride);385END_JIT_CALL();386}387}388}389390391/**392* Run the shader on all blocks in a tile. This is used when a tile is393* completely contained inside a triangle, and the shader is opaque.394* This is a bin command called during bin processing.395*/396static void397lp_rast_shade_tile_opaque(struct lp_rasterizer_task *task,398const union lp_rast_cmd_arg arg)399{400LP_DBG(DEBUG_RAST, "%s\n", __FUNCTION__);401402assert(task->state);403if (!task->state) {404return;405}406407lp_rast_shade_tile(task, arg);408}409410411/**412* Compute shading for a 4x4 block of pixels inside a triangle.413* This is a bin command called during bin processing.414* \param x X position of quad in window coords415* \param y Y position of quad in window coords416*/417void418lp_rast_shade_quads_mask_sample(struct lp_rasterizer_task *task,419const struct lp_rast_shader_inputs *inputs,420unsigned x, unsigned y,421uint64_t mask)422{423const struct lp_rast_state *state = task->state;424struct lp_fragment_shader_variant *variant = state->variant;425const struct lp_scene *scene = task->scene;426uint8_t *color[PIPE_MAX_COLOR_BUFS];427unsigned stride[PIPE_MAX_COLOR_BUFS];428unsigned sample_stride[PIPE_MAX_COLOR_BUFS];429uint8_t *depth = NULL;430unsigned depth_stride = 0;431unsigned depth_sample_stride = 0;432unsigned i;433434assert(state);435436/* Sanity checks */437assert(x < scene->tiles_x * TILE_SIZE);438assert(y < scene->tiles_y * TILE_SIZE);439assert(x % TILE_VECTOR_WIDTH == 0);440assert(y % TILE_VECTOR_HEIGHT == 0);441442assert((x % 4) == 0);443assert((y % 4) == 0);444445/* color buffer */446for (i = 0; i < scene->fb.nr_cbufs; i++) {447if (scene->fb.cbufs[i]) {448stride[i] = scene->cbufs[i].stride;449sample_stride[i] = scene->cbufs[i].sample_stride;450color[i] = lp_rast_get_color_block_pointer(task, i, x, y,451inputs->layer + inputs->view_index);452}453else {454stride[i] = 0;455sample_stride[i] = 0;456color[i] = NULL;457}458}459460/* depth buffer */461if (scene->zsbuf.map) {462depth_stride = scene->zsbuf.stride;463depth_sample_stride = scene->zsbuf.sample_stride;464depth = lp_rast_get_depth_block_pointer(task, x, y, inputs->layer + inputs->view_index);465}466467assert(lp_check_alignment(state->jit_context.u8_blend_color, 16));468469/*470* The rasterizer may produce fragments outside our471* allocated 4x4 blocks hence need to filter them out here.472*/473if ((x % TILE_SIZE) < task->width && (y % TILE_SIZE) < task->height) {474/* Propagate non-interpolated raster state. */475task->thread_data.raster_state.viewport_index = inputs->viewport_index;476task->thread_data.raster_state.view_index = inputs->view_index;477478/* run shader on 4x4 block */479BEGIN_JIT_CALL(state, task);480variant->jit_function[RAST_EDGE_TEST](&state->jit_context,481x, y,482inputs->frontfacing,483GET_A0(inputs),484GET_DADX(inputs),485GET_DADY(inputs),486color,487depth,488mask,489&task->thread_data,490stride,491depth_stride,492sample_stride,493depth_sample_stride);494END_JIT_CALL();495}496}497498void499lp_rast_shade_quads_mask(struct lp_rasterizer_task *task,500const struct lp_rast_shader_inputs *inputs,501unsigned x, unsigned y,502unsigned mask)503{504uint64_t new_mask = 0;505for (unsigned i = 0; i < task->scene->fb_max_samples; i++)506new_mask |= ((uint64_t)mask) << (16 * i);507lp_rast_shade_quads_mask_sample(task, inputs, x, y, new_mask);508}509510/**511* Begin a new occlusion query.512* This is a bin command put in all bins.513* Called per thread.514*/515static void516lp_rast_begin_query(struct lp_rasterizer_task *task,517const union lp_rast_cmd_arg arg)518{519struct llvmpipe_query *pq = arg.query_obj;520521switch (pq->type) {522case PIPE_QUERY_OCCLUSION_COUNTER:523case PIPE_QUERY_OCCLUSION_PREDICATE:524case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:525pq->start[task->thread_index] = task->thread_data.vis_counter;526break;527case PIPE_QUERY_PIPELINE_STATISTICS:528pq->start[task->thread_index] = task->thread_data.ps_invocations;529break;530case PIPE_QUERY_TIME_ELAPSED:531pq->start[task->thread_index] = os_time_get_nano();532break;533default:534assert(0);535break;536}537}538539540/**541* End the current occlusion query.542* This is a bin command put in all bins.543* Called per thread.544*/545static void546lp_rast_end_query(struct lp_rasterizer_task *task,547const union lp_rast_cmd_arg arg)548{549struct llvmpipe_query *pq = arg.query_obj;550551switch (pq->type) {552case PIPE_QUERY_OCCLUSION_COUNTER:553case PIPE_QUERY_OCCLUSION_PREDICATE:554case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:555pq->end[task->thread_index] +=556task->thread_data.vis_counter - pq->start[task->thread_index];557pq->start[task->thread_index] = 0;558break;559case PIPE_QUERY_TIMESTAMP:560case PIPE_QUERY_TIME_ELAPSED:561pq->end[task->thread_index] = os_time_get_nano();562break;563case PIPE_QUERY_PIPELINE_STATISTICS:564pq->end[task->thread_index] +=565task->thread_data.ps_invocations - pq->start[task->thread_index];566pq->start[task->thread_index] = 0;567break;568default:569assert(0);570break;571}572}573574575void576lp_rast_set_state(struct lp_rasterizer_task *task,577const union lp_rast_cmd_arg arg)578{579task->state = arg.state;580}581582583584/**585* Called when we're done writing to a color tile.586*/587static void588lp_rast_tile_end(struct lp_rasterizer_task *task)589{590unsigned i;591592for (i = 0; i < task->scene->num_active_queries; ++i) {593lp_rast_end_query(task, lp_rast_arg_query(task->scene->active_queries[i]));594}595596/* debug */597memset(task->color_tiles, 0, sizeof(task->color_tiles));598task->depth_tile = NULL;599600task->bin = NULL;601}602603static lp_rast_cmd_func dispatch[LP_RAST_OP_MAX] =604{605lp_rast_clear_color,606lp_rast_clear_zstencil,607lp_rast_triangle_1,608lp_rast_triangle_2,609lp_rast_triangle_3,610lp_rast_triangle_4,611lp_rast_triangle_5,612lp_rast_triangle_6,613lp_rast_triangle_7,614lp_rast_triangle_8,615lp_rast_triangle_3_4,616lp_rast_triangle_3_16,617lp_rast_triangle_4_16,618lp_rast_shade_tile,619lp_rast_shade_tile_opaque,620lp_rast_begin_query,621lp_rast_end_query,622lp_rast_set_state,623lp_rast_triangle_32_1,624lp_rast_triangle_32_2,625lp_rast_triangle_32_3,626lp_rast_triangle_32_4,627lp_rast_triangle_32_5,628lp_rast_triangle_32_6,629lp_rast_triangle_32_7,630lp_rast_triangle_32_8,631lp_rast_triangle_32_3_4,632lp_rast_triangle_32_3_16,633lp_rast_triangle_32_4_16,634lp_rast_triangle_ms_1,635lp_rast_triangle_ms_2,636lp_rast_triangle_ms_3,637lp_rast_triangle_ms_4,638lp_rast_triangle_ms_5,639lp_rast_triangle_ms_6,640lp_rast_triangle_ms_7,641lp_rast_triangle_ms_8,642lp_rast_triangle_ms_3_4,643lp_rast_triangle_ms_3_16,644lp_rast_triangle_ms_4_16,645};646647648static void649do_rasterize_bin(struct lp_rasterizer_task *task,650const struct cmd_bin *bin,651int x, int y)652{653const struct cmd_block *block;654unsigned k;655656if (0)657lp_debug_bin(bin, x, y);658659for (block = bin->head; block; block = block->next) {660for (k = 0; k < block->count; k++) {661dispatch[block->cmd[k]]( task, block->arg[k] );662}663}664}665666667668/**669* Rasterize commands for a single bin.670* \param x, y position of the bin's tile in the framebuffer671* Must be called between lp_rast_begin() and lp_rast_end().672* Called per thread.673*/674static void675rasterize_bin(struct lp_rasterizer_task *task,676const struct cmd_bin *bin, int x, int y )677{678lp_rast_tile_begin( task, bin, x, y );679680do_rasterize_bin(task, bin, x, y);681682lp_rast_tile_end(task);683684#ifdef DEBUG685/* Debug/Perf flags:686*/687if (bin->head->count == 1) {688if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE_OPAQUE)689LP_COUNT(nr_pure_shade_opaque_64);690else if (bin->head->cmd[0] == LP_RAST_OP_SHADE_TILE)691LP_COUNT(nr_pure_shade_64);692}693#endif694}695696697/* An empty bin is one that just loads the contents of the tile and698* stores them again unchanged. This typically happens when bins have699* been flushed for some reason in the middle of a frame, or when700* incremental updates are being made to a render target.701*702* Try to avoid doing pointless work in this case.703*/704static boolean705is_empty_bin( const struct cmd_bin *bin )706{707return bin->head == NULL;708}709710711/**712* Rasterize/execute all bins within a scene.713* Called per thread.714*/715static void716rasterize_scene(struct lp_rasterizer_task *task,717struct lp_scene *scene)718{719task->scene = scene;720721/* Clear the cache tags. This should not always be necessary but722simpler for now. */723#if LP_USE_TEXTURE_CACHE724memset(task->thread_data.cache->cache_tags, 0,725sizeof(task->thread_data.cache->cache_tags));726#if LP_BUILD_FORMAT_CACHE_DEBUG727task->thread_data.cache->cache_access_total = 0;728task->thread_data.cache->cache_access_miss = 0;729#endif730#endif731732if (!task->rast->no_rast) {733/* loop over scene bins, rasterize each */734{735struct cmd_bin *bin;736int i, j;737738assert(scene);739while ((bin = lp_scene_bin_iter_next(scene, &i, &j))) {740if (!is_empty_bin( bin ))741rasterize_bin(task, bin, i, j);742}743}744}745746747#if LP_BUILD_FORMAT_CACHE_DEBUG748{749uint64_t total, miss;750total = task->thread_data.cache->cache_access_total;751miss = task->thread_data.cache->cache_access_miss;752if (total) {753debug_printf("thread %d cache access %llu miss %llu hit rate %f\n",754task->thread_index, (long long unsigned)total,755(long long unsigned)miss,756(float)(total - miss)/(float)total);757}758}759#endif760761if (scene->fence) {762lp_fence_signal(scene->fence);763}764765task->scene = NULL;766}767768769/**770* Called by setup module when it has something for us to render.771*/772void773lp_rast_queue_scene( struct lp_rasterizer *rast,774struct lp_scene *scene)775{776LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);777778if (rast->num_threads == 0) {779/* no threading */780unsigned fpstate = util_fpstate_get();781782/* Make sure that denorms are treated like zeros. This is783* the behavior required by D3D10. OpenGL doesn't care.784*/785util_fpstate_set_denorms_to_zero(fpstate);786787lp_rast_begin( rast, scene );788789rasterize_scene( &rast->tasks[0], scene );790791lp_rast_end( rast );792793util_fpstate_set(fpstate);794795rast->curr_scene = NULL;796}797else {798/* threaded rendering! */799unsigned i;800801lp_scene_enqueue( rast->full_scenes, scene );802803/* signal the threads that there's work to do */804for (i = 0; i < rast->num_threads; i++) {805pipe_semaphore_signal(&rast->tasks[i].work_ready);806}807}808809LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);810}811812813void814lp_rast_finish( struct lp_rasterizer *rast )815{816if (rast->num_threads == 0) {817/* nothing to do */818}819else {820int i;821822/* wait for work to complete */823for (i = 0; i < rast->num_threads; i++) {824pipe_semaphore_wait(&rast->tasks[i].work_done);825}826}827}828829830/**831* This is the thread's main entrypoint.832* It's a simple loop:833* 1. wait for work834* 2. do work835* 3. signal that we're done836*/837static int838thread_function(void *init_data)839{840struct lp_rasterizer_task *task = (struct lp_rasterizer_task *) init_data;841struct lp_rasterizer *rast = task->rast;842boolean debug = false;843char thread_name[16];844unsigned fpstate;845846snprintf(thread_name, sizeof thread_name, "llvmpipe-%u", task->thread_index);847u_thread_setname(thread_name);848849/* Make sure that denorms are treated like zeros. This is850* the behavior required by D3D10. OpenGL doesn't care.851*/852fpstate = util_fpstate_get();853util_fpstate_set_denorms_to_zero(fpstate);854855while (1) {856/* wait for work */857if (debug)858debug_printf("thread %d waiting for work\n", task->thread_index);859pipe_semaphore_wait(&task->work_ready);860861if (rast->exit_flag)862break;863864if (task->thread_index == 0) {865/* thread[0]:866* - get next scene to rasterize867* - map the framebuffer surfaces868*/869lp_rast_begin( rast,870lp_scene_dequeue( rast->full_scenes, TRUE ) );871}872873/* Wait for all threads to get here so that threads[1+] don't874* get a null rast->curr_scene pointer.875*/876util_barrier_wait( &rast->barrier );877878/* do work */879if (debug)880debug_printf("thread %d doing work\n", task->thread_index);881882rasterize_scene(task,883rast->curr_scene);884885/* wait for all threads to finish with this scene */886util_barrier_wait( &rast->barrier );887888/* XXX: shouldn't be necessary:889*/890if (task->thread_index == 0) {891lp_rast_end( rast );892}893894/* signal done with work */895if (debug)896debug_printf("thread %d done working\n", task->thread_index);897898pipe_semaphore_signal(&task->work_done);899}900901#ifdef _WIN32902pipe_semaphore_signal(&task->work_done);903#endif904905return 0;906}907908909/**910* Initialize semaphores and spawn the threads.911*/912static void913create_rast_threads(struct lp_rasterizer *rast)914{915unsigned i;916917/* NOTE: if num_threads is zero, we won't use any threads */918for (i = 0; i < rast->num_threads; i++) {919pipe_semaphore_init(&rast->tasks[i].work_ready, 0);920pipe_semaphore_init(&rast->tasks[i].work_done, 0);921rast->threads[i] = u_thread_create(thread_function,922(void *) &rast->tasks[i]);923if (!rast->threads[i]) {924rast->num_threads = i; /* previous thread is max */925break;926}927}928}929930931932/**933* Create new lp_rasterizer. If num_threads is zero, don't create any934* new threads, do rendering synchronously.935* \param num_threads number of rasterizer threads to create936*/937struct lp_rasterizer *938lp_rast_create( unsigned num_threads )939{940struct lp_rasterizer *rast;941unsigned i;942943rast = CALLOC_STRUCT(lp_rasterizer);944if (!rast) {945goto no_rast;946}947948rast->full_scenes = lp_scene_queue_create();949if (!rast->full_scenes) {950goto no_full_scenes;951}952953for (i = 0; i < MAX2(1, num_threads); i++) {954struct lp_rasterizer_task *task = &rast->tasks[i];955task->rast = rast;956task->thread_index = i;957task->thread_data.cache = align_malloc(sizeof(struct lp_build_format_cache),95816);959if (!task->thread_data.cache) {960goto no_thread_data_cache;961}962}963964rast->num_threads = num_threads;965966rast->no_rast = debug_get_bool_option("LP_NO_RAST", FALSE);967968create_rast_threads(rast);969970/* for synchronizing rasterization threads */971if (rast->num_threads > 0) {972util_barrier_init( &rast->barrier, rast->num_threads );973}974975memset(lp_dummy_tile, 0, sizeof lp_dummy_tile);976977return rast;978979no_thread_data_cache:980for (i = 0; i < MAX2(1, rast->num_threads); i++) {981if (rast->tasks[i].thread_data.cache) {982align_free(rast->tasks[i].thread_data.cache);983}984}985986lp_scene_queue_destroy(rast->full_scenes);987no_full_scenes:988FREE(rast);989no_rast:990return NULL;991}992993994/* Shutdown:995*/996void lp_rast_destroy( struct lp_rasterizer *rast )997{998unsigned i;9991000/* Set exit_flag and signal each thread's work_ready semaphore.1001* Each thread will be woken up, notice that the exit_flag is set and1002* break out of its main loop. The thread will then exit.1003*/1004rast->exit_flag = TRUE;1005for (i = 0; i < rast->num_threads; i++) {1006pipe_semaphore_signal(&rast->tasks[i].work_ready);1007}10081009/* Wait for threads to terminate before cleaning up per-thread data.1010* We don't actually call pipe_thread_wait to avoid dead lock on Windows1011* per https://bugs.freedesktop.org/show_bug.cgi?id=76252 */1012for (i = 0; i < rast->num_threads; i++) {1013#ifdef _WIN321014pipe_semaphore_wait(&rast->tasks[i].work_done);1015#else1016thrd_join(rast->threads[i], NULL);1017#endif1018}10191020/* Clean up per-thread data */1021for (i = 0; i < rast->num_threads; i++) {1022pipe_semaphore_destroy(&rast->tasks[i].work_ready);1023pipe_semaphore_destroy(&rast->tasks[i].work_done);1024}1025for (i = 0; i < MAX2(1, rast->num_threads); i++) {1026align_free(rast->tasks[i].thread_data.cache);1027}10281029/* for synchronizing rasterization threads */1030if (rast->num_threads > 0) {1031util_barrier_destroy( &rast->barrier );1032}10331034lp_scene_queue_destroy(rast->full_scenes);10351036FREE(rast);1037}10381039104010411042