Path: blob/21.2-virgl/src/gallium/drivers/i915/i915_clear.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/* Authors:28* Brian Paul29*/3031#include "util/format/u_format.h"32#include "util/u_pack_color.h"33#include "i915_batch.h"34#include "i915_context.h"35#include "i915_reg.h"36#include "i915_resource.h"37#include "i915_screen.h"38#include "i915_state.h"3940void41i915_clear_emit(struct pipe_context *pipe, unsigned buffers,42const union pipe_color_union *color, double depth,43unsigned stencil, unsigned destx, unsigned desty,44unsigned width, unsigned height)45{46struct i915_context *i915 = i915_context(pipe);47uint32_t clear_params, clear_color, clear_depth, clear_stencil,48clear_color8888, packed_z_stencil;49union util_color u_color;50float f_depth = depth;51struct i915_texture *cbuf_tex, *depth_tex;52int depth_clear_bbp, color_clear_bbp;5354cbuf_tex = depth_tex = NULL;55clear_params = 0;56depth_clear_bbp = color_clear_bbp = 0;5758if (buffers & PIPE_CLEAR_COLOR) {59struct pipe_surface *cbuf = i915->framebuffer.cbufs[0];6061clear_params |= CLEARPARAM_WRITE_COLOR;62cbuf_tex = i915_texture(cbuf->texture);6364util_pack_color(color->f, cbuf->format, &u_color);65if (util_format_get_blocksize(cbuf_tex->b.format) == 4) {66clear_color = u_color.ui[0];67color_clear_bbp = 32;68} else {69clear_color = (u_color.ui[0] & 0xffff) | (u_color.ui[0] << 16);70color_clear_bbp = 16;71}7273/* correctly swizzle clear value */74if (i915->current.fixup_swizzle)75util_pack_color(color->f, cbuf->format, &u_color);76else77util_pack_color(color->f, PIPE_FORMAT_B8G8R8A8_UNORM, &u_color);78clear_color8888 = u_color.ui[0];79} else80clear_color = clear_color8888 = 0;8182clear_depth = clear_stencil = 0;83if (buffers & PIPE_CLEAR_DEPTH) {84struct pipe_surface *zbuf = i915->framebuffer.zsbuf;8586clear_params |= CLEARPARAM_WRITE_DEPTH;87depth_tex = i915_texture(zbuf->texture);88packed_z_stencil =89util_pack_z_stencil(depth_tex->b.format, depth, stencil);9091if (util_format_get_blocksize(depth_tex->b.format) == 4) {92/* Avoid read-modify-write if there's no stencil. */93if (buffers & PIPE_CLEAR_STENCIL ||94depth_tex->b.format != PIPE_FORMAT_Z24_UNORM_S8_UINT) {95clear_params |= CLEARPARAM_WRITE_STENCIL;96clear_stencil = packed_z_stencil >> 24;97}9899clear_depth = packed_z_stencil & 0xffffff;100depth_clear_bbp = 32;101} else {102clear_depth = (packed_z_stencil & 0xffff) | (packed_z_stencil << 16);103depth_clear_bbp = 16;104}105} else if (buffers & PIPE_CLEAR_STENCIL) {106struct pipe_surface *zbuf = i915->framebuffer.zsbuf;107108clear_params |= CLEARPARAM_WRITE_STENCIL;109depth_tex = i915_texture(zbuf->texture);110assert(depth_tex->b.format == PIPE_FORMAT_Z24_UNORM_S8_UINT);111112packed_z_stencil =113util_pack_z_stencil(depth_tex->b.format, depth, stencil);114depth_clear_bbp = 32;115clear_stencil = packed_z_stencil >> 24;116}117118/* hw can't fastclear both depth and color if their bbp mismatch. */119if (color_clear_bbp && depth_clear_bbp &&120color_clear_bbp != depth_clear_bbp) {121if (i915->hardware_dirty)122i915_emit_hardware_state(i915);123124if (!BEGIN_BATCH(1 + 2 * (7 + 7))) {125FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);126127i915_emit_hardware_state(i915);128i915->vbo_flushed = 1;129130assert(BEGIN_BATCH(1 + 2 * (7 + 7)));131}132133OUT_BATCH(_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);134135OUT_BATCH(_3DSTATE_CLEAR_PARAMETERS);136OUT_BATCH(CLEARPARAM_WRITE_COLOR | CLEARPARAM_CLEAR_RECT);137/* Used for zone init prim */138OUT_BATCH(clear_color);139OUT_BATCH(clear_depth);140/* Used for clear rect prim */141OUT_BATCH(clear_color8888);142OUT_BATCH_F(f_depth);143OUT_BATCH(clear_stencil);144145OUT_BATCH(_3DPRIMITIVE | PRIM3D_CLEAR_RECT | 5);146OUT_BATCH_F(destx + width);147OUT_BATCH_F(desty + height);148OUT_BATCH_F(destx);149OUT_BATCH_F(desty + height);150OUT_BATCH_F(destx);151OUT_BATCH_F(desty);152153OUT_BATCH(_3DSTATE_CLEAR_PARAMETERS);154OUT_BATCH((clear_params & ~CLEARPARAM_WRITE_COLOR) |155CLEARPARAM_CLEAR_RECT);156/* Used for zone init prim */157OUT_BATCH(clear_color);158OUT_BATCH(clear_depth);159/* Used for clear rect prim */160OUT_BATCH(clear_color8888);161OUT_BATCH_F(f_depth);162OUT_BATCH(clear_stencil);163164OUT_BATCH(_3DPRIMITIVE | PRIM3D_CLEAR_RECT | 5);165OUT_BATCH_F(destx + width);166OUT_BATCH_F(desty + height);167OUT_BATCH_F(destx);168OUT_BATCH_F(desty + height);169OUT_BATCH_F(destx);170OUT_BATCH_F(desty);171} else {172if (i915->hardware_dirty)173i915_emit_hardware_state(i915);174175if (!BEGIN_BATCH(1 + 7 + 7)) {176FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);177178i915_emit_hardware_state(i915);179i915->vbo_flushed = 1;180181assert(BEGIN_BATCH(1 + 7 + 7));182}183184OUT_BATCH(_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);185186OUT_BATCH(_3DSTATE_CLEAR_PARAMETERS);187OUT_BATCH(clear_params | CLEARPARAM_CLEAR_RECT);188/* Used for zone init prim */189OUT_BATCH(clear_color);190OUT_BATCH(clear_depth);191/* Used for clear rect prim */192OUT_BATCH(clear_color8888);193OUT_BATCH_F(f_depth);194OUT_BATCH(clear_stencil);195196OUT_BATCH(_3DPRIMITIVE | PRIM3D_CLEAR_RECT | 5);197OUT_BATCH_F(destx + width);198OUT_BATCH_F(desty + height);199OUT_BATCH_F(destx);200OUT_BATCH_F(desty + height);201OUT_BATCH_F(destx);202OUT_BATCH_F(desty);203}204205/* Flush after clear, its expected to be a costly operation.206* This is not required, just a heuristic, but without the flush we'd need to207* clobber the SCISSOR_ENABLE dynamic state. */208FLUSH_BATCH(NULL, I915_FLUSH_ASYNC);209210i915->last_fired_vertices = i915->fired_vertices;211i915->fired_vertices = 0;212}213214/**215* Clear the given buffers to the specified values.216* No masking, no scissor (clear entire buffer).217*/218void219i915_clear_blitter(struct pipe_context *pipe, unsigned buffers,220const struct pipe_scissor_state *scissor_state,221const union pipe_color_union *color, double depth,222unsigned stencil)223{224struct pipe_framebuffer_state *framebuffer =225&i915_context(pipe)->framebuffer;226unsigned i;227228for (i = 0; i < framebuffer->nr_cbufs; i++) {229if (buffers & (PIPE_CLEAR_COLOR0 << i)) {230struct pipe_surface *ps = framebuffer->cbufs[i];231232if (ps) {233pipe->clear_render_target(pipe, ps, color, 0, 0, ps->width,234ps->height, true);235}236}237}238239if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {240struct pipe_surface *ps = framebuffer->zsbuf;241pipe->clear_depth_stencil(pipe, ps, buffers & PIPE_CLEAR_DEPTHSTENCIL,242depth, stencil, 0, 0, ps->width, ps->height,243true);244}245}246247void248i915_clear_render(struct pipe_context *pipe, unsigned buffers,249const struct pipe_scissor_state *scissor_state,250const union pipe_color_union *color, double depth,251unsigned stencil)252{253struct i915_context *i915 = i915_context(pipe);254255if (i915->dirty)256i915_update_derived(i915);257258i915_clear_emit(pipe, buffers, color, depth, stencil, 0, 0,259i915->framebuffer.width, i915->framebuffer.height);260}261262263