1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ***************************************************************************/
27 #include "pipe/p_defines.h"
28 #include "tgsi/tgsi_scan.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "tgsi/tgsi_dump.h"
31 #include "gallivm/lp_bld_tgsi.h"
32 #include "util/u_hash.h"
34 #include "swr_tex_sample.h"
35 #include "swr_shader.h"
36 #include <unordered_map>
39 struct swr_vertex_shader
{
40 struct pipe_shader_state pipe
;
41 struct lp_tgsi_info info
;
44 SWR_STREAMOUT_STATE soState
;
45 PFN_SO_FUNC soFunc
[PIPE_PRIM_MAX
];
48 struct swr_fragment_shader
{
49 struct pipe_shader_state pipe
;
50 struct lp_tgsi_info info
;
51 uint32_t constantMask
;
52 uint32_t pointSpriteMask
;
53 std::unordered_map
<swr_jit_key
, PFN_PIXEL_KERNEL
> map
;
56 /* Vertex element state */
57 struct swr_vertex_element_state
{
58 FETCH_COMPILE_STATE fsState
;
59 PFN_FETCH_FUNC fsFunc
;
60 uint32_t stream_pitch
[PIPE_MAX_ATTRIBS
];
63 struct swr_blend_state
{
64 struct pipe_blend_state pipe
;
65 SWR_BLEND_STATE blendState
;
66 RENDER_TARGET_BLEND_COMPILE_STATE compileState
[PIPE_MAX_COLOR_BUFS
];
70 * Derived SWR API DrawState
71 * For convenience of making simple changes without re-deriving state.
73 struct swr_derived_state
{
74 SWR_RASTSTATE rastState
;
76 SWR_VIEWPORT_MATRIX vpm
;
79 void swr_update_derived(struct pipe_context
*,
80 const struct pipe_draw_info
* = nullptr);
83 * Conversion functions: Convert mesa state defines to SWR.
86 static INLINE SWR_LOGIC_OP
87 swr_convert_logic_op(const UINT op
)
90 case PIPE_LOGICOP_CLEAR
:
92 case PIPE_LOGICOP_NOR
:
94 case PIPE_LOGICOP_AND_INVERTED
:
96 case PIPE_LOGICOP_COPY_INVERTED
:
97 return LOGICOP_COPY_INVERTED
;
98 case PIPE_LOGICOP_AND_REVERSE
:
99 return LOGICOP_AND_REVERSE
;
100 case PIPE_LOGICOP_INVERT
:
101 return LOGICOP_INVERT
;
102 case PIPE_LOGICOP_XOR
:
104 case PIPE_LOGICOP_NAND
:
106 case PIPE_LOGICOP_AND
:
108 case PIPE_LOGICOP_EQUIV
:
109 return LOGICOP_EQUIV
;
110 case PIPE_LOGICOP_NOOP
:
112 case PIPE_LOGICOP_OR_INVERTED
:
113 return LOGICOP_OR_INVERTED
;
114 case PIPE_LOGICOP_COPY
:
116 case PIPE_LOGICOP_OR_REVERSE
:
117 return LOGICOP_OR_REVERSE
;
118 case PIPE_LOGICOP_OR
:
120 case PIPE_LOGICOP_SET
:
123 assert(0 && "Unsupported logic op");
128 static INLINE SWR_STENCILOP
129 swr_convert_stencil_op(const UINT op
)
132 case PIPE_STENCIL_OP_KEEP
:
133 return STENCILOP_KEEP
;
134 case PIPE_STENCIL_OP_ZERO
:
135 return STENCILOP_ZERO
;
136 case PIPE_STENCIL_OP_REPLACE
:
137 return STENCILOP_REPLACE
;
138 case PIPE_STENCIL_OP_INCR
:
139 return STENCILOP_INCRSAT
;
140 case PIPE_STENCIL_OP_DECR
:
141 return STENCILOP_DECRSAT
;
142 case PIPE_STENCIL_OP_INCR_WRAP
:
143 return STENCILOP_INCR
;
144 case PIPE_STENCIL_OP_DECR_WRAP
:
145 return STENCILOP_DECR
;
146 case PIPE_STENCIL_OP_INVERT
:
147 return STENCILOP_INVERT
;
149 assert(0 && "Unsupported stencil op");
150 return STENCILOP_KEEP
;
154 static INLINE SWR_FORMAT
155 swr_convert_index_type(const UINT index_size
)
157 switch (index_size
) {
158 case sizeof(unsigned char):
160 case sizeof(unsigned short):
162 case sizeof(unsigned int):
165 assert(0 && "Unsupported index type");
171 static INLINE SWR_ZFUNCTION
172 swr_convert_depth_func(const UINT pipe_func
)
175 case PIPE_FUNC_NEVER
:
179 case PIPE_FUNC_EQUAL
:
181 case PIPE_FUNC_LEQUAL
:
183 case PIPE_FUNC_GREATER
:
185 case PIPE_FUNC_NOTEQUAL
:
187 case PIPE_FUNC_GEQUAL
:
189 case PIPE_FUNC_ALWAYS
:
192 assert(0 && "Unsupported depth func");
198 static INLINE SWR_CULLMODE
199 swr_convert_cull_mode(const UINT cull_face
)
203 return SWR_CULLMODE_NONE
;
204 case PIPE_FACE_FRONT
:
205 return SWR_CULLMODE_FRONT
;
207 return SWR_CULLMODE_BACK
;
208 case PIPE_FACE_FRONT_AND_BACK
:
209 return SWR_CULLMODE_BOTH
;
211 assert(0 && "Invalid cull mode");
212 return SWR_CULLMODE_NONE
;
216 static INLINE SWR_BLEND_OP
217 swr_convert_blend_func(const UINT blend_func
)
219 switch (blend_func
) {
222 case PIPE_BLEND_SUBTRACT
:
223 return BLENDOP_SUBTRACT
;
224 case PIPE_BLEND_REVERSE_SUBTRACT
:
225 return BLENDOP_REVSUBTRACT
;
231 assert(0 && "Invalid blend func");
236 static INLINE SWR_BLEND_FACTOR
237 swr_convert_blend_factor(const UINT blend_factor
)
239 switch (blend_factor
) {
240 case PIPE_BLENDFACTOR_ONE
:
241 return BLENDFACTOR_ONE
;
242 case PIPE_BLENDFACTOR_SRC_COLOR
:
243 return BLENDFACTOR_SRC_COLOR
;
244 case PIPE_BLENDFACTOR_SRC_ALPHA
:
245 return BLENDFACTOR_SRC_ALPHA
;
246 case PIPE_BLENDFACTOR_DST_ALPHA
:
247 return BLENDFACTOR_DST_ALPHA
;
248 case PIPE_BLENDFACTOR_DST_COLOR
:
249 return BLENDFACTOR_DST_COLOR
;
250 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
251 return BLENDFACTOR_SRC_ALPHA_SATURATE
;
252 case PIPE_BLENDFACTOR_CONST_COLOR
:
253 return BLENDFACTOR_CONST_COLOR
;
254 case PIPE_BLENDFACTOR_CONST_ALPHA
:
255 return BLENDFACTOR_CONST_ALPHA
;
256 case PIPE_BLENDFACTOR_SRC1_COLOR
:
257 return BLENDFACTOR_SRC1_COLOR
;
258 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
259 return BLENDFACTOR_SRC1_ALPHA
;
260 case PIPE_BLENDFACTOR_ZERO
:
261 return BLENDFACTOR_ZERO
;
262 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
263 return BLENDFACTOR_INV_SRC_COLOR
;
264 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
265 return BLENDFACTOR_INV_SRC_ALPHA
;
266 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
267 return BLENDFACTOR_INV_DST_ALPHA
;
268 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
269 return BLENDFACTOR_INV_DST_COLOR
;
270 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
271 return BLENDFACTOR_INV_CONST_COLOR
;
272 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
273 return BLENDFACTOR_INV_CONST_ALPHA
;
274 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
275 return BLENDFACTOR_INV_SRC1_COLOR
;
276 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
277 return BLENDFACTOR_INV_SRC1_ALPHA
;
279 assert(0 && "Invalid blend factor");
280 return BLENDFACTOR_ONE
;
284 static INLINE
enum SWR_SURFACE_TYPE
285 swr_convert_target_type(const enum pipe_texture_target target
)
289 return SURFACE_BUFFER
;
290 case PIPE_TEXTURE_1D
:
291 case PIPE_TEXTURE_1D_ARRAY
:
293 case PIPE_TEXTURE_2D
:
294 case PIPE_TEXTURE_2D_ARRAY
:
295 case PIPE_TEXTURE_RECT
:
297 case PIPE_TEXTURE_3D
:
299 case PIPE_TEXTURE_CUBE
:
300 case PIPE_TEXTURE_CUBE_ARRAY
: