Path: blob/21.2-virgl/src/gallium/drivers/softpipe/sp_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_math.h"36#include "util/u_memory.h"37#include "util/u_pstipple.h"38#include "util/u_inlines.h"39#include "util/u_upload_mgr.h"40#include "tgsi/tgsi_exec.h"41#include "sp_buffer.h"42#include "sp_clear.h"43#include "sp_context.h"44#include "sp_flush.h"45#include "sp_prim_vbuf.h"46#include "sp_state.h"47#include "sp_surface.h"48#include "sp_tile_cache.h"49#include "sp_tex_tile_cache.h"50#include "sp_texture.h"51#include "sp_query.h"52#include "sp_screen.h"53#include "sp_tex_sample.h"54#include "sp_image.h"5556static void57softpipe_destroy( struct pipe_context *pipe )58{59struct softpipe_context *softpipe = softpipe_context( pipe );60uint i, sh;6162#if DO_PSTIPPLE_IN_HELPER_MODULE63if (softpipe->pstipple.sampler)64pipe->delete_sampler_state(pipe, softpipe->pstipple.sampler);6566pipe_resource_reference(&softpipe->pstipple.texture, NULL);67pipe_sampler_view_reference(&softpipe->pstipple.sampler_view, NULL);68#endif6970if (softpipe->blitter) {71util_blitter_destroy(softpipe->blitter);72}7374if (softpipe->draw)75draw_destroy( softpipe->draw );7677if (softpipe->quad.shade)78softpipe->quad.shade->destroy( softpipe->quad.shade );7980if (softpipe->quad.depth_test)81softpipe->quad.depth_test->destroy( softpipe->quad.depth_test );8283if (softpipe->quad.blend)84softpipe->quad.blend->destroy( softpipe->quad.blend );8586if (softpipe->quad.pstipple)87softpipe->quad.pstipple->destroy( softpipe->quad.pstipple );8889if (softpipe->pipe.stream_uploader)90u_upload_destroy(softpipe->pipe.stream_uploader);9192for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {93sp_destroy_tile_cache(softpipe->cbuf_cache[i]);94pipe_surface_reference(&softpipe->framebuffer.cbufs[i], NULL);95}9697sp_destroy_tile_cache(softpipe->zsbuf_cache);98pipe_surface_reference(&softpipe->framebuffer.zsbuf, NULL);99100for (sh = 0; sh < ARRAY_SIZE(softpipe->tex_cache); sh++) {101for (i = 0; i < ARRAY_SIZE(softpipe->tex_cache[0]); i++) {102sp_destroy_tex_tile_cache(softpipe->tex_cache[sh][i]);103pipe_sampler_view_reference(&softpipe->sampler_views[sh][i], NULL);104}105}106107for (sh = 0; sh < ARRAY_SIZE(softpipe->constants); sh++) {108for (i = 0; i < ARRAY_SIZE(softpipe->constants[0]); i++) {109if (softpipe->constants[sh][i]) {110pipe_resource_reference(&softpipe->constants[sh][i], NULL);111}112}113}114115for (i = 0; i < softpipe->num_vertex_buffers; i++) {116pipe_vertex_buffer_unreference(&softpipe->vertex_buffer[i]);117}118119tgsi_exec_machine_destroy(softpipe->fs_machine);120121for (i = 0; i < PIPE_SHADER_TYPES; i++) {122FREE(softpipe->tgsi.sampler[i]);123FREE(softpipe->tgsi.image[i]);124FREE(softpipe->tgsi.buffer[i]);125}126127FREE( softpipe );128}129130131/**132* if (the texture is being used as a framebuffer surface)133* return SP_REFERENCED_FOR_WRITE134* else if (the texture is a bound texture source)135* return SP_REFERENCED_FOR_READ136* else137* return SP_UNREFERENCED138*/139unsigned int140softpipe_is_resource_referenced( struct pipe_context *pipe,141struct pipe_resource *texture,142unsigned level, int layer)143{144struct softpipe_context *softpipe = softpipe_context( pipe );145unsigned i, sh;146147if (texture->target == PIPE_BUFFER)148return SP_UNREFERENCED;149150/* check if any of the bound drawing surfaces are this texture */151if (softpipe->dirty_render_cache) {152for (i = 0; i < softpipe->framebuffer.nr_cbufs; i++) {153if (softpipe->framebuffer.cbufs[i] &&154softpipe->framebuffer.cbufs[i]->texture == texture) {155return SP_REFERENCED_FOR_WRITE;156}157}158if (softpipe->framebuffer.zsbuf &&159softpipe->framebuffer.zsbuf->texture == texture) {160return SP_REFERENCED_FOR_WRITE;161}162}163164/* check if any of the tex_cache textures are this texture */165for (sh = 0; sh < ARRAY_SIZE(softpipe->tex_cache); sh++) {166for (i = 0; i < ARRAY_SIZE(softpipe->tex_cache[0]); i++) {167if (softpipe->tex_cache[sh][i] &&168softpipe->tex_cache[sh][i]->texture == texture)169return SP_REFERENCED_FOR_READ;170}171}172173return SP_UNREFERENCED;174}175176177178179static void180softpipe_render_condition(struct pipe_context *pipe,181struct pipe_query *query,182bool condition,183enum pipe_render_cond_flag mode)184{185struct softpipe_context *softpipe = softpipe_context( pipe );186187softpipe->render_cond_query = query;188softpipe->render_cond_mode = mode;189softpipe->render_cond_cond = condition;190}191192193static void194softpipe_set_debug_callback(struct pipe_context *pipe,195const struct pipe_debug_callback *cb)196{197struct softpipe_context *softpipe = softpipe_context(pipe);198199if (cb)200softpipe->debug = *cb;201else202memset(&softpipe->debug, 0, sizeof(softpipe->debug));203}204205206struct pipe_context *207softpipe_create_context(struct pipe_screen *screen,208void *priv, unsigned flags)209{210struct softpipe_screen *sp_screen = softpipe_screen(screen);211struct softpipe_context *softpipe = CALLOC_STRUCT(softpipe_context);212uint i, sh;213214util_init_math();215216for (i = 0; i < PIPE_SHADER_TYPES; i++) {217softpipe->tgsi.sampler[i] = sp_create_tgsi_sampler();218}219220for (i = 0; i < PIPE_SHADER_TYPES; i++) {221softpipe->tgsi.image[i] = sp_create_tgsi_image();222}223224for (i = 0; i < PIPE_SHADER_TYPES; i++) {225softpipe->tgsi.buffer[i] = sp_create_tgsi_buffer();226}227228softpipe->pipe.screen = screen;229softpipe->pipe.destroy = softpipe_destroy;230softpipe->pipe.priv = priv;231232/* state setters */233softpipe_init_blend_funcs(&softpipe->pipe);234softpipe_init_clip_funcs(&softpipe->pipe);235softpipe_init_query_funcs( softpipe );236softpipe_init_rasterizer_funcs(&softpipe->pipe);237softpipe_init_sampler_funcs(&softpipe->pipe);238softpipe_init_shader_funcs(&softpipe->pipe);239softpipe_init_streamout_funcs(&softpipe->pipe);240softpipe_init_texture_funcs( &softpipe->pipe );241softpipe_init_vertex_funcs(&softpipe->pipe);242softpipe_init_image_funcs(&softpipe->pipe);243244softpipe->pipe.set_framebuffer_state = softpipe_set_framebuffer_state;245softpipe->pipe.set_debug_callback = softpipe_set_debug_callback;246247softpipe->pipe.draw_vbo = softpipe_draw_vbo;248249softpipe->pipe.launch_grid = softpipe_launch_grid;250251softpipe->pipe.clear = softpipe_clear;252softpipe->pipe.flush = softpipe_flush_wrapped;253softpipe->pipe.texture_barrier = softpipe_texture_barrier;254softpipe->pipe.memory_barrier = softpipe_memory_barrier;255softpipe->pipe.render_condition = softpipe_render_condition;256257/*258* Alloc caches for accessing drawing surfaces and textures.259* Must be before quad stage setup!260*/261for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++)262softpipe->cbuf_cache[i] = sp_create_tile_cache( &softpipe->pipe );263softpipe->zsbuf_cache = sp_create_tile_cache( &softpipe->pipe );264265/* Allocate texture caches */266for (sh = 0; sh < ARRAY_SIZE(softpipe->tex_cache); sh++) {267for (i = 0; i < ARRAY_SIZE(softpipe->tex_cache[0]); i++) {268softpipe->tex_cache[sh][i] = sp_create_tex_tile_cache(&softpipe->pipe);269if (!softpipe->tex_cache[sh][i])270goto fail;271}272}273274softpipe->fs_machine = tgsi_exec_machine_create(PIPE_SHADER_FRAGMENT);275276/* setup quad rendering stages */277softpipe->quad.shade = sp_quad_shade_stage(softpipe);278softpipe->quad.depth_test = sp_quad_depth_test_stage(softpipe);279softpipe->quad.blend = sp_quad_blend_stage(softpipe);280softpipe->quad.pstipple = sp_quad_polygon_stipple_stage(softpipe);281282softpipe->pipe.stream_uploader = u_upload_create_default(&softpipe->pipe);283if (!softpipe->pipe.stream_uploader)284goto fail;285softpipe->pipe.const_uploader = softpipe->pipe.stream_uploader;286287/*288* Create drawing context and plug our rendering stage into it.289*/290if (sp_screen->use_llvm)291softpipe->draw = draw_create(&softpipe->pipe);292else293softpipe->draw = draw_create_no_llvm(&softpipe->pipe);294if (!softpipe->draw)295goto fail;296297draw_texture_sampler(softpipe->draw,298PIPE_SHADER_VERTEX,299(struct tgsi_sampler *)300softpipe->tgsi.sampler[PIPE_SHADER_VERTEX]);301302draw_texture_sampler(softpipe->draw,303PIPE_SHADER_GEOMETRY,304(struct tgsi_sampler *)305softpipe->tgsi.sampler[PIPE_SHADER_GEOMETRY]);306307draw_image(softpipe->draw,308PIPE_SHADER_VERTEX,309(struct tgsi_image *)310softpipe->tgsi.image[PIPE_SHADER_VERTEX]);311312draw_image(softpipe->draw,313PIPE_SHADER_GEOMETRY,314(struct tgsi_image *)315softpipe->tgsi.image[PIPE_SHADER_GEOMETRY]);316317draw_buffer(softpipe->draw,318PIPE_SHADER_VERTEX,319(struct tgsi_buffer *)320softpipe->tgsi.buffer[PIPE_SHADER_VERTEX]);321322draw_buffer(softpipe->draw,323PIPE_SHADER_GEOMETRY,324(struct tgsi_buffer *)325softpipe->tgsi.buffer[PIPE_SHADER_GEOMETRY]);326327softpipe->vbuf_backend = sp_create_vbuf_backend(softpipe);328if (!softpipe->vbuf_backend)329goto fail;330331softpipe->vbuf = draw_vbuf_stage(softpipe->draw, softpipe->vbuf_backend);332if (!softpipe->vbuf)333goto fail;334335draw_set_rasterize_stage(softpipe->draw, softpipe->vbuf);336draw_set_render(softpipe->draw, softpipe->vbuf_backend);337338softpipe->blitter = util_blitter_create(&softpipe->pipe);339if (!softpipe->blitter) {340goto fail;341}342343/* must be done before installing Draw stages */344util_blitter_cache_all_shaders(softpipe->blitter);345346/* plug in AA line/point stages */347draw_install_aaline_stage(softpipe->draw, &softpipe->pipe);348draw_install_aapoint_stage(softpipe->draw, &softpipe->pipe);349350/* Do polygon stipple w/ texture map + frag prog? */351#if DO_PSTIPPLE_IN_DRAW_MODULE352draw_install_pstipple_stage(softpipe->draw, &softpipe->pipe);353#endif354355draw_wide_point_sprites(softpipe->draw, TRUE);356357sp_init_surface_functions(softpipe);358359#if DO_PSTIPPLE_IN_HELPER_MODULE360/* create the polygon stipple sampler */361softpipe->pstipple.sampler = util_pstipple_create_sampler(&softpipe->pipe);362#endif363364return &softpipe->pipe;365366fail:367softpipe_destroy(&softpipe->pipe);368return NULL;369}370371372