Path: blob/21.2-virgl/src/gallium/drivers/etnaviv/etnaviv_blend.c
4570 views
/*1* Copyright (c) 2012-2015 Etnaviv Project2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sub license,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the11* next paragraph) shall be included in all copies or substantial portions12* of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER20* DEALINGS IN THE SOFTWARE.21*22* Authors:23* Wladimir J. van der Laan <[email protected]>24*/2526#include "etnaviv_blend.h"2728#include "etnaviv_context.h"29#include "etnaviv_screen.h"30#include "etnaviv_translate.h"31#include "hw/common.xml.h"32#include "pipe/p_defines.h"33#include "util/u_memory.h"34#include "util/half_float.h"3536void *37etna_blend_state_create(struct pipe_context *pctx,38const struct pipe_blend_state *so)39{40struct etna_context *ctx = etna_context(pctx);41const struct pipe_rt_blend_state *rt0 = &so->rt[0];42struct etna_blend_state *co = CALLOC_STRUCT(etna_blend_state);43bool alpha_enable, logicop_enable;4445/* pipe_blend_func happens to match the hardware. */46STATIC_ASSERT(PIPE_BLEND_ADD == BLEND_EQ_ADD);47STATIC_ASSERT(PIPE_BLEND_SUBTRACT == BLEND_EQ_SUBTRACT);48STATIC_ASSERT(PIPE_BLEND_REVERSE_SUBTRACT == BLEND_EQ_REVERSE_SUBTRACT);49STATIC_ASSERT(PIPE_BLEND_MIN == BLEND_EQ_MIN);50STATIC_ASSERT(PIPE_BLEND_MAX == BLEND_EQ_MAX);5152if (!co)53return NULL;5455co->base = *so;5657/* Enable blending if58* - blend enabled in blend state59* - NOT source factor is ONE and destination factor ZERO and eq is ADD for60* both rgb and alpha (which mean that blending is effectively disabled)61*/62alpha_enable = rt0->blend_enable &&63!(rt0->rgb_src_factor == PIPE_BLENDFACTOR_ONE &&64rt0->rgb_dst_factor == PIPE_BLENDFACTOR_ZERO &&65rt0->rgb_func == PIPE_BLEND_ADD &&66rt0->alpha_src_factor == PIPE_BLENDFACTOR_ONE &&67rt0->alpha_dst_factor == PIPE_BLENDFACTOR_ZERO &&68rt0->alpha_func == PIPE_BLEND_ADD);6970/* Enable separate alpha if71* - Blending enabled (see above)72* - NOT source/destination factor and eq is same for both rgb and alpha73* (which would effectively that mean alpha is not separate), and74*/75bool separate_alpha = alpha_enable &&76!(rt0->rgb_src_factor == rt0->alpha_src_factor &&77rt0->rgb_dst_factor == rt0->alpha_dst_factor &&78rt0->rgb_func == rt0->alpha_func);7980if (alpha_enable) {81co->PE_ALPHA_CONFIG =82VIVS_PE_ALPHA_CONFIG_BLEND_ENABLE_COLOR |83COND(separate_alpha, VIVS_PE_ALPHA_CONFIG_BLEND_SEPARATE_ALPHA) |84VIVS_PE_ALPHA_CONFIG_SRC_FUNC_COLOR(translate_blend_factor(rt0->rgb_src_factor)) |85VIVS_PE_ALPHA_CONFIG_SRC_FUNC_ALPHA(translate_blend_factor(rt0->alpha_src_factor)) |86VIVS_PE_ALPHA_CONFIG_DST_FUNC_COLOR(translate_blend_factor(rt0->rgb_dst_factor)) |87VIVS_PE_ALPHA_CONFIG_DST_FUNC_ALPHA(translate_blend_factor(rt0->alpha_dst_factor)) |88VIVS_PE_ALPHA_CONFIG_EQ_COLOR(rt0->rgb_func) |89VIVS_PE_ALPHA_CONFIG_EQ_ALPHA(rt0->alpha_func);90} else {91co->PE_ALPHA_CONFIG = 0;92}9394logicop_enable = so->logicop_enable &&95VIV_FEATURE(ctx->screen, chipMinorFeatures2, LOGIC_OP);9697co->PE_LOGIC_OP =98VIVS_PE_LOGIC_OP_OP(logicop_enable ? so->logicop_func : LOGIC_OP_COPY) |99VIVS_PE_LOGIC_OP_DITHER_MODE(3) | /* TODO: related to dithering, sometimes 2 */1000x000E4000 /* ??? */;101102co->fo_allowed = !alpha_enable && !logicop_enable;103104/* independent_blend_enable not needed: only one rt supported */105/* XXX alpha_to_coverage / alpha_to_one? */106/* Set dither registers based on dither status. These registers set the107* dither pattern,108* for now, set the same values as the blob.109*/110if (so->dither) {111co->PE_DITHER[0] = 0x6e4ca280;112co->PE_DITHER[1] = 0x5d7f91b3;113} else {114co->PE_DITHER[0] = 0xffffffff;115co->PE_DITHER[1] = 0xffffffff;116}117118return co;119}120121bool122etna_update_blend(struct etna_context *ctx)123{124struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;125struct pipe_blend_state *pblend = ctx->blend;126struct etna_blend_state *blend = etna_blend_state(pblend);127const struct pipe_rt_blend_state *rt0 = &pblend->rt[0];128const struct util_format_description *desc;129uint32_t colormask;130131if (pfb->cbufs[0] &&132translate_pe_format_rb_swap(pfb->cbufs[0]->format)) {133colormask = rt0->colormask & (PIPE_MASK_A | PIPE_MASK_G);134if (rt0->colormask & PIPE_MASK_R)135colormask |= PIPE_MASK_B;136if (rt0->colormask & PIPE_MASK_B)137colormask |= PIPE_MASK_R;138} else {139colormask = rt0->colormask;140}141142/* If the complete render target is written, set full_overwrite:143* - The color mask covers all channels of the render target144* - No blending or logicop is used145*/146if (pfb->cbufs[0])147desc = util_format_description(pfb->cbufs[0]->format);148bool full_overwrite = !pfb->cbufs[0] || ((blend->fo_allowed &&149util_format_colormask_full(desc, colormask)));150blend->PE_COLOR_FORMAT =151VIVS_PE_COLOR_FORMAT_COMPONENTS(colormask) |152COND(full_overwrite, VIVS_PE_COLOR_FORMAT_OVERWRITE);153154return true;155}156157void158etna_set_blend_color(struct pipe_context *pctx, const struct pipe_blend_color *bc)159{160struct etna_context *ctx = etna_context(pctx);161struct compiled_blend_color *cs = &ctx->blend_color;162163memcpy(cs->color, bc->color, sizeof(float) * 4);164165ctx->dirty |= ETNA_DIRTY_BLEND_COLOR;166}167168bool169etna_update_blend_color(struct etna_context *ctx)170{171struct pipe_framebuffer_state *pfb = &ctx->framebuffer_s;172struct compiled_blend_color *cs = &ctx->blend_color;173bool rb_swap = (pfb->cbufs[0] && translate_pe_format_rb_swap(pfb->cbufs[0]->format));174175cs->PE_ALPHA_BLEND_COLOR =176VIVS_PE_ALPHA_BLEND_COLOR_R(etna_cfloat_to_uint8(cs->color[rb_swap ? 2 : 0])) |177VIVS_PE_ALPHA_BLEND_COLOR_G(etna_cfloat_to_uint8(cs->color[1])) |178VIVS_PE_ALPHA_BLEND_COLOR_B(etna_cfloat_to_uint8(cs->color[rb_swap ? 0 : 2])) |179VIVS_PE_ALPHA_BLEND_COLOR_A(etna_cfloat_to_uint8(cs->color[3]));180181cs->PE_ALPHA_COLOR_EXT0 =182VIVS_PE_ALPHA_COLOR_EXT0_B(_mesa_float_to_half(cs->color[rb_swap ? 2 : 0])) |183VIVS_PE_ALPHA_COLOR_EXT0_G(_mesa_float_to_half(cs->color[1]));184cs->PE_ALPHA_COLOR_EXT1 =185VIVS_PE_ALPHA_COLOR_EXT1_R(_mesa_float_to_half(cs->color[rb_swap ? 0 : 2])) |186VIVS_PE_ALPHA_COLOR_EXT1_A(_mesa_float_to_half(cs->color[3]));187188return true;189}190191192