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
;
74 uint32_t stream_pitch
[PIPE_MAX_ATTRIBS
];
77 struct swr_blend_state
{
78 struct pipe_blend_state pipe
;
79 SWR_BLEND_STATE blendState
;
80 RENDER_TARGET_BLEND_COMPILE_STATE compileState
[PIPE_MAX_COLOR_BUFS
];
84 * Derived SWR API DrawState
85 * For convenience of making simple changes without re-deriving state.
87 struct swr_derived_state
{
88 SWR_RASTSTATE rastState
;
90 SWR_VIEWPORT_MATRICES vpm
;
93 void swr_update_derived(struct pipe_context
*,
94 const struct pipe_draw_info
* = nullptr);
97 * Conversion functions: Convert mesa state defines to SWR.
100 static INLINE SWR_LOGIC_OP
101 swr_convert_logic_op(const UINT op
)
104 case PIPE_LOGICOP_CLEAR
:
105 return LOGICOP_CLEAR
;
106 case PIPE_LOGICOP_NOR
:
108 case PIPE_LOGICOP_AND_INVERTED
:
109 return LOGICOP_AND_INVERTED
;
110 case PIPE_LOGICOP_COPY_INVERTED
:
111 return LOGICOP_COPY_INVERTED
;
112 case PIPE_LOGICOP_AND_REVERSE
:
113 return LOGICOP_AND_REVERSE
;
114 case PIPE_LOGICOP_INVERT
:
115 return LOGICOP_INVERT
;
116 case PIPE_LOGICOP_XOR
:
118 case PIPE_LOGICOP_NAND
:
120 case PIPE_LOGICOP_AND
:
122 case PIPE_LOGICOP_EQUIV
:
123 return LOGICOP_EQUIV
;
124 case PIPE_LOGICOP_NOOP
:
126 case PIPE_LOGICOP_OR_INVERTED
:
127 return LOGICOP_OR_INVERTED
;
128 case PIPE_LOGICOP_COPY
:
130 case PIPE_LOGICOP_OR_REVERSE
:
131 return LOGICOP_OR_REVERSE
;
132 case PIPE_LOGICOP_OR
:
134 case PIPE_LOGICOP_SET
:
137 assert(0 && "Unsupported logic op");
142 static INLINE SWR_STENCILOP
143 swr_convert_stencil_op(const UINT op
)
146 case PIPE_STENCIL_OP_KEEP
:
147 return STENCILOP_KEEP
;
148 case PIPE_STENCIL_OP_ZERO
:
149 return STENCILOP_ZERO
;
150 case PIPE_STENCIL_OP_REPLACE
:
151 return STENCILOP_REPLACE
;
152 case PIPE_STENCIL_OP_INCR
:
153 return STENCILOP_INCRSAT
;
154 case PIPE_STENCIL_OP_DECR
:
155 return STENCILOP_DECRSAT
;
156 case PIPE_STENCIL_OP_INCR_WRAP
:
157 return STENCILOP_INCR
;
158 case PIPE_STENCIL_OP_DECR_WRAP
:
159 return STENCILOP_DECR
;
160 case PIPE_STENCIL_OP_INVERT
:
161 return STENCILOP_INVERT
;
163 assert(0 && "Unsupported stencil op");
164 return STENCILOP_KEEP
;
168 static INLINE SWR_FORMAT
169 swr_convert_index_type(const UINT index_size
)
171 switch (index_size
) {
172 case sizeof(unsigned char):
174 case sizeof(unsigned short):
176 case sizeof(unsigned int):
179 assert(0 && "Unsupported index type");
185 static INLINE SWR_ZFUNCTION
186 swr_convert_depth_func(const UINT pipe_func
)
189 case PIPE_FUNC_NEVER
:
193 case PIPE_FUNC_EQUAL
:
195 case PIPE_FUNC_LEQUAL
:
197 case PIPE_FUNC_GREATER
:
199 case PIPE_FUNC_NOTEQUAL
:
201 case PIPE_FUNC_GEQUAL
:
203 case PIPE_FUNC_ALWAYS
:
206 assert(0 && "Unsupported depth func");
212 static INLINE SWR_CULLMODE
213 swr_convert_cull_mode(const UINT cull_face
)
217 return SWR_CULLMODE_NONE
;
218 case PIPE_FACE_FRONT
:
219 return SWR_CULLMODE_FRONT
;
221 return SWR_CULLMODE_BACK
;
222 case PIPE_FACE_FRONT_AND_BACK
:
223 return SWR_CULLMODE_BOTH
;
225 assert(0 && "Invalid cull mode");
226 return SWR_CULLMODE_NONE
;
230 static INLINE SWR_BLEND_OP
231 swr_convert_blend_func(const UINT blend_func
)
233 switch (blend_func
) {
236 case PIPE_BLEND_SUBTRACT
:
237 return BLENDOP_SUBTRACT
;
238 case PIPE_BLEND_REVERSE_SUBTRACT
:
239 return BLENDOP_REVSUBTRACT
;
245 assert(0 && "Invalid blend func");
250 static INLINE SWR_BLEND_FACTOR
251 swr_convert_blend_factor(const UINT blend_factor
)
253 switch (blend_factor
) {
254 case PIPE_BLENDFACTOR_ONE
:
255 return BLENDFACTOR_ONE
;
256 case PIPE_BLENDFACTOR_SRC_COLOR
:
257 return BLENDFACTOR_SRC_COLOR
;
258 case PIPE_BLENDFACTOR_SRC_ALPHA
:
259 return BLENDFACTOR_SRC_ALPHA
;
260 case PIPE_BLENDFACTOR_DST_ALPHA
:
261 return BLENDFACTOR_DST_ALPHA
;
262 case PIPE_BLENDFACTOR_DST_COLOR
:
263 return BLENDFACTOR_DST_COLOR
;
264 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
265 return BLENDFACTOR_SRC_ALPHA_SATURATE
;
266 case PIPE_BLENDFACTOR_CONST_COLOR
:
267 return BLENDFACTOR_CONST_COLOR
;
268 case PIPE_BLENDFACTOR_CONST_ALPHA
:
269 return BLENDFACTOR_CONST_ALPHA
;
270 case PIPE_BLENDFACTOR_SRC1_COLOR
:
271 return BLENDFACTOR_SRC1_COLOR
;
272 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
273 return BLENDFACTOR_SRC1_ALPHA
;
274 case PIPE_BLENDFACTOR_ZERO
:
275 return BLENDFACTOR_ZERO
;
276 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
277 return BLENDFACTOR_INV_SRC_COLOR
;
278 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
279 return BLENDFACTOR_INV_SRC_ALPHA
;
280 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
281 return BLENDFACTOR_INV_DST_ALPHA
;
282 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
283 return BLENDFACTOR_INV_DST_COLOR
;
284 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
285 return BLENDFACTOR_INV_CONST_COLOR
;
286 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
287 return BLENDFACTOR_INV_CONST_ALPHA
;
288 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
289 return BLENDFACTOR_INV_SRC1_COLOR
;
290 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
291 return BLENDFACTOR_INV_SRC1_ALPHA
;
293 assert(0 && "Invalid blend factor");
294 return BLENDFACTOR_ONE
;
298 static INLINE
enum SWR_SURFACE_TYPE
299 swr_convert_target_type(const enum pipe_texture_target target
)
303 return SURFACE_BUFFER
;
304 case PIPE_TEXTURE_1D
:
305 case PIPE_TEXTURE_1D_ARRAY
:
307 case PIPE_TEXTURE_2D
:
308 case PIPE_TEXTURE_2D_ARRAY
:
309 case PIPE_TEXTURE_RECT
:
311 case PIPE_TEXTURE_3D
:
313 case PIPE_TEXTURE_CUBE
:
314 case PIPE_TEXTURE_CUBE_ARRAY
: