Path: blob/21.2-virgl/src/gallium/drivers/i915/i915_context.c
4570 views
/**************************************************************************1*2* Copyright 2003 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 "i915_context.h"28#include "i915_batch.h"29#include "i915_query.h"30#include "i915_resource.h"31#include "i915_screen.h"32#include "i915_state.h"33#include "i915_surface.h"3435#include "draw/draw_context.h"36#include "pipe/p_defines.h"37#include "pipe/p_screen.h"38#include "util/u_draw.h"39#include "util/u_inlines.h"40#include "util/u_memory.h"41#include "util/u_prim.h"42#include "util/u_upload_mgr.h"4344DEBUG_GET_ONCE_BOOL_OPTION(i915_no_vbuf, "I915_NO_VBUF", false)4546/*47* Draw functions48*/4950static void51i915_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info,52unsigned drawid_offset,53const struct pipe_draw_indirect_info *indirect,54const struct pipe_draw_start_count_bias *draws,55unsigned num_draws)56{57if (num_draws > 1) {58util_draw_multi(pipe, info, drawid_offset, indirect, draws, num_draws);59return;60}6162struct i915_context *i915 = i915_context(pipe);63struct draw_context *draw = i915->draw;64const void *mapped_indices = NULL;65unsigned i;6667if (!u_trim_pipe_prim(info->mode, (unsigned *)&draws[0].count))68return;6970/*71* Ack vs contants here, helps ipers a lot.72*/73i915->dirty &= ~I915_NEW_VS_CONSTANTS;7475if (i915->dirty)76i915_update_derived(i915);7778/*79* Map vertex buffers80*/81for (i = 0; i < i915->nr_vertex_buffers; i++) {82const void *buf = i915->vertex_buffers[i].is_user_buffer83? i915->vertex_buffers[i].buffer.user84: NULL;85if (!buf) {86if (!i915->vertex_buffers[i].buffer.resource)87continue;88buf = i915_buffer(i915->vertex_buffers[i].buffer.resource)->data;89}90draw_set_mapped_vertex_buffer(draw, i, buf, ~0);91}9293/*94* Map index buffer, if present95*/96if (info->index_size) {97mapped_indices = info->has_user_indices ? info->index.user : NULL;98if (!mapped_indices)99mapped_indices = i915_buffer(info->index.resource)->data;100draw_set_indexes(draw, (ubyte *)mapped_indices, info->index_size, ~0);101}102103if (i915->constants[PIPE_SHADER_VERTEX])104draw_set_mapped_constant_buffer(105draw, PIPE_SHADER_VERTEX, 0,106i915_buffer(i915->constants[PIPE_SHADER_VERTEX])->data,107(i915->current.num_user_constants[PIPE_SHADER_VERTEX] * 4 *108sizeof(float)));109else110draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0, NULL, 0);111112/*113* Do the drawing114*/115draw_vbo(i915->draw, info, drawid_offset, NULL, draws, num_draws);116117/*118* unmap vertex/index buffers119*/120for (i = 0; i < i915->nr_vertex_buffers; i++) {121draw_set_mapped_vertex_buffer(i915->draw, i, NULL, 0);122}123if (mapped_indices)124draw_set_indexes(draw, NULL, 0, 0);125126/*127* Instead of flushing on every state change, we flush once here128* when we fire the vbo.129*/130draw_flush(i915->draw);131}132133/*134* Generic context functions135*/136137static void138i915_destroy(struct pipe_context *pipe)139{140struct i915_context *i915 = i915_context(pipe);141int i;142143if (i915->blitter)144util_blitter_destroy(i915->blitter);145146draw_destroy(i915->draw);147148if (i915->base.stream_uploader)149u_upload_destroy(i915->base.stream_uploader);150151if (i915->batch)152i915->iws->batchbuffer_destroy(i915->batch);153154/* unbind framebuffer */155for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {156pipe_surface_reference(&i915->framebuffer.cbufs[i], NULL);157}158pipe_surface_reference(&i915->framebuffer.zsbuf, NULL);159160/* unbind constant buffers */161for (i = 0; i < PIPE_SHADER_TYPES; i++) {162pipe_resource_reference(&i915->constants[i], NULL);163}164165FREE(i915);166}167168struct pipe_context *169i915_create_context(struct pipe_screen *screen, void *priv, unsigned flags)170{171struct i915_context *i915;172173i915 = CALLOC_STRUCT(i915_context);174if (!i915)175return NULL;176177i915->iws = i915_screen(screen)->iws;178i915->base.screen = screen;179i915->base.priv = priv;180i915->base.stream_uploader = u_upload_create_default(&i915->base);181i915->base.const_uploader = i915->base.stream_uploader;182183i915->base.destroy = i915_destroy;184185if (i915_screen(screen)->debug.use_blitter)186i915->base.clear = i915_clear_blitter;187else188i915->base.clear = i915_clear_render;189190i915->base.draw_vbo = i915_draw_vbo;191192/* init this before draw */193slab_create(&i915->transfer_pool, sizeof(struct pipe_transfer), 16);194slab_create(&i915->texture_transfer_pool, sizeof(struct i915_transfer), 16);195196/* Batch stream debugging is a bit hacked up at the moment:197*/198i915->batch = i915->iws->batchbuffer_create(i915->iws);199200/*201* Create drawing context and plug our rendering stage into it.202*/203i915->draw = draw_create(&i915->base);204assert(i915->draw);205if (!debug_get_option_i915_no_vbuf()) {206draw_set_rasterize_stage(i915->draw, i915_draw_vbuf_stage(i915));207} else {208draw_set_rasterize_stage(i915->draw, i915_draw_render_stage(i915));209}210211i915_init_surface_functions(i915);212i915_init_state_functions(i915);213i915_init_flush_functions(i915);214i915_init_resource_functions(i915);215i915_init_query_functions(i915);216217/* Create blitter. */218i915->blitter = util_blitter_create(&i915->base);219assert(i915->blitter);220221/* must be done before installing Draw stages */222i915->no_log_program_errors = true;223util_blitter_cache_all_shaders(i915->blitter);224i915->no_log_program_errors = false;225226draw_install_aaline_stage(i915->draw, &i915->base);227draw_install_aapoint_stage(i915->draw, &i915->base);228draw_enable_point_sprites(i915->draw, true);229230i915->dirty = ~0;231i915->hardware_dirty = ~0;232i915->immediate_dirty = ~0;233i915->dynamic_dirty = ~0;234i915->static_dirty = ~0;235i915->flush_dirty = 0;236237i915->current.fixup_swizzle = ~0;238239return &i915->base;240}241242243