Path: blob/21.2-virgl/src/gallium/drivers/llvmpipe/lp_context.c
4570 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4* Copyright 2008 VMware, Inc. All rights reserved.5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the8* "Software"), to deal in the Software without restriction, including9* without limitation the rights to use, copy, modify, merge, publish,10* distribute, sub license, and/or sell copies of the Software, and to11* permit persons to whom the Software is furnished to do so, subject to12* the following conditions:13*14* The above copyright notice and this permission notice (including the15* next paragraph) shall be included in all copies or substantial portions16* of the Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS19* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF20* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.21* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR22* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,23* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE24* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.25*26**************************************************************************/2728/* Author:29* Keith Whitwell <[email protected]>30*/3132#include "draw/draw_context.h"33#include "draw/draw_vbuf.h"34#include "pipe/p_defines.h"35#include "util/u_inlines.h"36#include "util/u_math.h"37#include "util/u_memory.h"38#include "util/simple_list.h"39#include "util/u_upload_mgr.h"40#include "lp_clear.h"41#include "lp_context.h"42#include "lp_flush.h"43#include "lp_perf.h"44#include "lp_state.h"45#include "lp_surface.h"46#include "lp_query.h"47#include "lp_setup.h"48#include "lp_screen.h"4950/* This is only safe if there's just one concurrent context */51#ifdef EMBEDDED_DEVICE52#define USE_GLOBAL_LLVM_CONTEXT53#endif5455static void llvmpipe_destroy( struct pipe_context *pipe )56{57struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );58uint i;5960lp_print_counters();6162if (llvmpipe->csctx) {63lp_csctx_destroy(llvmpipe->csctx);64}65if (llvmpipe->blitter) {66util_blitter_destroy(llvmpipe->blitter);67}6869if (llvmpipe->pipe.stream_uploader)70u_upload_destroy(llvmpipe->pipe.stream_uploader);7172/* This will also destroy llvmpipe->setup:73*/74if (llvmpipe->draw)75draw_destroy( llvmpipe->draw );7677for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {78pipe_surface_reference(&llvmpipe->framebuffer.cbufs[i], NULL);79}8081pipe_surface_reference(&llvmpipe->framebuffer.zsbuf, NULL);8283for (enum pipe_shader_type s = PIPE_SHADER_VERTEX; s < PIPE_SHADER_TYPES; s++) {84for (i = 0; i < ARRAY_SIZE(llvmpipe->sampler_views[0]); i++) {85pipe_sampler_view_reference(&llvmpipe->sampler_views[s][i], NULL);86}87for (i = 0; i < LP_MAX_TGSI_SHADER_IMAGES; i++) {88pipe_resource_reference(&llvmpipe->images[s][i].resource, NULL);89}90for (i = 0; i < LP_MAX_TGSI_SHADER_BUFFERS; i++) {91pipe_resource_reference(&llvmpipe->ssbos[s][i].buffer, NULL);92}93for (i = 0; i < ARRAY_SIZE(llvmpipe->constants[s]); i++) {94pipe_resource_reference(&llvmpipe->constants[s][i].buffer, NULL);95}96}9798for (i = 0; i < llvmpipe->num_vertex_buffers; i++) {99pipe_vertex_buffer_unreference(&llvmpipe->vertex_buffer[i]);100}101102lp_delete_setup_variants(llvmpipe);103104#ifndef USE_GLOBAL_LLVM_CONTEXT105LLVMContextDispose(llvmpipe->context);106#endif107llvmpipe->context = NULL;108109align_free( llvmpipe );110}111112static void113do_flush( struct pipe_context *pipe,114struct pipe_fence_handle **fence,115unsigned flags)116{117llvmpipe_flush(pipe, fence, __FUNCTION__);118}119120121static void122llvmpipe_render_condition(struct pipe_context *pipe,123struct pipe_query *query,124bool condition,125enum pipe_render_cond_flag mode)126{127struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );128129llvmpipe->render_cond_query = query;130llvmpipe->render_cond_mode = mode;131llvmpipe->render_cond_cond = condition;132}133134static void135llvmpipe_render_condition_mem(struct pipe_context *pipe,136struct pipe_resource *buffer,137unsigned offset,138bool condition)139{140struct llvmpipe_context *llvmpipe = llvmpipe_context( pipe );141142llvmpipe->render_cond_buffer = llvmpipe_resource(buffer);143llvmpipe->render_cond_offset = offset;144llvmpipe->render_cond_cond = condition;145}146147static void148llvmpipe_texture_barrier(struct pipe_context *pipe, unsigned flags)149{150llvmpipe_flush(pipe, NULL, __FUNCTION__);151}152153static void lp_draw_disk_cache_find_shader(void *cookie,154struct lp_cached_code *cache,155unsigned char ir_sha1_cache_key[20])156{157struct llvmpipe_screen *screen = cookie;158lp_disk_cache_find_shader(screen, cache, ir_sha1_cache_key);159}160161static void lp_draw_disk_cache_insert_shader(void *cookie,162struct lp_cached_code *cache,163unsigned char ir_sha1_cache_key[20])164{165struct llvmpipe_screen *screen = cookie;166lp_disk_cache_insert_shader(screen, cache, ir_sha1_cache_key);167}168169static enum pipe_reset_status170llvmpipe_get_device_reset_status(struct pipe_context *pipe)171{172return PIPE_NO_RESET;173}174175struct pipe_context *176llvmpipe_create_context(struct pipe_screen *screen, void *priv,177unsigned flags)178{179struct llvmpipe_context *llvmpipe;180181if (!llvmpipe_screen_late_init(llvmpipe_screen(screen)))182return NULL;183184llvmpipe = align_malloc(sizeof(struct llvmpipe_context), 16);185if (!llvmpipe)186return NULL;187188memset(llvmpipe, 0, sizeof *llvmpipe);189190make_empty_list(&llvmpipe->fs_variants_list);191192make_empty_list(&llvmpipe->setup_variants_list);193194make_empty_list(&llvmpipe->cs_variants_list);195196llvmpipe->pipe.screen = screen;197llvmpipe->pipe.priv = priv;198199/* Init the pipe context methods */200llvmpipe->pipe.destroy = llvmpipe_destroy;201llvmpipe->pipe.set_framebuffer_state = llvmpipe_set_framebuffer_state;202llvmpipe->pipe.clear = llvmpipe_clear;203llvmpipe->pipe.flush = do_flush;204llvmpipe->pipe.texture_barrier = llvmpipe_texture_barrier;205206llvmpipe->pipe.render_condition = llvmpipe_render_condition;207llvmpipe->pipe.render_condition_mem = llvmpipe_render_condition_mem;208209llvmpipe->pipe.get_device_reset_status = llvmpipe_get_device_reset_status;210llvmpipe_init_blend_funcs(llvmpipe);211llvmpipe_init_clip_funcs(llvmpipe);212llvmpipe_init_draw_funcs(llvmpipe);213llvmpipe_init_compute_funcs(llvmpipe);214llvmpipe_init_sampler_funcs(llvmpipe);215llvmpipe_init_query_funcs( llvmpipe );216llvmpipe_init_vertex_funcs(llvmpipe);217llvmpipe_init_so_funcs(llvmpipe);218llvmpipe_init_fs_funcs(llvmpipe);219llvmpipe_init_vs_funcs(llvmpipe);220llvmpipe_init_gs_funcs(llvmpipe);221llvmpipe_init_tess_funcs(llvmpipe);222llvmpipe_init_rasterizer_funcs(llvmpipe);223llvmpipe_init_context_resource_funcs( &llvmpipe->pipe );224llvmpipe_init_surface_functions(llvmpipe);225226#ifdef USE_GLOBAL_LLVM_CONTEXT227llvmpipe->context = LLVMGetGlobalContext();228#else229llvmpipe->context = LLVMContextCreate();230#endif231232if (!llvmpipe->context)233goto fail;234235/*236* Create drawing context and plug our rendering stage into it.237*/238llvmpipe->draw = draw_create_with_llvm_context(&llvmpipe->pipe,239llvmpipe->context);240if (!llvmpipe->draw)241goto fail;242243draw_set_disk_cache_callbacks(llvmpipe->draw,244llvmpipe_screen(screen),245lp_draw_disk_cache_find_shader,246lp_draw_disk_cache_insert_shader);247248draw_set_constant_buffer_stride(llvmpipe->draw, lp_get_constant_buffer_stride(screen));249250/* FIXME: devise alternative to draw_texture_samplers */251252llvmpipe->setup = lp_setup_create( &llvmpipe->pipe,253llvmpipe->draw );254if (!llvmpipe->setup)255goto fail;256257llvmpipe->csctx = lp_csctx_create( &llvmpipe->pipe );258if (!llvmpipe->csctx)259goto fail;260llvmpipe->pipe.stream_uploader = u_upload_create_default(&llvmpipe->pipe);261if (!llvmpipe->pipe.stream_uploader)262goto fail;263llvmpipe->pipe.const_uploader = llvmpipe->pipe.stream_uploader;264265llvmpipe->blitter = util_blitter_create(&llvmpipe->pipe);266if (!llvmpipe->blitter) {267goto fail;268}269270/* must be done before installing Draw stages */271util_blitter_cache_all_shaders(llvmpipe->blitter);272273/* plug in AA line/point stages */274draw_install_aaline_stage(llvmpipe->draw, &llvmpipe->pipe);275draw_install_aapoint_stage(llvmpipe->draw, &llvmpipe->pipe);276draw_install_pstipple_stage(llvmpipe->draw, &llvmpipe->pipe);277278/* convert points and lines into triangles:279* (otherwise, draw points and lines natively)280*/281draw_wide_point_sprites(llvmpipe->draw, FALSE);282draw_enable_point_sprites(llvmpipe->draw, FALSE);283draw_wide_point_threshold(llvmpipe->draw, 10000.0);284draw_wide_line_threshold(llvmpipe->draw, 10000.0);285286lp_reset_counters();287288/* If llvmpipe_set_scissor_states() is never called, we still need to289* make sure that derived scissor state is computed.290* See https://bugs.freedesktop.org/show_bug.cgi?id=101709291*/292llvmpipe->dirty |= LP_NEW_SCISSOR;293294return &llvmpipe->pipe;295296fail:297llvmpipe_destroy(&llvmpipe->pipe);298return NULL;299}300301302303