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_init.h"
32 #include "gallivm/lp_bld_tgsi.h"
33 #include "util/crc32.h"
35 #include "swr_tex_sample.h"
36 #include "swr_shader.h"
37 #include <unordered_map>
41 struct ShaderVariant
{
42 struct gallivm_state
*gallivm
;
45 ShaderVariant(struct gallivm_state
*gs
, T code
) : gallivm(gs
), shader(code
) {}
46 ~ShaderVariant() { gallivm_destroy(gallivm
); }
49 typedef ShaderVariant
<PFN_VERTEX_FUNC
> VariantVS
;
50 typedef ShaderVariant
<PFN_PIXEL_KERNEL
> VariantFS
;
53 struct swr_vertex_shader
{
54 struct pipe_shader_state pipe
;
55 struct lp_tgsi_info info
;
56 std::unordered_map
<swr_jit_vs_key
, std::unique_ptr
<VariantVS
>> map
;
57 SWR_STREAMOUT_STATE soState
;
58 PFN_SO_FUNC soFunc
[PIPE_PRIM_MAX
] {0};
61 struct swr_fragment_shader
{
62 struct pipe_shader_state pipe
;
63 struct lp_tgsi_info info
;
64 uint32_t constantMask
;
65 uint32_t flatConstantMask
;
66 uint32_t pointSpriteMask
;
67 std::unordered_map
<swr_jit_fs_key
, std::unique_ptr
<VariantFS
>> map
;
70 /* Vertex element state */
71 struct swr_vertex_element_state
{
72 FETCH_COMPILE_STATE fsState
;
73 PFN_FETCH_FUNC fsFunc
{NULL
};
74 uint32_t stream_pitch
[PIPE_MAX_ATTRIBS
] {0};
75 uint32_t min_instance_div
[PIPE_MAX_ATTRIBS
] {0};
76 uint32_t instanced_bufs
{0};
77 std::unordered_map
<swr_jit_fetch_key
, PFN_FETCH_FUNC
> map
;
80 struct swr_blend_state
{
81 struct pipe_blend_state pipe
;
82 SWR_BLEND_STATE blendState
;
83 RENDER_TARGET_BLEND_COMPILE_STATE compileState
[PIPE_MAX_COLOR_BUFS
];
87 * Derived SWR API DrawState
88 * For convenience of making simple changes without re-deriving state.
90 struct swr_derived_state
{
91 SWR_RASTSTATE rastState
;
93 SWR_VIEWPORT_MATRICES vpm
;
96 void swr_update_derived(struct pipe_context
*,
97 const struct pipe_draw_info
* = nullptr);
100 * Conversion functions: Convert mesa state defines to SWR.
103 static INLINE SWR_LOGIC_OP
104 swr_convert_logic_op(const UINT op
)
107 case PIPE_LOGICOP_CLEAR
:
108 return LOGICOP_CLEAR
;
109 case PIPE_LOGICOP_NOR
:
111 case PIPE_LOGICOP_AND_INVERTED
:
112 return LOGICOP_AND_INVERTED
;
113 case PIPE_LOGICOP_COPY_INVERTED
:
114 return LOGICOP_COPY_INVERTED
;
115 case PIPE_LOGICOP_AND_REVERSE
:
116 return LOGICOP_AND_REVERSE
;
117 case PIPE_LOGICOP_INVERT
:
118 return LOGICOP_INVERT
;
119 case PIPE_LOGICOP_XOR
:
121 case PIPE_LOGICOP_NAND
:
123 case PIPE_LOGICOP_AND
:
125 case PIPE_LOGICOP_EQUIV
:
126 return LOGICOP_EQUIV
;
127 case PIPE_LOGICOP_NOOP
:
129 case PIPE_LOGICOP_OR_INVERTED
:
130 return LOGICOP_OR_INVERTED
;
131 case PIPE_LOGICOP_COPY
:
133 case PIPE_LOGICOP_OR_REVERSE
:
134 return LOGICOP_OR_REVERSE
;
135 case PIPE_LOGICOP_OR
:
137 case PIPE_LOGICOP_SET
:
140 assert(0 && "Unsupported logic op");
145 static INLINE SWR_STENCILOP
146 swr_convert_stencil_op(const UINT op
)
149 case PIPE_STENCIL_OP_KEEP
:
150 return STENCILOP_KEEP
;
151 case PIPE_STENCIL_OP_ZERO
:
152 return STENCILOP_ZERO
;
153 case PIPE_STENCIL_OP_REPLACE
:
154 return STENCILOP_REPLACE
;
155 case PIPE_STENCIL_OP_INCR
:
156 return STENCILOP_INCRSAT
;
157 case PIPE_STENCIL_OP_DECR
:
158 return STENCILOP_DECRSAT
;
159 case PIPE_STENCIL_OP_INCR_WRAP
:
160 return STENCILOP_INCR
;
161 case PIPE_STENCIL_OP_DECR_WRAP
:
162 return STENCILOP_DECR
;
163 case PIPE_STENCIL_OP_INVERT
:
164 return STENCILOP_INVERT
;
166 assert(0 && "Unsupported stencil op");
167 return STENCILOP_KEEP
;
171 static INLINE SWR_FORMAT
172 swr_convert_index_type(const UINT index_size
)
174 switch (index_size
) {
175 case sizeof(unsigned char):
177 case sizeof(unsigned short):
179 case sizeof(unsigned int):
182 assert(0 && "Unsupported index type");
188 static INLINE SWR_ZFUNCTION
189 swr_convert_depth_func(const UINT pipe_func
)
192 case PIPE_FUNC_NEVER
:
196 case PIPE_FUNC_EQUAL
:
198 case PIPE_FUNC_LEQUAL
:
200 case PIPE_FUNC_GREATER
:
202 case PIPE_FUNC_NOTEQUAL
:
204 case PIPE_FUNC_GEQUAL
:
206 case PIPE_FUNC_ALWAYS
:
209 assert(0 && "Unsupported depth func");
215 static INLINE SWR_CULLMODE
216 swr_convert_cull_mode(const UINT cull_face
)
220 return SWR_CULLMODE_NONE
;
221 case PIPE_FACE_FRONT
:
222 return SWR_CULLMODE_FRONT
;
224 return SWR_CULLMODE_BACK
;
225 case PIPE_FACE_FRONT_AND_BACK
:
226 return SWR_CULLMODE_BOTH
;
228 assert(0 && "Invalid cull mode");
229 return SWR_CULLMODE_NONE
;
233 static INLINE SWR_BLEND_OP
234 swr_convert_blend_func(const UINT blend_func
)
236 switch (blend_func
) {
239 case PIPE_BLEND_SUBTRACT
:
240 return BLENDOP_SUBTRACT
;
241 case PIPE_BLEND_REVERSE_SUBTRACT
:
242 return BLENDOP_REVSUBTRACT
;
248 assert(0 && "Invalid blend func");
253 static INLINE SWR_BLEND_FACTOR
254 swr_convert_blend_factor(const UINT blend_factor
)
256 switch (blend_factor
) {
257 case PIPE_BLENDFACTOR_ONE
:
258 return BLENDFACTOR_ONE
;
259 case PIPE_BLENDFACTOR_SRC_COLOR
:
260 return BLENDFACTOR_SRC_COLOR
;
261 case PIPE_BLENDFACTOR_SRC_ALPHA
:
262 return BLENDFACTOR_SRC_ALPHA
;
263 case PIPE_BLENDFACTOR_DST_ALPHA
:
264 return BLENDFACTOR_DST_ALPHA
;
265 case PIPE_BLENDFACTOR_DST_COLOR
:
266 return BLENDFACTOR_DST_COLOR
;
267 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
268 return BLENDFACTOR_SRC_ALPHA_SATURATE
;
269 case PIPE_BLENDFACTOR_CONST_COLOR
:
270 return BLENDFACTOR_CONST_COLOR
;
271 case PIPE_BLENDFACTOR_CONST_ALPHA
:
272 return BLENDFACTOR_CONST_ALPHA
;
273 case PIPE_BLENDFACTOR_SRC1_COLOR
:
274 return BLENDFACTOR_SRC1_COLOR
;
275 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
276 return BLENDFACTOR_SRC1_ALPHA
;
277 case PIPE_BLENDFACTOR_ZERO
:
278 return BLENDFACTOR_ZERO
;
279 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
280 return BLENDFACTOR_INV_SRC_COLOR
;
281 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
282 return BLENDFACTOR_INV_SRC_ALPHA
;
283 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
284 return BLENDFACTOR_INV_DST_ALPHA
;
285 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
286 return BLENDFACTOR_INV_DST_COLOR
;
287 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
288 return BLENDFACTOR_INV_CONST_COLOR
;
289 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
290 return BLENDFACTOR_INV_CONST_ALPHA
;
291 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
292 return BLENDFACTOR_INV_SRC1_COLOR
;
293 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
294 return BLENDFACTOR_INV_SRC1_ALPHA
;
296 assert(0 && "Invalid blend factor");
297 return BLENDFACTOR_ONE
;
301 static INLINE
enum SWR_SURFACE_TYPE
302 swr_convert_target_type(const enum pipe_texture_target target
)
306 return SURFACE_BUFFER
;
307 case PIPE_TEXTURE_1D
:
308 case PIPE_TEXTURE_1D_ARRAY
:
310 case PIPE_TEXTURE_2D
:
311 case PIPE_TEXTURE_2D_ARRAY
:
312 case PIPE_TEXTURE_RECT
:
314 case PIPE_TEXTURE_3D
:
316 case PIPE_TEXTURE_CUBE
:
317 case PIPE_TEXTURE_CUBE_ARRAY
: