Path: blob/21.2-virgl/src/gallium/drivers/llvmpipe/lp_setup.c
4570 views
/**************************************************************************1*2* Copyright 2007 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/**28* Tiling engine.29*30* Builds per-tile display lists and executes them on calls to31* lp_setup_flush().32*/3334#include <limits.h>3536#include "pipe/p_defines.h"37#include "util/u_framebuffer.h"38#include "util/u_inlines.h"39#include "util/u_memory.h"40#include "util/u_pack_color.h"41#include "util/u_viewport.h"42#include "draw/draw_pipe.h"43#include "util/os_time.h"44#include "lp_context.h"45#include "lp_memory.h"46#include "lp_scene.h"47#include "lp_texture.h"48#include "lp_debug.h"49#include "lp_fence.h"50#include "lp_query.h"51#include "lp_rast.h"52#include "lp_setup_context.h"53#include "lp_screen.h"54#include "lp_state.h"55#include "frontend/sw_winsys.h"5657#include "draw/draw_context.h"58#include "draw/draw_vbuf.h"596061static boolean set_scene_state( struct lp_setup_context *, enum setup_state,62const char *reason);63static boolean try_update_scene_state( struct lp_setup_context *setup );646566static void67lp_setup_get_empty_scene(struct lp_setup_context *setup)68{69assert(setup->scene == NULL);7071setup->scene_idx++;72setup->scene_idx %= ARRAY_SIZE(setup->scenes);7374setup->scene = setup->scenes[setup->scene_idx];7576if (setup->scene->fence) {77if (LP_DEBUG & DEBUG_SETUP)78debug_printf("%s: wait for scene %d\n",79__FUNCTION__, setup->scene->fence->id);8081lp_fence_wait(setup->scene->fence);82}8384lp_scene_begin_binning(setup->scene, &setup->fb);8586}878889static void90first_triangle( struct lp_setup_context *setup,91const float (*v0)[4],92const float (*v1)[4],93const float (*v2)[4])94{95assert(setup->state == SETUP_ACTIVE);96lp_setup_choose_triangle( setup );97setup->triangle( setup, v0, v1, v2 );98}99100static void101first_line( struct lp_setup_context *setup,102const float (*v0)[4],103const float (*v1)[4])104{105assert(setup->state == SETUP_ACTIVE);106lp_setup_choose_line( setup );107setup->line( setup, v0, v1 );108}109110static void111first_point( struct lp_setup_context *setup,112const float (*v0)[4])113{114assert(setup->state == SETUP_ACTIVE);115lp_setup_choose_point( setup );116setup->point( setup, v0 );117}118119void lp_setup_reset( struct lp_setup_context *setup )120{121unsigned i;122123LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);124125/* Reset derived state */126for (i = 0; i < ARRAY_SIZE(setup->constants); ++i) {127setup->constants[i].stored_size = 0;128setup->constants[i].stored_data = NULL;129}130131setup->fs.stored = NULL;132setup->dirty = ~0;133134/* no current bin */135setup->scene = NULL;136137/* Reset some state:138*/139memset(&setup->clear, 0, sizeof setup->clear);140141/* Have an explicit "start-binning" call and get rid of this142* pointer twiddling?143*/144setup->line = first_line;145setup->point = first_point;146setup->triangle = first_triangle;147}148149150/** Rasterize all scene's bins */151static void152lp_setup_rasterize_scene( struct lp_setup_context *setup )153{154struct lp_scene *scene = setup->scene;155struct llvmpipe_screen *screen = llvmpipe_screen(scene->pipe->screen);156157scene->num_active_queries = setup->active_binned_queries;158memcpy(scene->active_queries, setup->active_queries,159scene->num_active_queries * sizeof(scene->active_queries[0]));160161lp_scene_end_binning(scene);162163lp_fence_reference(&setup->last_fence, scene->fence);164165if (setup->last_fence)166setup->last_fence->issued = TRUE;167168mtx_lock(&screen->rast_mutex);169170/* FIXME: We enqueue the scene then wait on the rasterizer to finish.171* This means we never actually run any vertex stuff in parallel to172* rasterization (not in the same context at least) which is what the173* multiple scenes per setup is about - when we get a new empty scene174* any old one is already empty again because we waited here for175* raster tasks to be finished. Ideally, we shouldn't need to wait here176* and rely on fences elsewhere when waiting is necessary.177* Certainly, lp_scene_end_rasterization() would need to be deferred too178* and there's probably other bits why this doesn't actually work.179*/180lp_rast_queue_scene(screen->rast, scene);181lp_rast_finish(screen->rast);182mtx_unlock(&screen->rast_mutex);183184lp_scene_end_rasterization(setup->scene);185lp_setup_reset( setup );186187LP_DBG(DEBUG_SETUP, "%s done \n", __FUNCTION__);188}189190191192static boolean193begin_binning( struct lp_setup_context *setup )194{195struct lp_scene *scene = setup->scene;196boolean need_zsload = FALSE;197boolean ok;198199assert(scene);200assert(scene->fence == NULL);201202/* Always create a fence:203*/204scene->fence = lp_fence_create(MAX2(1, setup->num_threads));205if (!scene->fence)206return FALSE;207208ok = try_update_scene_state(setup);209if (!ok)210return FALSE;211212if (setup->fb.zsbuf &&213((setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&214util_format_is_depth_and_stencil(setup->fb.zsbuf->format))215need_zsload = TRUE;216217LP_DBG(DEBUG_SETUP, "%s color clear bufs: %x depth: %s\n", __FUNCTION__,218setup->clear.flags >> 2,219need_zsload ? "clear": "load");220221if (setup->clear.flags & PIPE_CLEAR_COLOR) {222unsigned cbuf;223for (cbuf = 0; cbuf < setup->fb.nr_cbufs; cbuf++) {224assert(PIPE_CLEAR_COLOR0 == 1 << 2);225if (setup->clear.flags & (1 << (2 + cbuf))) {226union lp_rast_cmd_arg clearrb_arg;227struct lp_rast_clear_rb *cc_scene =228(struct lp_rast_clear_rb *)229lp_scene_alloc(scene, sizeof(struct lp_rast_clear_rb));230231if (!cc_scene) {232return FALSE;233}234235cc_scene->cbuf = cbuf;236cc_scene->color_val = setup->clear.color_val[cbuf];237clearrb_arg.clear_rb = cc_scene;238239if (!lp_scene_bin_everywhere(scene,240LP_RAST_OP_CLEAR_COLOR,241clearrb_arg))242return FALSE;243}244}245}246247if (setup->fb.zsbuf) {248if (setup->clear.flags & PIPE_CLEAR_DEPTHSTENCIL) {249ok = lp_scene_bin_everywhere( scene,250LP_RAST_OP_CLEAR_ZSTENCIL,251lp_rast_arg_clearzs(252setup->clear.zsvalue,253setup->clear.zsmask));254if (!ok)255return FALSE;256}257}258259setup->clear.flags = 0;260setup->clear.zsmask = 0;261setup->clear.zsvalue = 0;262263scene->had_queries = !!setup->active_binned_queries;264265LP_DBG(DEBUG_SETUP, "%s done\n", __FUNCTION__);266return TRUE;267}268269270/* This basically bins and then flushes any outstanding full-screen271* clears.272*273* TODO: fast path for fullscreen clears and no triangles.274*/275static boolean276execute_clears( struct lp_setup_context *setup )277{278LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);279280return begin_binning( setup );281}282283const char *states[] = {284"FLUSHED",285"CLEARED",286"ACTIVE "287};288289290static boolean291set_scene_state( struct lp_setup_context *setup,292enum setup_state new_state,293const char *reason)294{295unsigned old_state = setup->state;296297if (old_state == new_state)298return TRUE;299300if (LP_DEBUG & DEBUG_SCENE) {301debug_printf("%s old %s new %s%s%s\n",302__FUNCTION__,303states[old_state],304states[new_state],305(new_state == SETUP_FLUSHED) ? ": " : "",306(new_state == SETUP_FLUSHED) ? reason : "");307308if (new_state == SETUP_FLUSHED && setup->scene)309lp_debug_draw_bins_by_cmd_length(setup->scene);310}311312/* wait for a free/empty scene313*/314if (old_state == SETUP_FLUSHED)315lp_setup_get_empty_scene(setup);316317switch (new_state) {318case SETUP_CLEARED:319break;320321case SETUP_ACTIVE:322if (!begin_binning( setup ))323goto fail;324break;325326case SETUP_FLUSHED:327if (old_state == SETUP_CLEARED)328if (!execute_clears( setup ))329goto fail;330331lp_setup_rasterize_scene( setup );332assert(setup->scene == NULL);333break;334335default:336assert(0 && "invalid setup state mode");337goto fail;338}339340setup->state = new_state;341return TRUE;342343fail:344if (setup->scene) {345lp_scene_end_rasterization(setup->scene);346setup->scene = NULL;347}348349setup->state = SETUP_FLUSHED;350lp_setup_reset( setup );351return FALSE;352}353354355void356lp_setup_flush( struct lp_setup_context *setup,357struct pipe_fence_handle **fence,358const char *reason)359{360set_scene_state( setup, SETUP_FLUSHED, reason );361362if (fence) {363lp_fence_reference((struct lp_fence **)fence, setup->last_fence);364if (!*fence)365*fence = (struct pipe_fence_handle *)lp_fence_create(0);366}367}368369370void371lp_setup_bind_framebuffer( struct lp_setup_context *setup,372const struct pipe_framebuffer_state *fb )373{374LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);375376/* Flush any old scene.377*/378set_scene_state( setup, SETUP_FLUSHED, __FUNCTION__ );379380/*381* Ensure the old scene is not reused.382*/383assert(!setup->scene);384385/* Set new state. This will be picked up later when we next need a386* scene.387*/388util_copy_framebuffer_state(&setup->fb, fb);389setup->framebuffer.x0 = 0;390setup->framebuffer.y0 = 0;391setup->framebuffer.x1 = fb->width-1;392setup->framebuffer.y1 = fb->height-1;393setup->dirty |= LP_SETUP_NEW_SCISSOR;394}395396397/*398* Try to clear one color buffer of the attached fb, either by binning a clear399* command or queuing up the clear for later (when binning is started).400*/401static boolean402lp_setup_try_clear_color_buffer(struct lp_setup_context *setup,403const union pipe_color_union *color,404unsigned cbuf)405{406union lp_rast_cmd_arg clearrb_arg;407union util_color uc;408enum pipe_format format = setup->fb.cbufs[cbuf]->format;409410LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);411412util_pack_color_union(format, &uc, color);413414if (setup->state == SETUP_ACTIVE) {415struct lp_scene *scene = setup->scene;416417/* Add the clear to existing scene. In the unusual case where418* both color and depth-stencil are being cleared when there's419* already been some rendering, we could discard the currently420* binned scene and start again, but I don't see that as being421* a common usage.422*/423struct lp_rast_clear_rb *cc_scene =424(struct lp_rast_clear_rb *)425lp_scene_alloc_aligned(scene, sizeof(struct lp_rast_clear_rb), 8);426427if (!cc_scene) {428return FALSE;429}430431cc_scene->cbuf = cbuf;432cc_scene->color_val = uc;433clearrb_arg.clear_rb = cc_scene;434435if (!lp_scene_bin_everywhere(scene,436LP_RAST_OP_CLEAR_COLOR,437clearrb_arg))438return FALSE;439}440else {441/* Put ourselves into the 'pre-clear' state, specifically to try442* and accumulate multiple clears to color and depth_stencil443* buffers which the app or gallium frontend might issue444* separately.445*/446set_scene_state( setup, SETUP_CLEARED, __FUNCTION__ );447448assert(PIPE_CLEAR_COLOR0 == (1 << 2));449setup->clear.flags |= 1 << (cbuf + 2);450setup->clear.color_val[cbuf] = uc;451}452453return TRUE;454}455456static boolean457lp_setup_try_clear_zs(struct lp_setup_context *setup,458double depth,459unsigned stencil,460unsigned flags)461{462uint64_t zsmask = 0;463uint64_t zsvalue = 0;464uint32_t zmask32;465uint8_t smask8;466enum pipe_format format = setup->fb.zsbuf->format;467468LP_DBG(DEBUG_SETUP, "%s state %d\n", __FUNCTION__, setup->state);469470zmask32 = (flags & PIPE_CLEAR_DEPTH) ? ~0 : 0;471smask8 = (flags & PIPE_CLEAR_STENCIL) ? ~0 : 0;472473zsvalue = util_pack64_z_stencil(format, depth, stencil);474475zsmask = util_pack64_mask_z_stencil(format, zmask32, smask8);476477zsvalue &= zsmask;478479if (format == PIPE_FORMAT_Z24X8_UNORM ||480format == PIPE_FORMAT_X8Z24_UNORM) {481/*482* Make full mask if there's "X" bits so we can do full483* clear (without rmw).484*/485uint32_t zsmask_full = 0;486zsmask_full = util_pack_mask_z_stencil(format, ~0, ~0);487zsmask |= ~zsmask_full;488}489490if (setup->state == SETUP_ACTIVE) {491struct lp_scene *scene = setup->scene;492493/* Add the clear to existing scene. In the unusual case where494* both color and depth-stencil are being cleared when there's495* already been some rendering, we could discard the currently496* binned scene and start again, but I don't see that as being497* a common usage.498*/499if (!lp_scene_bin_everywhere(scene,500LP_RAST_OP_CLEAR_ZSTENCIL,501lp_rast_arg_clearzs(zsvalue, zsmask)))502return FALSE;503}504else {505/* Put ourselves into the 'pre-clear' state, specifically to try506* and accumulate multiple clears to color and depth_stencil507* buffers which the app or gallium frontend might issue508* separately.509*/510set_scene_state( setup, SETUP_CLEARED, __FUNCTION__ );511512setup->clear.flags |= flags;513514setup->clear.zsmask |= zsmask;515setup->clear.zsvalue =516(setup->clear.zsvalue & ~zsmask) | (zsvalue & zsmask);517}518519return TRUE;520}521522void523lp_setup_clear( struct lp_setup_context *setup,524const union pipe_color_union *color,525double depth,526unsigned stencil,527unsigned flags )528{529unsigned i;530531/*532* Note any of these (max 9) clears could fail (but at most there should533* be just one failure!). This avoids doing the previous succeeded534* clears again (we still clear tiles twice if a clear command succeeded535* partially for one buffer).536*/537if (flags & PIPE_CLEAR_DEPTHSTENCIL) {538unsigned flagszs = flags & PIPE_CLEAR_DEPTHSTENCIL;539if (!lp_setup_try_clear_zs(setup, depth, stencil, flagszs)) {540lp_setup_flush(setup, NULL, __FUNCTION__);541542if (!lp_setup_try_clear_zs(setup, depth, stencil, flagszs))543assert(0);544}545}546547if (flags & PIPE_CLEAR_COLOR) {548assert(PIPE_CLEAR_COLOR0 == (1 << 2));549for (i = 0; i < setup->fb.nr_cbufs; i++) {550if ((flags & (1 << (2 + i))) && setup->fb.cbufs[i]) {551if (!lp_setup_try_clear_color_buffer(setup, color, i)) {552lp_setup_flush(setup, NULL, __FUNCTION__);553554if (!lp_setup_try_clear_color_buffer(setup, color, i))555assert(0);556}557}558}559}560}561562563564void565lp_setup_set_triangle_state( struct lp_setup_context *setup,566unsigned cull_mode,567boolean ccw_is_frontface,568boolean scissor,569boolean half_pixel_center,570boolean bottom_edge_rule,571boolean multisample)572{573LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);574575setup->ccw_is_frontface = ccw_is_frontface;576setup->cullmode = cull_mode;577setup->triangle = first_triangle;578setup->multisample = multisample;579setup->pixel_offset = half_pixel_center ? 0.5f : 0.0f;580setup->bottom_edge_rule = bottom_edge_rule;581582if (setup->scissor_test != scissor) {583setup->dirty |= LP_SETUP_NEW_SCISSOR;584setup->scissor_test = scissor;585}586}587588void589lp_setup_set_line_state( struct lp_setup_context *setup,590float line_width,591boolean line_rectangular)592{593LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);594595setup->line_width = line_width;596setup->rectangular_lines = line_rectangular;597}598599void600lp_setup_set_point_state( struct lp_setup_context *setup,601float point_size,602boolean point_size_per_vertex,603uint sprite_coord_enable,604uint sprite_coord_origin,605boolean point_quad_rasterization)606{607LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);608609setup->point_size = point_size;610setup->sprite_coord_enable = sprite_coord_enable;611setup->sprite_coord_origin = sprite_coord_origin;612setup->point_size_per_vertex = point_size_per_vertex;613setup->legacy_points = !point_quad_rasterization;614}615616void617lp_setup_set_setup_variant( struct lp_setup_context *setup,618const struct lp_setup_variant *variant)619{620LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);621622setup->setup.variant = variant;623}624625void626lp_setup_set_fs_variant( struct lp_setup_context *setup,627struct lp_fragment_shader_variant *variant)628{629LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__,630variant);631632setup->fs.current.variant = variant;633setup->dirty |= LP_SETUP_NEW_FS;634}635636void637lp_setup_set_fs_constants(struct lp_setup_context *setup,638unsigned num,639struct pipe_constant_buffer *buffers)640{641unsigned i;642643LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffers);644645assert(num <= ARRAY_SIZE(setup->constants));646647for (i = 0; i < num; ++i) {648util_copy_constant_buffer(&setup->constants[i].current, &buffers[i], false);649}650for (; i < ARRAY_SIZE(setup->constants); i++) {651util_copy_constant_buffer(&setup->constants[i].current, NULL, false);652}653setup->dirty |= LP_SETUP_NEW_CONSTANTS;654}655656void657lp_setup_set_fs_ssbos(struct lp_setup_context *setup,658unsigned num,659struct pipe_shader_buffer *buffers)660{661unsigned i;662663LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) buffers);664665assert(num <= ARRAY_SIZE(setup->ssbos));666667for (i = 0; i < num; ++i) {668util_copy_shader_buffer(&setup->ssbos[i].current, &buffers[i]);669}670for (; i < ARRAY_SIZE(setup->ssbos); i++) {671util_copy_shader_buffer(&setup->ssbos[i].current, NULL);672}673setup->dirty |= LP_SETUP_NEW_SSBOS;674}675676void677lp_setup_set_fs_images(struct lp_setup_context *setup,678unsigned num,679struct pipe_image_view *images)680{681unsigned i;682683LP_DBG(DEBUG_SETUP, "%s %p\n", __FUNCTION__, (void *) images);684685assert(num <= ARRAY_SIZE(setup->images));686687for (i = 0; i < num; ++i) {688struct pipe_image_view *image = &images[i];689util_copy_image_view(&setup->images[i].current, &images[i]);690691struct pipe_resource *res = image->resource;692struct llvmpipe_resource *lp_res = llvmpipe_resource(res);693struct lp_jit_image *jit_image;694695jit_image = &setup->fs.current.jit_context.images[i];696if (!lp_res)697continue;698if (!lp_res->dt) {699/* regular texture - setup array of mipmap level offsets */700if (llvmpipe_resource_is_texture(res)) {701jit_image->base = lp_res->tex_data;702} else703jit_image->base = lp_res->data;704705jit_image->width = res->width0;706jit_image->height = res->height0;707jit_image->depth = res->depth0;708jit_image->num_samples = res->nr_samples;709710if (llvmpipe_resource_is_texture(res)) {711uint32_t mip_offset = lp_res->mip_offsets[image->u.tex.level];712713jit_image->width = u_minify(jit_image->width, image->u.tex.level);714jit_image->height = u_minify(jit_image->height, image->u.tex.level);715716if (res->target == PIPE_TEXTURE_1D_ARRAY ||717res->target == PIPE_TEXTURE_2D_ARRAY ||718res->target == PIPE_TEXTURE_3D ||719res->target == PIPE_TEXTURE_CUBE ||720res->target == PIPE_TEXTURE_CUBE_ARRAY) {721/*722* For array textures, we don't have first_layer, instead723* adjust last_layer (stored as depth) plus the mip level offsets724* (as we have mip-first layout can't just adjust base ptr).725* XXX For mip levels, could do something similar.726*/727jit_image->depth = image->u.tex.last_layer - image->u.tex.first_layer + 1;728mip_offset += image->u.tex.first_layer * lp_res->img_stride[image->u.tex.level];729} else730jit_image->depth = u_minify(jit_image->depth, image->u.tex.level);731732jit_image->row_stride = lp_res->row_stride[image->u.tex.level];733jit_image->img_stride = lp_res->img_stride[image->u.tex.level];734jit_image->sample_stride = lp_res->sample_stride;735jit_image->base = (uint8_t *)jit_image->base + mip_offset;736}737else {738unsigned view_blocksize = util_format_get_blocksize(image->format);739jit_image->width = image->u.buf.size / view_blocksize;740jit_image->base = (uint8_t *)jit_image->base + image->u.buf.offset;741}742}743}744for (; i < ARRAY_SIZE(setup->images); i++) {745util_copy_image_view(&setup->images[i].current, NULL);746}747setup->dirty |= LP_SETUP_NEW_FS;748}749750void751lp_setup_set_alpha_ref_value( struct lp_setup_context *setup,752float alpha_ref_value )753{754LP_DBG(DEBUG_SETUP, "%s %f\n", __FUNCTION__, alpha_ref_value);755756if(setup->fs.current.jit_context.alpha_ref_value != alpha_ref_value) {757setup->fs.current.jit_context.alpha_ref_value = alpha_ref_value;758setup->dirty |= LP_SETUP_NEW_FS;759}760}761762void763lp_setup_set_stencil_ref_values( struct lp_setup_context *setup,764const ubyte refs[2] )765{766LP_DBG(DEBUG_SETUP, "%s %d %d\n", __FUNCTION__, refs[0], refs[1]);767768if (setup->fs.current.jit_context.stencil_ref_front != refs[0] ||769setup->fs.current.jit_context.stencil_ref_back != refs[1]) {770setup->fs.current.jit_context.stencil_ref_front = refs[0];771setup->fs.current.jit_context.stencil_ref_back = refs[1];772setup->dirty |= LP_SETUP_NEW_FS;773}774}775776void777lp_setup_set_blend_color( struct lp_setup_context *setup,778const struct pipe_blend_color *blend_color )779{780LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);781782assert(blend_color);783784if(memcmp(&setup->blend_color.current, blend_color, sizeof *blend_color) != 0) {785memcpy(&setup->blend_color.current, blend_color, sizeof *blend_color);786setup->dirty |= LP_SETUP_NEW_BLEND_COLOR;787}788}789790791void792lp_setup_set_scissors( struct lp_setup_context *setup,793const struct pipe_scissor_state *scissors )794{795unsigned i;796LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);797798assert(scissors);799800for (i = 0; i < PIPE_MAX_VIEWPORTS; ++i) {801setup->scissors[i].x0 = scissors[i].minx;802setup->scissors[i].x1 = scissors[i].maxx-1;803setup->scissors[i].y0 = scissors[i].miny;804setup->scissors[i].y1 = scissors[i].maxy-1;805}806setup->dirty |= LP_SETUP_NEW_SCISSOR;807}808809void810lp_setup_set_sample_mask(struct lp_setup_context *setup,811uint32_t sample_mask)812{813if (setup->fs.current.jit_context.sample_mask != sample_mask) {814setup->fs.current.jit_context.sample_mask = sample_mask;815setup->dirty |= LP_SETUP_NEW_FS;816}817}818819void820lp_setup_set_flatshade_first(struct lp_setup_context *setup,821boolean flatshade_first)822{823setup->flatshade_first = flatshade_first;824}825826void827lp_setup_set_rasterizer_discard(struct lp_setup_context *setup,828boolean rasterizer_discard)829{830if (setup->rasterizer_discard != rasterizer_discard) {831setup->rasterizer_discard = rasterizer_discard;832setup->line = first_line;833setup->point = first_point;834setup->triangle = first_triangle;835}836}837838void839lp_setup_set_vertex_info(struct lp_setup_context *setup,840struct vertex_info *vertex_info)841{842/* XXX: just silently holding onto the pointer:843*/844setup->vertex_info = vertex_info;845}846847848/**849* Called during state validation when LP_NEW_VIEWPORT is set.850*/851void852lp_setup_set_viewports(struct lp_setup_context *setup,853unsigned num_viewports,854const struct pipe_viewport_state *viewports)855{856struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);857unsigned i;858859LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);860861assert(num_viewports <= PIPE_MAX_VIEWPORTS);862assert(viewports);863864/*865* For use in lp_state_fs.c, propagate the viewport values for all viewports.866*/867for (i = 0; i < num_viewports; i++) {868float min_depth;869float max_depth;870util_viewport_zmin_zmax(&viewports[i], lp->rasterizer->clip_halfz,871&min_depth, &max_depth);872873if (setup->viewports[i].min_depth != min_depth ||874setup->viewports[i].max_depth != max_depth) {875setup->viewports[i].min_depth = min_depth;876setup->viewports[i].max_depth = max_depth;877setup->dirty |= LP_SETUP_NEW_VIEWPORTS;878}879}880}881882883/**884* Called during state validation when LP_NEW_SAMPLER_VIEW is set.885*/886void887lp_setup_set_fragment_sampler_views(struct lp_setup_context *setup,888unsigned num,889struct pipe_sampler_view **views)890{891unsigned i, max_tex_num;892893LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);894895assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);896897max_tex_num = MAX2(num, setup->fs.current_tex_num);898899for (i = 0; i < max_tex_num; i++) {900struct pipe_sampler_view *view = i < num ? views[i] : NULL;901902/* We are going to overwrite/unref the current texture further below. If903* set, make sure to unmap its resource to avoid leaking previous904* mapping. */905if (setup->fs.current_tex[i])906llvmpipe_resource_unmap(setup->fs.current_tex[i], 0, 0);907908if (view) {909struct pipe_resource *res = view->texture;910struct llvmpipe_resource *lp_tex = llvmpipe_resource(res);911struct lp_jit_texture *jit_tex;912jit_tex = &setup->fs.current.jit_context.textures[i];913914/* We're referencing the texture's internal data, so save a915* reference to it.916*/917pipe_resource_reference(&setup->fs.current_tex[i], res);918919if (!lp_tex->dt) {920/* regular texture - setup array of mipmap level offsets */921int j;922unsigned first_level = 0;923unsigned last_level = 0;924925if (llvmpipe_resource_is_texture(res)) {926first_level = view->u.tex.first_level;927last_level = view->u.tex.last_level;928assert(first_level <= last_level);929assert(last_level <= res->last_level);930jit_tex->base = lp_tex->tex_data;931}932else {933jit_tex->base = lp_tex->data;934}935936if (LP_PERF & PERF_TEX_MEM) {937/* use dummy tile memory */938jit_tex->base = lp_dummy_tile;939jit_tex->width = TILE_SIZE/8;940jit_tex->height = TILE_SIZE/8;941jit_tex->depth = 1;942jit_tex->first_level = 0;943jit_tex->last_level = 0;944jit_tex->mip_offsets[0] = 0;945jit_tex->row_stride[0] = 0;946jit_tex->img_stride[0] = 0;947jit_tex->num_samples = 0;948jit_tex->sample_stride = 0;949}950else {951jit_tex->width = res->width0;952jit_tex->height = res->height0;953jit_tex->depth = res->depth0;954jit_tex->first_level = first_level;955jit_tex->last_level = last_level;956jit_tex->num_samples = res->nr_samples;957jit_tex->sample_stride = 0;958959if (llvmpipe_resource_is_texture(res)) {960for (j = first_level; j <= last_level; j++) {961jit_tex->mip_offsets[j] = lp_tex->mip_offsets[j];962jit_tex->row_stride[j] = lp_tex->row_stride[j];963jit_tex->img_stride[j] = lp_tex->img_stride[j];964}965966jit_tex->sample_stride = lp_tex->sample_stride;967968if (res->target == PIPE_TEXTURE_1D_ARRAY ||969res->target == PIPE_TEXTURE_2D_ARRAY ||970res->target == PIPE_TEXTURE_CUBE ||971res->target == PIPE_TEXTURE_CUBE_ARRAY) {972/*973* For array textures, we don't have first_layer, instead974* adjust last_layer (stored as depth) plus the mip level offsets975* (as we have mip-first layout can't just adjust base ptr).976* XXX For mip levels, could do something similar.977*/978jit_tex->depth = view->u.tex.last_layer - view->u.tex.first_layer + 1;979for (j = first_level; j <= last_level; j++) {980jit_tex->mip_offsets[j] += view->u.tex.first_layer *981lp_tex->img_stride[j];982}983if (view->target == PIPE_TEXTURE_CUBE ||984view->target == PIPE_TEXTURE_CUBE_ARRAY) {985assert(jit_tex->depth % 6 == 0);986}987assert(view->u.tex.first_layer <= view->u.tex.last_layer);988assert(view->u.tex.last_layer < res->array_size);989}990}991else {992/*993* For buffers, we don't have "offset", instead adjust994* the size (stored as width) plus the base pointer.995*/996unsigned view_blocksize = util_format_get_blocksize(view->format);997/* probably don't really need to fill that out */998jit_tex->mip_offsets[0] = 0;999jit_tex->row_stride[0] = 0;1000jit_tex->img_stride[0] = 0;10011002/* everything specified in number of elements here. */1003jit_tex->width = view->u.buf.size / view_blocksize;1004jit_tex->base = (uint8_t *)jit_tex->base + view->u.buf.offset;1005/* XXX Unsure if we need to sanitize parameters? */1006assert(view->u.buf.offset + view->u.buf.size <= res->width0);1007}1008}1009}1010else {1011/* display target texture/surface */1012jit_tex->base = llvmpipe_resource_map(res, 0, 0, LP_TEX_USAGE_READ);1013jit_tex->row_stride[0] = lp_tex->row_stride[0];1014jit_tex->img_stride[0] = lp_tex->img_stride[0];1015jit_tex->mip_offsets[0] = 0;1016jit_tex->width = res->width0;1017jit_tex->height = res->height0;1018jit_tex->depth = res->depth0;1019jit_tex->first_level = jit_tex->last_level = 0;1020jit_tex->num_samples = res->nr_samples;1021jit_tex->sample_stride = 0;1022assert(jit_tex->base);1023}1024}1025else {1026pipe_resource_reference(&setup->fs.current_tex[i], NULL);1027}1028}1029setup->fs.current_tex_num = num;10301031setup->dirty |= LP_SETUP_NEW_FS;1032}103310341035/**1036* Called during state validation when LP_NEW_SAMPLER is set.1037*/1038void1039lp_setup_set_fragment_sampler_state(struct lp_setup_context *setup,1040unsigned num,1041struct pipe_sampler_state **samplers)1042{1043unsigned i;10441045LP_DBG(DEBUG_SETUP, "%s\n", __FUNCTION__);10461047assert(num <= PIPE_MAX_SAMPLERS);10481049for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {1050const struct pipe_sampler_state *sampler = i < num ? samplers[i] : NULL;10511052if (sampler) {1053struct lp_jit_sampler *jit_sam;1054jit_sam = &setup->fs.current.jit_context.samplers[i];10551056jit_sam->min_lod = sampler->min_lod;1057jit_sam->max_lod = sampler->max_lod;1058jit_sam->lod_bias = sampler->lod_bias;1059COPY_4V(jit_sam->border_color, sampler->border_color.f);1060}1061}10621063setup->dirty |= LP_SETUP_NEW_FS;1064}106510661067/**1068* Is the given texture referenced by any scene?1069* Note: we have to check all scenes including any scenes currently1070* being rendered and the current scene being built.1071*/1072unsigned1073lp_setup_is_resource_referenced( const struct lp_setup_context *setup,1074const struct pipe_resource *texture )1075{1076unsigned i;10771078/* check the render targets */1079for (i = 0; i < setup->fb.nr_cbufs; i++) {1080if (setup->fb.cbufs[i] && setup->fb.cbufs[i]->texture == texture)1081return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;1082}1083if (setup->fb.zsbuf && setup->fb.zsbuf->texture == texture) {1084return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;1085}10861087/* check textures referenced by the scene */1088for (i = 0; i < ARRAY_SIZE(setup->scenes); i++) {1089if (lp_scene_is_resource_referenced(setup->scenes[i], texture)) {1090return LP_REFERENCED_FOR_READ;1091}1092}10931094for (i = 0; i < ARRAY_SIZE(setup->ssbos); i++) {1095if (setup->ssbos[i].current.buffer == texture)1096return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;1097}10981099for (i = 0; i < ARRAY_SIZE(setup->images); i++) {1100if (setup->images[i].current.resource == texture)1101return LP_REFERENCED_FOR_READ | LP_REFERENCED_FOR_WRITE;1102}11031104return LP_UNREFERENCED;1105}110611071108/**1109* Called by vbuf code when we're about to draw something.1110*1111* This function stores all dirty state in the current scene's display list1112* memory, via lp_scene_alloc(). We can not pass pointers of mutable state to1113* the JIT functions, as the JIT functions will be called later on, most likely1114* on a different thread.1115*1116* When processing dirty state it is imperative that we don't refer to any1117* pointers previously allocated with lp_scene_alloc() in this function (or any1118* function) as they may belong to a scene freed since then.1119*/1120static boolean1121try_update_scene_state( struct lp_setup_context *setup )1122{1123static const float fake_const_buf[4];1124boolean new_scene = (setup->fs.stored == NULL);1125struct lp_scene *scene = setup->scene;1126unsigned i;11271128assert(scene);11291130if (setup->dirty & LP_SETUP_NEW_VIEWPORTS) {1131/*1132* Record new depth range state for changes due to viewport updates.1133*1134* TODO: Collapse the existing viewport and depth range information1135* into one structure, for access by JIT.1136*/1137struct lp_jit_viewport *stored;11381139stored = (struct lp_jit_viewport *)1140lp_scene_alloc(scene, sizeof setup->viewports);11411142if (!stored) {1143assert(!new_scene);1144return FALSE;1145}11461147memcpy(stored, setup->viewports, sizeof setup->viewports);11481149setup->fs.current.jit_context.viewports = stored;1150setup->dirty |= LP_SETUP_NEW_FS;1151}11521153if(setup->dirty & LP_SETUP_NEW_BLEND_COLOR) {1154uint8_t *stored;1155float* fstored;1156unsigned i, j;1157unsigned size;11581159/* Alloc u8_blend_color (16 x i8) and f_blend_color (4 or 8 x f32) */1160size = 4 * 16 * sizeof(uint8_t);1161size += (LP_MAX_VECTOR_LENGTH / 4) * sizeof(float);1162stored = lp_scene_alloc_aligned(scene, size, LP_MIN_VECTOR_ALIGN);11631164if (!stored) {1165assert(!new_scene);1166return FALSE;1167}11681169/* Store floating point colour */1170fstored = (float*)(stored + 4*16);1171for (i = 0; i < (LP_MAX_VECTOR_LENGTH / 4); ++i) {1172fstored[i] = setup->blend_color.current.color[i % 4];1173}11741175/* smear each blend color component across 16 ubyte elements */1176for (i = 0; i < 4; ++i) {1177uint8_t c = float_to_ubyte(setup->blend_color.current.color[i]);1178for (j = 0; j < 16; ++j)1179stored[i*16 + j] = c;1180}11811182setup->blend_color.stored = stored;1183setup->fs.current.jit_context.u8_blend_color = stored;1184setup->fs.current.jit_context.f_blend_color = fstored;1185setup->dirty |= LP_SETUP_NEW_FS;1186}11871188struct llvmpipe_context *llvmpipe = llvmpipe_context(setup->pipe);1189if (llvmpipe->dirty & LP_NEW_FS_CONSTANTS)1190lp_setup_set_fs_constants(llvmpipe->setup,1191ARRAY_SIZE(llvmpipe->constants[PIPE_SHADER_FRAGMENT]),1192llvmpipe->constants[PIPE_SHADER_FRAGMENT]);11931194if (setup->dirty & LP_SETUP_NEW_CONSTANTS) {1195for (i = 0; i < ARRAY_SIZE(setup->constants); ++i) {1196struct pipe_resource *buffer = setup->constants[i].current.buffer;1197const unsigned current_size = MIN2(setup->constants[i].current.buffer_size,1198LP_MAX_TGSI_CONST_BUFFER_SIZE);1199const ubyte *current_data = NULL;1200int num_constants;12011202STATIC_ASSERT(DATA_BLOCK_SIZE >= LP_MAX_TGSI_CONST_BUFFER_SIZE);12031204if (buffer) {1205/* resource buffer */1206current_data = (ubyte *) llvmpipe_resource_data(buffer);1207}1208else if (setup->constants[i].current.user_buffer) {1209/* user-space buffer */1210current_data = (ubyte *) setup->constants[i].current.user_buffer;1211}12121213if (current_data && current_size >= sizeof(float)) {1214current_data += setup->constants[i].current.buffer_offset;12151216/* TODO: copy only the actually used constants? */12171218if (setup->constants[i].stored_size != current_size ||1219!setup->constants[i].stored_data ||1220memcmp(setup->constants[i].stored_data,1221current_data,1222current_size) != 0) {1223void *stored;12241225stored = lp_scene_alloc(scene, current_size);1226if (!stored) {1227assert(!new_scene);1228return FALSE;1229}12301231memcpy(stored,1232current_data,1233current_size);1234setup->constants[i].stored_size = current_size;1235setup->constants[i].stored_data = stored;1236}1237setup->fs.current.jit_context.constants[i] =1238setup->constants[i].stored_data;1239}1240else {1241setup->constants[i].stored_size = 0;1242setup->constants[i].stored_data = NULL;1243setup->fs.current.jit_context.constants[i] = fake_const_buf;1244}12451246num_constants =1247DIV_ROUND_UP(setup->constants[i].stored_size, lp_get_constant_buffer_stride(scene->pipe->screen));1248setup->fs.current.jit_context.num_constants[i] = num_constants;1249setup->dirty |= LP_SETUP_NEW_FS;1250}1251}12521253if (setup->dirty & LP_SETUP_NEW_SSBOS) {1254for (i = 0; i < ARRAY_SIZE(setup->ssbos); ++i) {1255struct pipe_resource *buffer = setup->ssbos[i].current.buffer;1256const ubyte *current_data = NULL;12571258if (!buffer)1259continue;1260/* resource buffer */1261current_data = (ubyte *) llvmpipe_resource_data(buffer);1262if (current_data) {1263current_data += setup->ssbos[i].current.buffer_offset;12641265setup->fs.current.jit_context.ssbos[i] = (const uint32_t *)current_data;1266setup->fs.current.jit_context.num_ssbos[i] = setup->ssbos[i].current.buffer_size;1267} else {1268setup->fs.current.jit_context.ssbos[i] = NULL;1269setup->fs.current.jit_context.num_ssbos[i] = 0;1270}1271setup->dirty |= LP_SETUP_NEW_FS;1272}1273}1274if (setup->dirty & LP_SETUP_NEW_FS) {1275if (!setup->fs.stored ||1276memcmp(setup->fs.stored,1277&setup->fs.current,1278sizeof setup->fs.current) != 0)1279{1280struct lp_rast_state *stored;12811282/* The fs state that's been stored in the scene is different from1283* the new, current state. So allocate a new lp_rast_state object1284* and append it to the bin's setup data buffer.1285*/1286stored = (struct lp_rast_state *) lp_scene_alloc(scene, sizeof *stored);1287if (!stored) {1288assert(!new_scene);1289return FALSE;1290}12911292memcpy(&stored->jit_context,1293&setup->fs.current.jit_context,1294sizeof setup->fs.current.jit_context);1295stored->variant = setup->fs.current.variant;12961297if (!lp_scene_add_frag_shader_reference(scene,1298setup->fs.current.variant))1299return FALSE;1300setup->fs.stored = stored;13011302/* The scene now references the textures in the rasterization1303* state record. Note that now.1304*/1305for (i = 0; i < ARRAY_SIZE(setup->fs.current_tex); i++) {1306if (setup->fs.current_tex[i]) {1307if (!lp_scene_add_resource_reference(scene,1308setup->fs.current_tex[i],1309new_scene)) {1310assert(!new_scene);1311return FALSE;1312}1313}1314}1315}1316}13171318if (setup->dirty & LP_SETUP_NEW_SCISSOR) {1319unsigned i;1320for (i = 0; i < PIPE_MAX_VIEWPORTS; ++i) {1321setup->draw_regions[i] = setup->framebuffer;1322if (setup->scissor_test) {1323u_rect_possible_intersection(&setup->scissors[i],1324&setup->draw_regions[i]);1325}1326}1327}13281329setup->dirty = 0;13301331assert(setup->fs.stored);1332return TRUE;1333}13341335boolean1336lp_setup_update_state( struct lp_setup_context *setup,1337boolean update_scene )1338{1339/* Some of the 'draw' pipeline stages may have changed some driver state.1340* Make sure we've processed those state changes before anything else.1341*1342* XXX this is the only place where llvmpipe_context is used in the1343* setup code. This may get refactored/changed...1344*/1345{1346struct llvmpipe_context *lp = llvmpipe_context(setup->pipe);1347if (lp->dirty) {1348llvmpipe_update_derived(lp);1349}13501351if (lp->setup->dirty) {1352llvmpipe_update_setup(lp);1353}13541355assert(setup->setup.variant);13561357/* Will probably need to move this somewhere else, just need1358* to know about vertex shader point size attribute.1359*/1360setup->psize_slot = lp->psize_slot;1361setup->viewport_index_slot = lp->viewport_index_slot;1362setup->layer_slot = lp->layer_slot;1363setup->face_slot = lp->face_slot;13641365assert(lp->dirty == 0);13661367assert(lp->setup_variant.key.size ==1368setup->setup.variant->key.size);13691370assert(memcmp(&lp->setup_variant.key,1371&setup->setup.variant->key,1372setup->setup.variant->key.size) == 0);1373}13741375if (update_scene && setup->state != SETUP_ACTIVE) {1376if (!set_scene_state( setup, SETUP_ACTIVE, __FUNCTION__ ))1377return FALSE;1378}13791380/* Only call into update_scene_state() if we already have a1381* scene:1382*/1383if (update_scene && setup->scene) {1384assert(setup->state == SETUP_ACTIVE);13851386if (try_update_scene_state(setup))1387return TRUE;13881389/* Update failed, try to restart the scene.1390*1391* Cannot call lp_setup_flush_and_restart() directly here1392* because of potential recursion.1393*/1394if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))1395return FALSE;13961397if (!set_scene_state(setup, SETUP_ACTIVE, __FUNCTION__))1398return FALSE;13991400if (!setup->scene)1401return FALSE;14021403return try_update_scene_state(setup);1404}14051406return TRUE;1407}1408140914101411/* Only caller is lp_setup_vbuf_destroy()1412*/1413void1414lp_setup_destroy( struct lp_setup_context *setup )1415{1416uint i;14171418lp_setup_reset( setup );14191420util_unreference_framebuffer_state(&setup->fb);14211422for (i = 0; i < ARRAY_SIZE(setup->fs.current_tex); i++) {1423struct pipe_resource **res_ptr = &setup->fs.current_tex[i];1424if (*res_ptr)1425llvmpipe_resource_unmap(*res_ptr, 0, 0);1426pipe_resource_reference(res_ptr, NULL);1427}14281429for (i = 0; i < ARRAY_SIZE(setup->constants); i++) {1430pipe_resource_reference(&setup->constants[i].current.buffer, NULL);1431}14321433for (i = 0; i < ARRAY_SIZE(setup->ssbos); i++) {1434pipe_resource_reference(&setup->ssbos[i].current.buffer, NULL);1435}14361437/* free the scenes in the 'empty' queue */1438for (i = 0; i < ARRAY_SIZE(setup->scenes); i++) {1439struct lp_scene *scene = setup->scenes[i];14401441if (scene->fence)1442lp_fence_wait(scene->fence);14431444lp_scene_destroy(scene);1445}14461447lp_fence_reference(&setup->last_fence, NULL);14481449FREE( setup );1450}145114521453/**1454* Create a new primitive tiling engine. Plug it into the backend of1455* the draw module. Currently also creates a rasterizer to use with1456* it.1457*/1458struct lp_setup_context *1459lp_setup_create( struct pipe_context *pipe,1460struct draw_context *draw )1461{1462struct llvmpipe_screen *screen = llvmpipe_screen(pipe->screen);1463struct lp_setup_context *setup;1464unsigned i;14651466setup = CALLOC_STRUCT(lp_setup_context);1467if (!setup) {1468goto no_setup;1469}14701471lp_setup_init_vbuf(setup);14721473/* Used only in update_state():1474*/1475setup->pipe = pipe;147614771478setup->num_threads = screen->num_threads;1479setup->vbuf = draw_vbuf_stage(draw, &setup->base);1480if (!setup->vbuf) {1481goto no_vbuf;1482}14831484draw_set_rasterize_stage(draw, setup->vbuf);1485draw_set_render(draw, &setup->base);14861487/* create some empty scenes */1488for (i = 0; i < MAX_SCENES; i++) {1489setup->scenes[i] = lp_scene_create( pipe );1490if (!setup->scenes[i]) {1491goto no_scenes;1492}1493}14941495setup->triangle = first_triangle;1496setup->line = first_line;1497setup->point = first_point;14981499setup->dirty = ~0;15001501/* Initialize empty default fb correctly, so the rect is empty */1502setup->framebuffer.x1 = -1;1503setup->framebuffer.y1 = -1;15041505return setup;15061507no_scenes:1508for (i = 0; i < MAX_SCENES; i++) {1509if (setup->scenes[i]) {1510lp_scene_destroy(setup->scenes[i]);1511}1512}15131514setup->vbuf->destroy(setup->vbuf);1515no_vbuf:1516FREE(setup);1517no_setup:1518return NULL;1519}152015211522/**1523* Put a BeginQuery command into all bins.1524*/1525void1526lp_setup_begin_query(struct lp_setup_context *setup,1527struct llvmpipe_query *pq)1528{1529set_scene_state(setup, SETUP_ACTIVE, "begin_query");15301531if (!(pq->type == PIPE_QUERY_OCCLUSION_COUNTER ||1532pq->type == PIPE_QUERY_OCCLUSION_PREDICATE ||1533pq->type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE ||1534pq->type == PIPE_QUERY_PIPELINE_STATISTICS ||1535pq->type == PIPE_QUERY_TIME_ELAPSED))1536return;15371538/* init the query to its beginning state */1539assert(setup->active_binned_queries < LP_MAX_ACTIVE_BINNED_QUERIES);1540/* exceeding list size so just ignore the query */1541if (setup->active_binned_queries >= LP_MAX_ACTIVE_BINNED_QUERIES) {1542return;1543}1544assert(setup->active_queries[setup->active_binned_queries] == NULL);1545setup->active_queries[setup->active_binned_queries] = pq;1546setup->active_binned_queries++;15471548assert(setup->scene);1549if (setup->scene) {1550if (!lp_scene_bin_everywhere(setup->scene,1551LP_RAST_OP_BEGIN_QUERY,1552lp_rast_arg_query(pq))) {15531554if (!lp_setup_flush_and_restart(setup))1555return;15561557if (!lp_scene_bin_everywhere(setup->scene,1558LP_RAST_OP_BEGIN_QUERY,1559lp_rast_arg_query(pq))) {1560return;1561}1562}1563setup->scene->had_queries |= TRUE;1564}1565}156615671568/**1569* Put an EndQuery command into all bins.1570*/1571void1572lp_setup_end_query(struct lp_setup_context *setup, struct llvmpipe_query *pq)1573{1574set_scene_state(setup, SETUP_ACTIVE, "end_query");15751576assert(setup->scene);1577if (setup->scene) {1578/* pq->fence should be the fence of the *last* scene which1579* contributed to the query result.1580*/1581lp_fence_reference(&pq->fence, setup->scene->fence);15821583if (pq->type == PIPE_QUERY_OCCLUSION_COUNTER ||1584pq->type == PIPE_QUERY_OCCLUSION_PREDICATE ||1585pq->type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE ||1586pq->type == PIPE_QUERY_PIPELINE_STATISTICS ||1587pq->type == PIPE_QUERY_TIMESTAMP ||1588pq->type == PIPE_QUERY_TIME_ELAPSED) {1589if (pq->type == PIPE_QUERY_TIMESTAMP &&1590!(setup->scene->tiles_x | setup->scene->tiles_y)) {1591/*1592* If there's a zero width/height framebuffer, there's no bins and1593* hence no rast task is ever run. So fill in something here instead.1594*/1595pq->end[0] = os_time_get_nano();1596}15971598if (!lp_scene_bin_everywhere(setup->scene,1599LP_RAST_OP_END_QUERY,1600lp_rast_arg_query(pq))) {1601if (!lp_setup_flush_and_restart(setup))1602goto fail;16031604if (!lp_scene_bin_everywhere(setup->scene,1605LP_RAST_OP_END_QUERY,1606lp_rast_arg_query(pq))) {1607goto fail;1608}1609}1610setup->scene->had_queries |= TRUE;1611}1612}1613else {1614lp_fence_reference(&pq->fence, setup->last_fence);1615}16161617fail:1618/* Need to do this now not earlier since it still needs to be marked as1619* active when binning it would cause a flush.1620*/1621if (pq->type == PIPE_QUERY_OCCLUSION_COUNTER ||1622pq->type == PIPE_QUERY_OCCLUSION_PREDICATE ||1623pq->type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE ||1624pq->type == PIPE_QUERY_PIPELINE_STATISTICS ||1625pq->type == PIPE_QUERY_TIME_ELAPSED) {1626unsigned i;16271628/* remove from active binned query list */1629for (i = 0; i < setup->active_binned_queries; i++) {1630if (setup->active_queries[i] == pq)1631break;1632}1633assert(i < setup->active_binned_queries);1634if (i == setup->active_binned_queries)1635return;1636setup->active_binned_queries--;1637setup->active_queries[i] = setup->active_queries[setup->active_binned_queries];1638setup->active_queries[setup->active_binned_queries] = NULL;1639}1640}164116421643boolean1644lp_setup_flush_and_restart(struct lp_setup_context *setup)1645{1646if (0) debug_printf("%s\n", __FUNCTION__);16471648assert(setup->state == SETUP_ACTIVE);16491650if (!set_scene_state(setup, SETUP_FLUSHED, __FUNCTION__))1651return FALSE;16521653if (!lp_setup_update_state(setup, TRUE))1654return FALSE;16551656return TRUE;1657}16581659166016611662