1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipe/p_state.h"
28bf215546Sopenharmony_ci#include "util/u_blend.h"
29bf215546Sopenharmony_ci#include "util/u_memory.h"
30bf215546Sopenharmony_ci#include "util/u_string.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "fd4_blend.h"
33bf215546Sopenharmony_ci#include "fd4_context.h"
34bf215546Sopenharmony_ci#include "fd4_format.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistatic enum a3xx_rb_blend_opcode
37bf215546Sopenharmony_ciblend_func(unsigned func)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   switch (func) {
40bf215546Sopenharmony_ci   case PIPE_BLEND_ADD:
41bf215546Sopenharmony_ci      return BLEND_DST_PLUS_SRC;
42bf215546Sopenharmony_ci   case PIPE_BLEND_MIN:
43bf215546Sopenharmony_ci      return BLEND_MIN_DST_SRC;
44bf215546Sopenharmony_ci   case PIPE_BLEND_MAX:
45bf215546Sopenharmony_ci      return BLEND_MAX_DST_SRC;
46bf215546Sopenharmony_ci   case PIPE_BLEND_SUBTRACT:
47bf215546Sopenharmony_ci      return BLEND_SRC_MINUS_DST;
48bf215546Sopenharmony_ci   case PIPE_BLEND_REVERSE_SUBTRACT:
49bf215546Sopenharmony_ci      return BLEND_DST_MINUS_SRC;
50bf215546Sopenharmony_ci   default:
51bf215546Sopenharmony_ci      DBG("invalid blend func: %x", func);
52bf215546Sopenharmony_ci      return 0;
53bf215546Sopenharmony_ci   }
54bf215546Sopenharmony_ci}
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_civoid *
57bf215546Sopenharmony_cifd4_blend_state_create(struct pipe_context *pctx,
58bf215546Sopenharmony_ci                       const struct pipe_blend_state *cso)
59bf215546Sopenharmony_ci{
60bf215546Sopenharmony_ci   struct fd4_blend_stateobj *so;
61bf215546Sopenharmony_ci   enum a3xx_rop_code rop = ROP_COPY;
62bf215546Sopenharmony_ci   bool reads_dest = false;
63bf215546Sopenharmony_ci   unsigned i, mrt_blend = 0;
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   if (cso->logicop_enable) {
66bf215546Sopenharmony_ci      rop = cso->logicop_func; /* maps 1:1 */
67bf215546Sopenharmony_ci      reads_dest = util_logicop_reads_dest(cso->logicop_func);
68bf215546Sopenharmony_ci   }
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   so = CALLOC_STRUCT(fd4_blend_stateobj);
71bf215546Sopenharmony_ci   if (!so)
72bf215546Sopenharmony_ci      return NULL;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   so->base = *cso;
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   for (i = 0; i < ARRAY_SIZE(so->rb_mrt); i++) {
77bf215546Sopenharmony_ci      const struct pipe_rt_blend_state *rt;
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci      if (cso->independent_blend_enable)
80bf215546Sopenharmony_ci         rt = &cso->rt[i];
81bf215546Sopenharmony_ci      else
82bf215546Sopenharmony_ci         rt = &cso->rt[0];
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci      so->rb_mrt[i].blend_control =
85bf215546Sopenharmony_ci         A4XX_RB_MRT_BLEND_CONTROL_RGB_SRC_FACTOR(
86bf215546Sopenharmony_ci            fd_blend_factor(rt->rgb_src_factor)) |
87bf215546Sopenharmony_ci         A4XX_RB_MRT_BLEND_CONTROL_RGB_BLEND_OPCODE(blend_func(rt->rgb_func)) |
88bf215546Sopenharmony_ci         A4XX_RB_MRT_BLEND_CONTROL_RGB_DEST_FACTOR(
89bf215546Sopenharmony_ci            fd_blend_factor(rt->rgb_dst_factor)) |
90bf215546Sopenharmony_ci         A4XX_RB_MRT_BLEND_CONTROL_ALPHA_SRC_FACTOR(
91bf215546Sopenharmony_ci            fd_blend_factor(rt->alpha_src_factor)) |
92bf215546Sopenharmony_ci         A4XX_RB_MRT_BLEND_CONTROL_ALPHA_BLEND_OPCODE(
93bf215546Sopenharmony_ci            blend_func(rt->alpha_func)) |
94bf215546Sopenharmony_ci         A4XX_RB_MRT_BLEND_CONTROL_ALPHA_DEST_FACTOR(
95bf215546Sopenharmony_ci            fd_blend_factor(rt->alpha_dst_factor));
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci      so->rb_mrt[i].control =
98bf215546Sopenharmony_ci         A4XX_RB_MRT_CONTROL_ROP_CODE(rop) |
99bf215546Sopenharmony_ci         COND(cso->logicop_enable, A4XX_RB_MRT_CONTROL_ROP_ENABLE) |
100bf215546Sopenharmony_ci         A4XX_RB_MRT_CONTROL_COMPONENT_ENABLE(rt->colormask);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci      if (rt->blend_enable) {
103bf215546Sopenharmony_ci         so->rb_mrt[i].control |= A4XX_RB_MRT_CONTROL_READ_DEST_ENABLE |
104bf215546Sopenharmony_ci                                  A4XX_RB_MRT_CONTROL_BLEND |
105bf215546Sopenharmony_ci                                  A4XX_RB_MRT_CONTROL_BLEND2;
106bf215546Sopenharmony_ci         mrt_blend |= (1 << i);
107bf215546Sopenharmony_ci      }
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci      if (reads_dest) {
110bf215546Sopenharmony_ci         so->rb_mrt[i].control |= A4XX_RB_MRT_CONTROL_READ_DEST_ENABLE;
111bf215546Sopenharmony_ci         mrt_blend |= (1 << i);
112bf215546Sopenharmony_ci      }
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci      if (cso->dither)
115bf215546Sopenharmony_ci         so->rb_mrt[i].buf_info |=
116bf215546Sopenharmony_ci            A4XX_RB_MRT_BUF_INFO_DITHER_MODE(DITHER_ALWAYS);
117bf215546Sopenharmony_ci   }
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci   so->rb_fs_output =
120bf215546Sopenharmony_ci      A4XX_RB_FS_OUTPUT_ENABLE_BLEND(mrt_blend) |
121bf215546Sopenharmony_ci      COND(cso->independent_blend_enable, A4XX_RB_FS_OUTPUT_INDEPENDENT_BLEND);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   return so;
124bf215546Sopenharmony_ci}
125