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 ***************************************************************************/
24 #include <llvm/Config/llvm-config.h>
26 #if LLVM_VERSION_MAJOR < 7
27 // llvm redefines DEBUG
28 #pragma push_macro("DEBUG")
32 #include "JitManager.h"
33 #include "llvm-c/Core.h"
34 #include "llvm/Support/CBindingWrapping.h"
35 #include "llvm/IR/LegacyPassManager.h"
37 #if LLVM_VERSION_MAJOR < 7
38 #pragma pop_macro("DEBUG")
42 #include "gen_state_llvm.h"
44 #include "functionpasses/passes.h"
46 #include "tgsi/tgsi_strings.h"
47 #include "util/format/u_format.h"
48 #include "util/u_prim.h"
49 #include "gallivm/lp_bld_init.h"
50 #include "gallivm/lp_bld_flow.h"
51 #include "gallivm/lp_bld_struct.h"
52 #include "gallivm/lp_bld_tgsi.h"
53 #include "gallivm/lp_bld_const.h"
54 #include "gallivm/lp_bld_printf.h"
56 #include "swr_context.h"
57 #include "gen_surf_state_llvm.h"
58 #include "gen_swr_context_llvm.h"
59 #include "swr_resource.h"
60 #include "swr_state.h"
61 #include "swr_screen.h"
64 /////////////////////////////////////////////////////////////////////////
69 #include "util/u_debug.h"
70 #include "util/u_memory.h"
71 #include "util/u_string.h"
73 #include "gallivm/lp_bld_type.h"
76 constexpr bool verbose_shader
= true;
78 constexpr bool verbose_shader
= false;
81 using namespace SwrJit
;
85 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
);
87 bool operator==(const swr_jit_fs_key
&lhs
, const swr_jit_fs_key
&rhs
)
89 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
92 bool operator==(const swr_jit_vs_key
&lhs
, const swr_jit_vs_key
&rhs
)
94 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
97 bool operator==(const swr_jit_fetch_key
&lhs
, const swr_jit_fetch_key
&rhs
)
99 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
102 bool operator==(const swr_jit_gs_key
&lhs
, const swr_jit_gs_key
&rhs
)
104 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
107 bool operator==(const swr_jit_tcs_key
&lhs
, const swr_jit_tcs_key
&rhs
)
109 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
112 bool operator==(const swr_jit_tes_key
&lhs
, const swr_jit_tes_key
&rhs
)
114 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
119 swr_generate_sampler_key(const struct lp_tgsi_info
&info
,
120 struct swr_context
*ctx
,
121 enum pipe_shader_type shader_type
,
122 struct swr_jit_sampler_key
&key
)
124 key
.nr_samplers
= info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
126 for (unsigned i
= 0; i
< key
.nr_samplers
; i
++) {
127 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
128 lp_sampler_static_sampler_state(
129 &key
.sampler
[i
].sampler_state
,
130 ctx
->samplers
[shader_type
][i
]);
135 * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes
136 * are dx10-style? Can't really have mixed opcodes, at least not
137 * if we want to skip the holes here (without rescanning tgsi).
139 if (info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
140 key
.nr_sampler_views
=
141 info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
142 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
143 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER_VIEW
] & (1u << (i
& 31))) {
144 const struct pipe_sampler_view
*view
=
145 ctx
->sampler_views
[shader_type
][i
];
146 lp_sampler_static_texture_state(
147 &key
.sampler
[i
].texture_state
, view
);
149 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
150 const struct util_format_description
*desc
=
151 util_format_description(view
->format
);
152 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
153 !util_format_has_depth(desc
))
154 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
159 key
.nr_sampler_views
= key
.nr_samplers
;
160 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
161 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
162 const struct pipe_sampler_view
*view
=
163 ctx
->sampler_views
[shader_type
][i
];
164 lp_sampler_static_texture_state(
165 &key
.sampler
[i
].texture_state
, view
);
167 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
168 const struct util_format_description
*desc
=
169 util_format_description(view
->format
);
170 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
171 !util_format_has_depth(desc
))
172 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
180 swr_generate_fs_key(struct swr_jit_fs_key
&key
,
181 struct swr_context
*ctx
,
182 swr_fragment_shader
*swr_fs
)
184 memset(&key
, 0, sizeof(key
));
186 key
.nr_cbufs
= ctx
->framebuffer
.nr_cbufs
;
187 key
.light_twoside
= ctx
->rasterizer
->light_twoside
;
188 key
.sprite_coord_enable
= ctx
->rasterizer
->sprite_coord_enable
;
190 struct tgsi_shader_info
*pPrevShader
;
192 pPrevShader
= &ctx
->gs
->info
.base
;
194 pPrevShader
= &ctx
->tes
->info
.base
;
196 pPrevShader
= &ctx
->vs
->info
.base
;
198 memcpy(&key
.vs_output_semantic_name
,
199 &pPrevShader
->output_semantic_name
,
200 sizeof(key
.vs_output_semantic_name
));
201 memcpy(&key
.vs_output_semantic_idx
,
202 &pPrevShader
->output_semantic_index
,
203 sizeof(key
.vs_output_semantic_idx
));
205 swr_generate_sampler_key(swr_fs
->info
, ctx
, PIPE_SHADER_FRAGMENT
, key
);
207 key
.poly_stipple_enable
= ctx
->rasterizer
->poly_stipple_enable
&&
208 ctx
->poly_stipple
.prim_is_poly
;
212 swr_generate_vs_key(struct swr_jit_vs_key
&key
,
213 struct swr_context
*ctx
,
214 swr_vertex_shader
*swr_vs
)
216 memset(&key
, 0, sizeof(key
));
218 key
.clip_plane_mask
=
219 swr_vs
->info
.base
.clipdist_writemask
?
220 swr_vs
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
221 ctx
->rasterizer
->clip_plane_enable
;
223 swr_generate_sampler_key(swr_vs
->info
, ctx
, PIPE_SHADER_VERTEX
, key
);
227 swr_generate_fetch_key(struct swr_jit_fetch_key
&key
,
228 struct swr_vertex_element_state
*velems
)
230 memset(&key
, 0, sizeof(key
));
232 key
.fsState
= velems
->fsState
;
236 swr_generate_gs_key(struct swr_jit_gs_key
&key
,
237 struct swr_context
*ctx
,
238 swr_geometry_shader
*swr_gs
)
240 memset(&key
, 0, sizeof(key
));
242 struct tgsi_shader_info
*pPrevShader
= nullptr;
245 pPrevShader
= &ctx
->tes
->info
.base
;
247 pPrevShader
= &ctx
->vs
->info
.base
;
250 memcpy(&key
.vs_output_semantic_name
,
251 &pPrevShader
->output_semantic_name
,
252 sizeof(key
.vs_output_semantic_name
));
253 memcpy(&key
.vs_output_semantic_idx
,
254 &pPrevShader
->output_semantic_index
,
255 sizeof(key
.vs_output_semantic_idx
));
257 swr_generate_sampler_key(swr_gs
->info
, ctx
, PIPE_SHADER_GEOMETRY
, key
);
261 swr_generate_tcs_key(struct swr_jit_tcs_key
&key
,
262 struct swr_context
*ctx
,
263 swr_tess_control_shader
*swr_tcs
)
265 memset(&key
, 0, sizeof(key
));
267 struct tgsi_shader_info
*pPrevShader
= &ctx
->vs
->info
.base
;
269 memcpy(&key
.vs_output_semantic_name
,
270 &pPrevShader
->output_semantic_name
,
271 sizeof(key
.vs_output_semantic_name
));
272 memcpy(&key
.vs_output_semantic_idx
,
273 &pPrevShader
->output_semantic_index
,
274 sizeof(key
.vs_output_semantic_idx
));
276 key
.clip_plane_mask
=
277 swr_tcs
->info
.base
.clipdist_writemask
?
278 swr_tcs
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
279 ctx
->rasterizer
->clip_plane_enable
;
281 swr_generate_sampler_key(swr_tcs
->info
, ctx
, PIPE_SHADER_TESS_CTRL
, key
);
285 swr_generate_tes_key(struct swr_jit_tes_key
&key
,
286 struct swr_context
*ctx
,
287 swr_tess_evaluation_shader
*swr_tes
)
289 memset(&key
, 0, sizeof(key
));
291 struct tgsi_shader_info
*pPrevShader
= nullptr;
294 pPrevShader
= &ctx
->tcs
->info
.base
;
297 pPrevShader
= &ctx
->vs
->info
.base
;
300 SWR_ASSERT(pPrevShader
!= nullptr, "TES: No TCS or VS defined");
302 memcpy(&key
.prev_output_semantic_name
,
303 &pPrevShader
->output_semantic_name
,
304 sizeof(key
.prev_output_semantic_name
));
305 memcpy(&key
.prev_output_semantic_idx
,
306 &pPrevShader
->output_semantic_index
,
307 sizeof(key
.prev_output_semantic_idx
));
309 key
.clip_plane_mask
=
310 swr_tes
->info
.base
.clipdist_writemask
?
311 swr_tes
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
312 ctx
->rasterizer
->clip_plane_enable
;
314 swr_generate_sampler_key(swr_tes
->info
, ctx
, PIPE_SHADER_TESS_EVAL
, key
);
317 struct BuilderSWR
: public Builder
{
318 BuilderSWR(JitManager
*pJitMgr
, const char *pName
)
321 pJitMgr
->SetupNewModule();
322 gallivm
= gallivm_create(pName
, wrap(&JM()->mContext
));
323 pJitMgr
->mpCurrentModule
= unwrap(gallivm
->module
);
327 gallivm_free_ir(gallivm
);
330 void WriteVS(Value
*pVal
, Value
*pVsContext
, Value
*pVtxOutput
,
331 unsigned slot
, unsigned channel
);
333 struct gallivm_state
*gallivm
;
334 PFN_VERTEX_FUNC
CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
);
335 PFN_PIXEL_KERNEL
CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
);
336 PFN_GS_FUNC
CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
);
337 PFN_TCS_FUNC
CompileTCS(struct swr_context
*ctx
, swr_jit_tcs_key
&key
);
338 PFN_TES_FUNC
CompileTES(struct swr_context
*ctx
, swr_jit_tes_key
&key
);
340 // GS-specific emit functions
342 swr_gs_llvm_fetch_input(const struct lp_build_gs_iface
*gs_iface
,
343 struct lp_build_context
* bld
,
344 boolean is_vindex_indirect
,
345 LLVMValueRef vertex_index
,
346 boolean is_aindex_indirect
,
347 LLVMValueRef attrib_index
,
348 LLVMValueRef swizzle_index
);
350 swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface
*gs_base
,
351 struct lp_build_context
* bld
,
352 LLVMValueRef (*outputs
)[4],
353 LLVMValueRef emitted_vertices_vec
,
354 LLVMValueRef stream_id
);
357 swr_gs_llvm_end_primitive(const struct lp_build_gs_iface
*gs_base
,
358 struct lp_build_context
* bld
,
359 LLVMValueRef total_emitted_vertices_vec_ptr
,
360 LLVMValueRef verts_per_prim_vec
,
361 LLVMValueRef emitted_prims_vec
,
362 LLVMValueRef mask_vec
);
365 swr_gs_llvm_epilogue(const struct lp_build_gs_iface
*gs_base
,
366 LLVMValueRef total_emitted_vertices_vec
,
367 LLVMValueRef emitted_prims_vec
);
369 // TCS-specific emit functions
370 void swr_tcs_llvm_emit_prologue(struct lp_build_tgsi_soa_context
* bld
);
371 void swr_tcs_llvm_emit_epilogue(struct lp_build_tgsi_soa_context
* bld
);
374 swr_tcs_llvm_fetch_input(const struct lp_build_tcs_iface
*tcs_iface
,
375 struct lp_build_tgsi_context
* bld_base
,
376 boolean is_vindex_indirect
,
377 LLVMValueRef vertex_index
,
378 boolean is_aindex_indirect
,
379 LLVMValueRef attrib_index
,
380 LLVMValueRef swizzle_index
);
383 swr_tcs_llvm_fetch_output(const struct lp_build_tcs_iface
*tcs_iface
,
384 struct lp_build_tgsi_context
* bld_base
,
385 boolean is_vindex_indirect
,
386 LLVMValueRef vertex_index
,
387 boolean is_aindex_indirect
,
388 LLVMValueRef attrib_index
,
389 LLVMValueRef swizzle_index
,
393 swr_tcs_llvm_store_output(const struct lp_build_tcs_iface
*tcs_iface
,
394 struct lp_build_tgsi_context
* bld_base
,
396 boolean is_vindex_indirect
,
397 LLVMValueRef vertex_index
,
398 boolean is_aindex_indirect
,
399 LLVMValueRef attrib_index
,
400 LLVMValueRef swizzle_index
,
403 // Barrier implementation (available only in TCS)
405 swr_tcs_llvm_emit_barrier(const struct lp_build_tcs_iface
*tcs_iface
,
406 struct lp_build_tgsi_context
*bld_base
);
408 // TES-specific emit functions
410 swr_tes_llvm_fetch_vtx_input(const struct lp_build_tes_iface
*tes_iface
,
411 struct lp_build_tgsi_context
* bld_base
,
412 boolean is_vindex_indirect
,
413 LLVMValueRef vertex_index
,
414 boolean is_aindex_indirect
,
415 LLVMValueRef attrib_index
,
416 LLVMValueRef swizzle_index
);
419 swr_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface
*tes_iface
,
420 struct lp_build_tgsi_context
* bld_base
,
421 boolean is_aindex_indirect
,
422 LLVMValueRef attrib_index
,
423 LLVMValueRef swizzle_index
);
426 struct swr_gs_llvm_iface
{
427 struct lp_build_gs_iface base
;
428 struct tgsi_shader_info
*info
;
430 BuilderSWR
*pBuilder
;
433 SWR_GS_STATE
*pGsState
;
434 uint32_t num_outputs
;
435 uint32_t num_verts_per_prim
;
437 Value
*pVtxAttribMap
;
440 struct swr_tcs_llvm_iface
{
441 struct lp_build_tcs_iface base
;
442 struct tgsi_shader_info
*info
;
444 BuilderSWR
*pBuilder
;
447 SWR_TS_STATE
*pTsState
;
449 uint32_t output_vertices
;
451 struct lp_build_for_loop_state loop_state
;
453 Value
*pVtxAttribMap
;
454 Value
*pVtxOutputAttribMap
;
455 Value
*pPatchOutputAttribMap
;
458 struct swr_tes_llvm_iface
{
459 struct lp_build_tes_iface base
;
460 struct tgsi_shader_info
*info
;
462 BuilderSWR
*pBuilder
;
465 SWR_TS_STATE
*pTsState
;
467 uint32_t num_outputs
;
469 Value
*pVtxAttribMap
;
470 Value
*pPatchAttribMap
;
473 // trampoline functions so we can use the builder llvm construction methods
475 swr_gs_llvm_fetch_input(const struct lp_build_gs_iface
*gs_iface
,
476 struct lp_build_context
* bld
,
477 boolean is_vindex_indirect
,
478 LLVMValueRef vertex_index
,
479 boolean is_aindex_indirect
,
480 LLVMValueRef attrib_index
,
481 LLVMValueRef swizzle_index
)
483 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_iface
;
485 return iface
->pBuilder
->swr_gs_llvm_fetch_input(gs_iface
, bld
,
494 swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface
*gs_base
,
495 struct lp_build_context
* bld
,
496 LLVMValueRef (*outputs
)[4],
497 LLVMValueRef emitted_vertices_vec
,
498 LLVMValueRef stream_id
)
500 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
502 iface
->pBuilder
->swr_gs_llvm_emit_vertex(gs_base
, bld
,
504 emitted_vertices_vec
,
509 swr_gs_llvm_end_primitive(const struct lp_build_gs_iface
*gs_base
,
510 struct lp_build_context
* bld
,
511 LLVMValueRef total_emitted_vertices_vec_ptr
,
512 LLVMValueRef verts_per_prim_vec
,
513 LLVMValueRef emitted_prims_vec
,
514 LLVMValueRef mask_vec
)
516 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
518 iface
->pBuilder
->swr_gs_llvm_end_primitive(gs_base
, bld
,
519 total_emitted_vertices_vec_ptr
,
526 swr_gs_llvm_epilogue(const struct lp_build_gs_iface
*gs_base
,
527 LLVMValueRef total_emitted_vertices_vec
,
528 LLVMValueRef emitted_prims_vec
)
530 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
532 iface
->pBuilder
->swr_gs_llvm_epilogue(gs_base
,
533 total_emitted_vertices_vec
,
538 swr_tcs_llvm_fetch_input(const struct lp_build_tcs_iface
*tcs_iface
,
539 struct lp_build_context
* bld
,
540 boolean is_vindex_indirect
,
541 LLVMValueRef vertex_index
,
542 boolean is_aindex_indirect
,
543 LLVMValueRef attrib_index
,
544 LLVMValueRef swizzle_index
)
546 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)tcs_iface
;
547 struct lp_build_tgsi_context
*bld_base
= (struct lp_build_tgsi_context
*)bld
;
549 return iface
->pBuilder
->swr_tcs_llvm_fetch_input(tcs_iface
, bld_base
,
558 swr_tcs_llvm_fetch_output(const struct lp_build_tcs_iface
*tcs_iface
,
559 struct lp_build_context
* bld
,
560 boolean is_vindex_indirect
,
561 LLVMValueRef vertex_index
,
562 boolean is_aindex_indirect
,
563 LLVMValueRef attrib_index
,
564 LLVMValueRef swizzle_index
,
567 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)tcs_iface
;
568 struct lp_build_tgsi_context
*bld_base
= (struct lp_build_tgsi_context
*)bld
;
570 return iface
->pBuilder
->swr_tcs_llvm_fetch_output(tcs_iface
, bld_base
,
581 swr_tcs_llvm_emit_prologue(struct lp_build_context
* bld
)
583 lp_build_tgsi_soa_context
* bld_base
= (lp_build_tgsi_soa_context
*)bld
;
584 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)bld_base
->tcs_iface
;
585 iface
->pBuilder
->swr_tcs_llvm_emit_prologue(bld_base
);
589 swr_tcs_llvm_emit_epilogue(struct lp_build_context
* bld
)
591 lp_build_tgsi_soa_context
* bld_base
= (lp_build_tgsi_soa_context
*)bld
;
592 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)bld_base
->tcs_iface
;
593 iface
->pBuilder
->swr_tcs_llvm_emit_epilogue(bld_base
);
597 void swr_tcs_llvm_store_output(const struct lp_build_tcs_iface
*tcs_iface
,
598 struct lp_build_context
* bld
,
600 boolean is_vindex_indirect
,
601 LLVMValueRef vertex_index
,
602 boolean is_aindex_indirect
,
603 LLVMValueRef attrib_index
,
604 LLVMValueRef swizzle_index
,
607 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)tcs_iface
;
608 struct lp_build_tgsi_context
*bld_base
= (struct lp_build_tgsi_context
*)bld
;
610 iface
->pBuilder
->swr_tcs_llvm_store_output(tcs_iface
,
623 void swr_tcs_llvm_emit_barrier(struct lp_build_context
*bld
)
625 lp_build_tgsi_soa_context
* bld_base
= (lp_build_tgsi_soa_context
*)bld
;
626 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)bld_base
->tcs_iface
;
628 iface
->pBuilder
->swr_tcs_llvm_emit_barrier(bld_base
->tcs_iface
, &bld_base
->bld_base
);
633 swr_tes_llvm_fetch_vtx_input(const struct lp_build_tes_iface
*tes_iface
,
634 struct lp_build_context
* bld
,
635 boolean is_vindex_indirect
,
636 LLVMValueRef vertex_index
,
637 boolean is_aindex_indirect
,
638 LLVMValueRef attrib_index
,
639 LLVMValueRef swizzle_index
)
641 swr_tes_llvm_iface
*iface
= (swr_tes_llvm_iface
*)tes_iface
;
642 struct lp_build_tgsi_context
*bld_base
= (struct lp_build_tgsi_context
*)bld
;
644 return iface
->pBuilder
->swr_tes_llvm_fetch_vtx_input(tes_iface
, bld_base
,
653 swr_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface
*tes_iface
,
654 struct lp_build_context
* bld
,
655 boolean is_aindex_indirect
,
656 LLVMValueRef attrib_index
,
657 LLVMValueRef swizzle_index
)
659 swr_tes_llvm_iface
*iface
= (swr_tes_llvm_iface
*)tes_iface
;
660 struct lp_build_tgsi_context
*bld_base
= (struct lp_build_tgsi_context
*)bld
;
662 return iface
->pBuilder
->swr_tes_llvm_fetch_patch_input(tes_iface
, bld_base
,
669 BuilderSWR::swr_gs_llvm_fetch_input(const struct lp_build_gs_iface
*gs_iface
,
670 struct lp_build_context
* bld
,
671 boolean is_vindex_indirect
,
672 LLVMValueRef vertex_index
,
673 boolean is_aindex_indirect
,
674 LLVMValueRef attrib_index
,
675 LLVMValueRef swizzle_index
)
677 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_iface
;
678 Value
*vert_index
= unwrap(vertex_index
);
679 Value
*attr_index
= unwrap(attrib_index
);
681 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
683 if (is_vindex_indirect
|| is_aindex_indirect
) {
685 Value
*res
= unwrap(bld
->zero
);
686 struct lp_type type
= bld
->type
;
688 for (i
= 0; i
< type
.length
; i
++) {
689 Value
*vert_chan_index
= vert_index
;
690 Value
*attr_chan_index
= attr_index
;
692 if (is_vindex_indirect
) {
693 vert_chan_index
= VEXTRACT(vert_index
, C(i
));
695 if (is_aindex_indirect
) {
696 attr_chan_index
= VEXTRACT(attr_index
, C(i
));
700 LOAD(GEP(iface
->pVtxAttribMap
, {C(0), attr_chan_index
}));
702 Value
*pVertex
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pVerts
});
703 Value
*pInputVertStride
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_inputVertStride
});
705 Value
*pVector
= ADD(MUL(vert_chan_index
, pInputVertStride
), attrib
);
706 Value
*pInput
= LOAD(GEP(pVertex
, {pVector
, unwrap(swizzle_index
)}));
708 Value
*value
= VEXTRACT(pInput
, C(i
));
709 res
= VINSERT(res
, value
, C(i
));
714 Value
*attrib
= LOAD(GEP(iface
->pVtxAttribMap
, {C(0), attr_index
}));
716 Value
*pVertex
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pVerts
});
717 Value
*pInputVertStride
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_inputVertStride
});
719 Value
*pVector
= ADD(MUL(vert_index
, pInputVertStride
), attrib
);
721 Value
*pInput
= LOAD(GEP(pVertex
, {pVector
, unwrap(swizzle_index
)}));
727 // GS output stream layout
728 #define VERTEX_COUNT_SIZE 32
729 #define CONTROL_HEADER_SIZE (8*32)
732 BuilderSWR::swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface
*gs_base
,
733 struct lp_build_context
* bld
,
734 LLVMValueRef (*outputs
)[4],
735 LLVMValueRef emitted_vertices_vec
,
736 LLVMValueRef stream_id
)
738 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
740 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
741 const uint32_t headerSize
= VERTEX_COUNT_SIZE
+ CONTROL_HEADER_SIZE
;
742 const uint32_t attribSize
= 4 * sizeof(float);
743 const uint32_t vertSize
= attribSize
* SWR_VTX_NUM_SLOTS
;
744 Value
*pVertexOffset
= MUL(unwrap(emitted_vertices_vec
), VIMMED1(vertSize
));
746 Value
*vMask
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_mask
});
747 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, mVWidth
));
749 Value
*pStack
= STACKSAVE();
750 Value
*pTmpPtr
= ALLOCA(mFP32Ty
, C(4)); // used for dummy write for lane masking
752 for (uint32_t attrib
= 0; attrib
< iface
->num_outputs
; ++attrib
) {
753 uint32_t attribSlot
= attrib
;
754 uint32_t sgvChannel
= 0;
755 if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
756 attribSlot
= VERTEX_SGV_SLOT
;
757 sgvChannel
= VERTEX_SGV_POINT_SIZE_COMP
;
758 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_LAYER
) {
759 attribSlot
= VERTEX_SGV_SLOT
;
760 sgvChannel
= VERTEX_SGV_RTAI_COMP
;
761 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_VIEWPORT_INDEX
) {
762 attribSlot
= VERTEX_SGV_SLOT
;
763 sgvChannel
= VERTEX_SGV_VAI_COMP
;
764 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
765 attribSlot
= VERTEX_POSITION_SLOT
;
767 attribSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
;
768 if (iface
->info
->writes_position
) {
773 Value
*pOutputOffset
= ADD(pVertexOffset
, VIMMED1(headerSize
+ attribSize
* attribSlot
)); // + sgvChannel ?
775 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
776 Value
*pLaneOffset
= VEXTRACT(pOutputOffset
, C(lane
));
777 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
778 Value
*pStreamOffset
= GEP(pStream
, pLaneOffset
);
779 pStreamOffset
= BITCAST(pStreamOffset
, mFP32PtrTy
);
781 Value
*pLaneMask
= VEXTRACT(vMask1
, C(lane
));
782 pStreamOffset
= SELECT(pLaneMask
, pStreamOffset
, pTmpPtr
);
784 for (uint32_t channel
= 0; channel
< 4; ++channel
) {
787 if (attribSlot
== VERTEX_SGV_SLOT
)
788 vData
= LOAD(unwrap(outputs
[attrib
][0]));
790 vData
= LOAD(unwrap(outputs
[attrib
][channel
]));
792 if (attribSlot
!= VERTEX_SGV_SLOT
||
793 sgvChannel
== channel
) {
794 vData
= VEXTRACT(vData
, C(lane
));
795 STORE(vData
, pStreamOffset
);
797 pStreamOffset
= GEP(pStreamOffset
, C(1));
802 /* When the output type is not points, the geometry shader may not
803 * output data to multiple streams. So early exit here.
805 if(iface
->pGsState
->outputTopology
!= TOP_POINT_LIST
) {
806 STACKRESTORE(pStack
);
810 // Info about stream id for each vertex
811 // is coded in 2 bits (4 vert per byte "box"):
812 // ----------------- ----------------- ----
813 // |d|d|c|c|b|b|a|a| |h|h|g|g|f|f|e|e| |...
814 // ----------------- ----------------- ----
816 // Calculate where need to put stream id for current vert
818 Value
*pShiftControl
= MUL(unwrap(emitted_vertices_vec
), VIMMED1(2));
820 // Calculate in which box put stream id for current vert.
821 Value
*pOffsetControl
= LSHR(unwrap(emitted_vertices_vec
), VIMMED1(2));
824 Value
*pStreamIdOffset
= ADD(pOffsetControl
, VIMMED1(VERTEX_COUNT_SIZE
));
826 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
827 Value
*pShift
= TRUNC(VEXTRACT(pShiftControl
, C(lane
)), mInt8Ty
);
828 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
830 Value
*pStreamOffset
= GEP(pStream
, VEXTRACT(pStreamIdOffset
, C(lane
)));
832 // Just make sure that not overflow max - stream id = (0,1,2,3)
833 Value
*vVal
= TRUNC(AND(VEXTRACT(unwrap(stream_id
), C(0)), C(0x3)), mInt8Ty
);
835 // Shift it to correct position in byte "box"
836 vVal
= SHL(vVal
, pShift
);
838 // Info about other vertices can be already stored
839 // so we need to read and add bits from current vert info.
840 Value
*storedValue
= LOAD(pStreamOffset
);
841 vVal
= OR(storedValue
, vVal
);
842 STORE(vVal
, pStreamOffset
);
845 STACKRESTORE(pStack
);
849 BuilderSWR::swr_gs_llvm_end_primitive(const struct lp_build_gs_iface
*gs_base
,
850 struct lp_build_context
* bld
,
851 LLVMValueRef total_emitted_vertices_vec
,
852 LLVMValueRef verts_per_prim_vec
,
853 LLVMValueRef emitted_prims_vec
,
854 LLVMValueRef mask_vec
)
856 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
858 /* When the output type is points, the geometry shader may output data
859 * to multiple streams, and end_primitive has no effect. Info about
860 * stream id for vertices is stored into the same place in memory where
861 * end primitive info is stored so early exit in this case.
863 if (iface
->pGsState
->outputTopology
== TOP_POINT_LIST
) {
867 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
869 Value
*vMask
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_mask
});
870 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, 8));
872 uint32_t vertsPerPrim
= iface
->num_verts_per_prim
;
875 ADD(MUL(unwrap(emitted_prims_vec
), VIMMED1(vertsPerPrim
)),
876 unwrap(verts_per_prim_vec
));
878 vCount
= unwrap(total_emitted_vertices_vec
);
880 Value
*mask
= unwrap(mask_vec
);
881 Value
*cmpMask
= VMASK(ICMP_NE(unwrap(verts_per_prim_vec
), VIMMED1(0)));
882 mask
= AND(mask
, cmpMask
);
883 vMask1
= TRUNC(mask
, VectorType::get(mInt1Ty
, 8));
885 vCount
= SUB(vCount
, VIMMED1(1));
886 Value
*vOffset
= ADD(UDIV(vCount
, VIMMED1(8)), VIMMED1(VERTEX_COUNT_SIZE
));
887 Value
*vValue
= SHL(VIMMED1(1), UREM(vCount
, VIMMED1(8)));
889 vValue
= TRUNC(vValue
, VectorType::get(mInt8Ty
, 8));
891 Value
*pStack
= STACKSAVE();
892 Value
*pTmpPtr
= ALLOCA(mInt8Ty
, C(4)); // used for dummy read/write for lane masking
894 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
895 Value
*vLaneOffset
= VEXTRACT(vOffset
, C(lane
));
896 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
897 Value
*pStreamOffset
= GEP(pStream
, vLaneOffset
);
899 Value
*pLaneMask
= VEXTRACT(vMask1
, C(lane
));
900 pStreamOffset
= SELECT(pLaneMask
, pStreamOffset
, pTmpPtr
);
902 Value
*vVal
= LOAD(pStreamOffset
);
903 vVal
= OR(vVal
, VEXTRACT(vValue
, C(lane
)));
904 STORE(vVal
, pStreamOffset
);
907 STACKRESTORE(pStack
);
911 BuilderSWR::swr_gs_llvm_epilogue(const struct lp_build_gs_iface
*gs_base
,
912 LLVMValueRef total_emitted_vertices_vec
,
913 LLVMValueRef emitted_prims_vec
)
915 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
917 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
919 // Store emit count to each output stream in the first DWORD
920 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
922 Value
* pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
923 pStream
= BITCAST(pStream
, mInt32PtrTy
);
924 Value
* pLaneCount
= VEXTRACT(unwrap(total_emitted_vertices_vec
), C(lane
));
925 STORE(pLaneCount
, pStream
);
930 BuilderSWR::swr_tcs_llvm_emit_prologue(struct lp_build_tgsi_soa_context
* bld
)
932 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)bld
->tcs_iface
;
934 // Iterate for all the vertices in the output patch
935 lp_build_for_loop_begin(&iface
->loop_state
, gallivm
,
936 lp_build_const_int32(gallivm
, 0),
938 lp_build_const_int32(gallivm
, iface
->output_vertices
),
939 lp_build_const_int32(gallivm
, 1));
941 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
942 bld
->system_values
.invocation_id
= wrap(VBROADCAST(unwrap(iface
->loop_state
.counter
)));
944 if (verbose_shader
) {
945 lp_build_printf(gallivm
, "Prologue LOOP: Iteration %d BEGIN\n", iface
->loop_state
.counter
);
946 lp_build_print_value(gallivm
, "LOOP: InvocationId: \n", bld
->system_values
.invocation_id
);
951 BuilderSWR::swr_tcs_llvm_emit_epilogue(struct lp_build_tgsi_soa_context
* bld
)
953 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)bld
->tcs_iface
;
955 if (verbose_shader
) {
956 lp_build_printf(gallivm
, "Epilogue LOOP: Iteration %d END\n", iface
->loop_state
.counter
);
958 lp_build_for_loop_end(&iface
->loop_state
);
962 BuilderSWR::swr_tcs_llvm_fetch_input(const struct lp_build_tcs_iface
*tcs_iface
,
963 struct lp_build_tgsi_context
* bld_base
,
964 boolean is_vindex_indirect
,
965 LLVMValueRef vertex_index
,
966 boolean is_aindex_indirect
,
967 LLVMValueRef attrib_index
,
968 LLVMValueRef swizzle_index
)
970 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)tcs_iface
;
971 Value
*vert_index
= unwrap(vertex_index
);
972 Value
*attr_index
= unwrap(attrib_index
);
974 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
976 if (verbose_shader
) {
977 lp_build_print_value(gallivm
, "TCS: Vertex index: ", vertex_index
);
978 lp_build_print_value(gallivm
, "TCS: Attrib index: ", attrib_index
);
979 lp_build_print_value(gallivm
, "TCS: Swizzle index: ", swizzle_index
);
982 if (is_vindex_indirect
) {
983 vert_index
= VEXTRACT(vert_index
, C(0));
984 if (verbose_shader
) {
985 lp_build_print_value(gallivm
, "TCS: Extracted vertex index: ", vertex_index
);
989 if (is_aindex_indirect
) {
990 attr_index
= VEXTRACT(attr_index
, C(0));
991 if (verbose_shader
) {
992 lp_build_print_value(gallivm
, "TCS: Extracted attrib index: ", attrib_index
);
996 Value
*attrib
= LOAD(GEP(iface
->pVtxAttribMap
, {C(0), attr_index
}));
997 if (verbose_shader
) {
998 lp_build_print_value(gallivm
, "TCS: Attrib index loaded from map: ", wrap(attrib
));
1001 Value
*pBase
= GEP(iface
->pTcsCtx
,
1002 { C(0), C(SWR_HS_CONTEXT_vert
), vert_index
,
1003 C(simdvertex_attrib
), attrib
/*attr_index*/, unwrap(swizzle_index
) });
1005 LLVMValueRef res
= wrap(LOAD(pBase
));
1007 if (verbose_shader
) {
1008 lp_build_print_value(gallivm
, "TCS input fetched: ", res
);
1014 BuilderSWR::swr_tcs_llvm_fetch_output(const struct lp_build_tcs_iface
*tcs_iface
,
1015 struct lp_build_tgsi_context
* bld_base
,
1016 boolean is_vindex_indirect
,
1017 LLVMValueRef vertex_index
,
1018 boolean is_aindex_indirect
,
1019 LLVMValueRef attrib_index
,
1020 LLVMValueRef swizzle_index
,
1023 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)tcs_iface
;
1025 Value
*vert_index
= unwrap(vertex_index
);
1026 Value
*attr_index
= unwrap(attrib_index
);
1028 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1030 if (verbose_shader
) {
1031 lp_build_print_value(gallivm
, "++TCSo: Vertex index: ", vertex_index
);
1032 lp_build_print_value(gallivm
, "++TCSo: Attrib index: ", wrap(attr_index
));
1033 lp_build_print_value(gallivm
, "++TCSo: Swizzle index: ", swizzle_index
);
1036 if (is_vindex_indirect
) {
1037 vert_index
= VEXTRACT(vert_index
, C(0));
1040 lp_build_print_value(gallivm
, "TCSo: Extracted vertex index: ", vertex_index
);
1044 if (is_aindex_indirect
) {
1045 attr_index
= VEXTRACT(attr_index
, C(0));
1046 if (verbose_shader
) {
1047 lp_build_print_value(gallivm
, "TCSo: Extracted attrib index: ", attrib_index
);
1051 Value
* res
= unwrap(bld_base
->base
.zero
);
1053 for (uint32_t lane
= 0; lane
< mVWidth
; lane
++) {
1054 Value
* p1
= LOAD(iface
->pTcsCtx
, {0, SWR_HS_CONTEXT_pCPout
});
1055 Value
* pCpOut
= GEP(p1
, {lane
});
1057 if (name
== TGSI_SEMANTIC_TESSOUTER
|| name
== TGSI_SEMANTIC_TESSINNER
) {
1059 Value
* tessFactors
= GEP(pCpOut
, {(uint32_t)0, ScalarPatch_tessFactors
});
1060 Value
* tessFactorArray
= nullptr;
1061 if (name
== TGSI_SEMANTIC_TESSOUTER
) {
1062 tessFactorArray
= GEP(tessFactors
, {(uint32_t)0, SWR_TESSELLATION_FACTORS_OuterTessFactors
});
1064 tessFactorArray
= GEP(tessFactors
, {(uint32_t)0, SWR_TESSELLATION_FACTORS_InnerTessFactors
});
1066 Value
* tessFactor
= GEP(tessFactorArray
, {C(0), unwrap(swizzle_index
)});
1067 res
= VINSERT(res
, LOAD(tessFactor
), C(lane
));
1069 } else if (name
== TGSI_SEMANTIC_PATCH
) {
1070 lp_build_print_value(gallivm
, "bbbbb TCS per-patch attr_index: ", wrap(attr_index
));
1071 Value
* attr
= GEP(pCpOut
, {C(0), C(ScalarPatch_patchData
), C(ScalarCPoint_attrib
), attr_index
, unwrap(swizzle_index
)});
1072 res
= VINSERT(res
, LOAD(attr
), C(lane
));
1073 if (verbose_shader
) {
1074 lp_build_print_value(gallivm
, "++TCSo per-patch lane (patch-id): ", wrap(C(lane
)));
1075 lp_build_print_value(gallivm
, "++TCSo per-patch loaded value: ", wrap(res
));
1078 // Generic attribute
1080 LOAD(GEP(iface
->pVtxOutputAttribMap
, {C(0), attr_index
}));
1083 lp_build_print_value(gallivm
, "TCSo: Attrib index from map: ", wrap(attrib
));
1085 Value
* attr_chan
= GEP(pCpOut
, {C(0), C(ScalarPatch_cp
), vert_index
,
1086 C(ScalarCPoint_attrib
), attrib
, unwrap(swizzle_index
)});
1088 res
= VINSERT(res
, LOAD(attr_chan
), C(lane
));
1092 if (verbose_shader
) {
1093 lp_build_print_value(gallivm
, "TCSo: output fetched: ", wrap(res
));
1099 BuilderSWR::swr_tcs_llvm_store_output(const struct lp_build_tcs_iface
*tcs_iface
,
1100 struct lp_build_tgsi_context
*bld_base
,
1102 boolean is_vindex_indirect
,
1103 LLVMValueRef vertex_index
,
1104 boolean is_aindex_indirect
,
1105 LLVMValueRef attrib_index
,
1106 LLVMValueRef swizzle_index
,
1109 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)tcs_iface
;
1110 struct lp_build_tgsi_soa_context
* bld
= (struct lp_build_tgsi_soa_context
*)bld_base
;
1112 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1114 if (verbose_shader
) {
1115 lp_build_printf(gallivm
, "[TCS OUT] =============================================\n");
1118 if (verbose_shader
) {
1119 lp_build_print_value(gallivm
, "[TCS OUT] Store mask: ", bld
->exec_mask
.exec_mask
);
1120 lp_build_print_value(gallivm
, "[TCS OUT] Store value: ", value
);
1123 Value
*vert_index
= unwrap(vertex_index
);
1124 Value
*attr_index
= unwrap(attrib_index
);
1126 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1128 if (verbose_shader
) {
1129 lp_build_print_value(gallivm
, "[TCS OUT] Vertex index: ", vertex_index
);
1130 lp_build_print_value(gallivm
, "[TCS OUT] Attrib index: ", wrap(attr_index
));
1131 lp_build_print_value(gallivm
, "[TCS OUT] Swizzle index: ", swizzle_index
);
1134 if (is_vindex_indirect
) {
1135 vert_index
= VEXTRACT(vert_index
, C(0));
1138 lp_build_print_value(gallivm
, "[TCS OUT] Extracted vertex index: ", vertex_index
);
1142 if (is_aindex_indirect
) {
1143 attr_index
= VEXTRACT(attr_index
, C(0));
1144 if (verbose_shader
) {
1145 lp_build_print_value(gallivm
, "[TCS OUT] Extracted attrib index: ", wrap(attr_index
));
1149 for (uint32_t lane
= 0; lane
< mVWidth
; lane
++) {
1150 Value
* p1
= LOAD(iface
->pTcsCtx
, {0, SWR_HS_CONTEXT_pCPout
});
1151 Value
* pCpOut
= GEP(p1
, {lane
});
1153 if (name
== TGSI_SEMANTIC_TESSOUTER
|| name
== TGSI_SEMANTIC_TESSINNER
) {
1154 Value
* tessFactors
= GEP(pCpOut
, {(uint32_t)0, ScalarPatch_tessFactors
});
1155 Value
* tessFactorArray
= nullptr;
1156 if (name
== TGSI_SEMANTIC_TESSOUTER
) {
1157 tessFactorArray
= GEP(tessFactors
, {(uint32_t)0, SWR_TESSELLATION_FACTORS_OuterTessFactors
});
1159 tessFactorArray
= GEP(tessFactors
, {(uint32_t)0, SWR_TESSELLATION_FACTORS_InnerTessFactors
});
1161 Value
* tessFactor
= GEP(tessFactorArray
, {C(0), unwrap(swizzle_index
)});
1162 Value
* valueToStore
= VEXTRACT(unwrap(value
), C(lane
));
1163 struct lp_exec_mask
*mask
= &bld
->exec_mask
;
1164 if (mask
->has_mask
) {
1165 Value
*originalVal
= LOAD(tessFactor
);
1166 Value
*vMask
= TRUNC(VEXTRACT(unwrap(mask
->exec_mask
), C(lane
)), mInt1Ty
);
1167 valueToStore
= SELECT(vMask
, valueToStore
, originalVal
);
1169 STORE(valueToStore
, tessFactor
);
1170 if (verbose_shader
) {
1171 lp_build_print_value(gallivm
, "[TCS OUT][FACTOR] Stored value: ", wrap(valueToStore
));
1173 } else if (name
== TGSI_SEMANTIC_PATCH
) {
1174 Value
* attrib
= LOAD(GEP(iface
->pPatchOutputAttribMap
, {C(0), attr_index
}));
1175 if (verbose_shader
) {
1176 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] vert_index: ", wrap(vert_index
));
1177 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] attr_index: ", wrap(attr_index
));
1178 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] vert_index_indirect: ", wrap(C(is_vindex_indirect
)));
1179 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] attr_index_indirect: ", wrap(C(is_aindex_indirect
)));
1180 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] attr index loaded from map: ", wrap(attrib
));
1182 Value
* attr
= GEP(pCpOut
, {C(0), C(ScalarPatch_patchData
), C(ScalarCPoint_attrib
), attrib
});
1183 Value
* value_to_store
= VEXTRACT(unwrap(value
), C(lane
));
1184 if (verbose_shader
) {
1185 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] lane (patch-id): ", wrap(C(lane
)));
1186 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] value to store: ", value
);
1187 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] per-patch value to store: ", wrap(value_to_store
));
1188 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] chan_index: ", swizzle_index
);
1190 struct lp_exec_mask
*mask
= &bld
->exec_mask
;
1191 if (mask
->has_mask
) {
1192 Value
*originalVal
= LOADV(attr
, {C(0), unwrap(swizzle_index
)});
1193 Value
*vMask
= TRUNC(VEXTRACT(unwrap(mask
->exec_mask
), C(lane
)), mInt1Ty
);
1194 value_to_store
= SELECT(vMask
, BITCAST(value_to_store
, mFP32Ty
), originalVal
);
1195 if (verbose_shader
) {
1196 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] store mask: ", bld
->exec_mask
.exec_mask
);
1197 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] loaded original value: ", wrap(originalVal
));
1198 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] vMask: ", wrap(vMask
));
1199 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] selected value to store: ", wrap(value_to_store
));
1202 STOREV(value_to_store
, attr
, {C(0), unwrap(swizzle_index
)});
1203 if (verbose_shader
) {
1204 lp_build_print_value(gallivm
, "[TCS OUT][PATCH] stored value: ", wrap(value_to_store
));
1207 Value
* value_to_store
= VEXTRACT(unwrap(value
), C(lane
));
1208 Value
* attrib
= LOAD(GEP(iface
->pVtxOutputAttribMap
, {C(0), attr_index
}));
1210 if (verbose_shader
) {
1211 lp_build_print_value(gallivm
, "[TCS OUT][VTX] invocation_id: ", bld
->system_values
.invocation_id
);
1212 lp_build_print_value(gallivm
, "[TCS OUT][VTX] attribIndex: ", wrap(attr_index
));
1213 lp_build_print_value(gallivm
, "[TCS OUT][VTX] attrib read from map: ", wrap(attrib
));
1214 lp_build_print_value(gallivm
, "[TCS OUT][VTX] chan_index: ", swizzle_index
);
1215 lp_build_print_value(gallivm
, "[TCS OUT][VTX] value: ", value
);
1216 lp_build_print_value(gallivm
, "[TCS OUT][VTX] value_to_store: ", wrap(value_to_store
));
1219 Value
* attr_chan
= GEP(pCpOut
, {C(0), C(ScalarPatch_cp
),
1220 VEXTRACT(unwrap(bld
->system_values
.invocation_id
), C(0)),
1221 C(ScalarCPoint_attrib
), attrib
, unwrap(swizzle_index
)});
1223 // Mask output values if needed
1224 struct lp_exec_mask
*mask
= &bld
->exec_mask
;
1225 if (mask
->has_mask
) {
1226 Value
*originalVal
= LOAD(attr_chan
);
1227 Value
*vMask
= TRUNC(VEXTRACT(unwrap(mask
->exec_mask
), C(lane
)), mInt1Ty
);
1228 // convert input to float before trying to store
1229 value_to_store
= SELECT(vMask
, BITCAST(value_to_store
, mFP32Ty
), originalVal
);
1231 STORE(value_to_store
, attr_chan
);
1232 if (verbose_shader
) {
1233 lp_build_print_value(gallivm
, "[TCS OUT][VTX] stored: ", wrap(value_to_store
));
1242 BuilderSWR::swr_tcs_llvm_emit_barrier(const struct lp_build_tcs_iface
*tcs_iface
,
1243 struct lp_build_tgsi_context
*bld_base
)
1245 swr_tcs_llvm_iface
*iface
= (swr_tcs_llvm_iface
*)tcs_iface
;
1246 struct lp_build_tgsi_soa_context
* bld
= (struct lp_build_tgsi_soa_context
*)bld_base
;
1248 if (verbose_shader
) {
1249 lp_build_printf(gallivm
, "Barrier LOOP: Iteration %d END\n", iface
->loop_state
.counter
);
1252 // End previous loop
1253 lp_build_for_loop_end(&iface
->loop_state
);
1256 lp_build_for_loop_begin(&iface
->loop_state
, gallivm
,
1257 lp_build_const_int32(gallivm
, 0),
1259 lp_build_const_int32(gallivm
, iface
->output_vertices
),
1260 lp_build_const_int32(gallivm
, 1));
1263 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1265 bld
->system_values
.invocation_id
= wrap(VBROADCAST(unwrap(iface
->loop_state
.counter
)));
1267 if (verbose_shader
) {
1268 lp_build_printf(gallivm
, "Barrier LOOP: Iteration %d BEGIN\n", iface
->loop_state
.counter
);
1269 lp_build_print_value(gallivm
, "LOOP: InvocationId: \n", bld
->system_values
.invocation_id
);
1275 BuilderSWR::swr_tes_llvm_fetch_patch_input(const struct lp_build_tes_iface
*tes_iface
,
1276 struct lp_build_tgsi_context
* bld_base
,
1277 boolean is_aindex_indirect
,
1278 LLVMValueRef attrib_index
,
1279 LLVMValueRef swizzle_index
)
1281 swr_tes_llvm_iface
*iface
= (swr_tes_llvm_iface
*)tes_iface
;
1282 Value
*attr_index
= unwrap(attrib_index
);
1283 Value
*res
= unwrap(bld_base
->base
.zero
);
1285 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1287 if (verbose_shader
) {
1288 lp_build_printf(gallivm
, "[TES IN][PATCH] --------------------------------------\n");
1291 if (is_aindex_indirect
) {
1293 struct lp_type type
= bld_base
->base
.type
;
1295 for (i
= 0; i
< type
.length
; i
++) {
1296 Value
*attr_chan_index
= attr_index
;
1298 if (is_aindex_indirect
) {
1299 attr_chan_index
= VEXTRACT(attr_index
, C(i
));
1303 LOAD(GEP(iface
->pPatchAttribMap
, {C(0), attr_chan_index
}));
1305 Value
*pCpIn
= LOAD(iface
->pTesCtx
, {0, SWR_DS_CONTEXT_pCpIn
}, "pCpIn");
1306 Value
*pPatchData
= GEP(pCpIn
, {(uint32_t)0, ScalarPatch_patchData
});
1307 Value
*pAttr
= GEP(pPatchData
, {(uint32_t)0, ScalarCPoint_attrib
});
1308 Value
*Val
= LOADV(pAttr
, {C(0), attrib
, unwrap(swizzle_index
)});
1309 if (verbose_shader
) {
1310 lp_build_print_value(gallivm
, "[TES IN][PATCH] attrib_index: ", attrib_index
);
1311 lp_build_print_value(gallivm
, "[TES IN][PATCH] attr_chan_index: ", wrap(attr_chan_index
));
1312 lp_build_print_value(gallivm
, "[TES IN][PATCH] attrib read from map: ", wrap(attrib
));
1313 lp_build_print_value(gallivm
, "[TES IN][PATCH] swizzle_index: ", swizzle_index
);
1314 lp_build_print_value(gallivm
, "[TES IN][PATCH] Loaded: ", wrap(Val
));
1316 res
= VINSERT(res
, Val
, C(i
));
1319 Value
*attrib
= LOAD(GEP(iface
->pPatchAttribMap
, {C(0), attr_index
}));
1321 Value
*pCpIn
= LOAD(iface
->pTesCtx
, {(uint32_t)0, SWR_DS_CONTEXT_pCpIn
}, "pCpIn");
1322 Value
*pPatchData
= GEP(pCpIn
, {(uint32_t)0, ScalarPatch_patchData
});
1323 Value
*pAttr
= GEP(pPatchData
, {(uint32_t)0, ScalarCPoint_attrib
});
1324 Value
*Val
= LOADV(pAttr
, {C(0), attrib
, unwrap(swizzle_index
)});
1325 if (verbose_shader
) {
1326 lp_build_print_value(gallivm
, "[TES IN][PATCH] attrib_index: ", attrib_index
);
1327 lp_build_print_value(gallivm
, "[TES IN][PATCH] attr_chan_index: ", wrap(attr_index
));
1328 lp_build_print_value(gallivm
, "[TES IN][PATCH] attrib read from map: ", wrap(attrib
));
1329 lp_build_print_value(gallivm
, "[TES IN][PATCH] swizzle_index: ", swizzle_index
);
1330 lp_build_print_value(gallivm
, "[TES IN][PATCH] Loaded: ", wrap(Val
));
1332 res
= VBROADCAST(Val
);
1334 if (verbose_shader
) {
1335 lp_build_print_value(gallivm
, "[TES IN][PATCH] returning: ", wrap(res
));
1343 BuilderSWR::swr_tes_llvm_fetch_vtx_input(const struct lp_build_tes_iface
*tes_iface
,
1344 struct lp_build_tgsi_context
* bld_base
,
1345 boolean is_vindex_indirect
,
1346 LLVMValueRef vertex_index
,
1347 boolean is_aindex_indirect
,
1348 LLVMValueRef attrib_index
,
1349 LLVMValueRef swizzle_index
)
1351 swr_tes_llvm_iface
*iface
= (swr_tes_llvm_iface
*)tes_iface
;
1352 Value
*vert_index
= unwrap(vertex_index
);
1353 Value
*attr_index
= unwrap(attrib_index
);
1355 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1357 if (verbose_shader
) {
1358 lp_build_printf(gallivm
, "[TES IN][VTX] --------------------------------------\n");
1361 Value
*res
= unwrap(bld_base
->base
.zero
);
1362 if (is_vindex_indirect
|| is_aindex_indirect
) {
1364 struct lp_type type
= bld_base
->base
.type
;
1366 for (i
= 0; i
< type
.length
; i
++) {
1367 Value
*vert_chan_index
= vert_index
;
1368 Value
*attr_chan_index
= attr_index
;
1370 if (is_vindex_indirect
) {
1371 vert_chan_index
= VEXTRACT(vert_index
, C(i
));
1373 if (is_aindex_indirect
) {
1374 attr_chan_index
= VEXTRACT(attr_index
, C(i
));
1378 LOAD(GEP(iface
->pVtxAttribMap
, {C(0), attr_chan_index
}));
1380 Value
*pCpIn
= LOAD(iface
->pTesCtx
, {0, SWR_DS_CONTEXT_pCpIn
}, "pCpIn");
1381 Value
*pCp
= GEP(pCpIn
, {0, ScalarPatch_cp
});
1382 Value
*pVertex
= GEP(pCp
, {(Value
*)C(0), vert_chan_index
});
1383 Value
*pAttrTab
= GEP(pVertex
, {uint32_t(0), uint32_t(0)});
1384 Value
*pAttr
= GEP(pAttrTab
, {(Value
*)C(0), attrib
});
1385 Value
*Val
= LOADV(pAttr
, {C(0), unwrap(swizzle_index
)});
1386 if (verbose_shader
) {
1387 lp_build_print_value(gallivm
, "[TES IN][VTX] attrib_index: ", attrib_index
);
1388 lp_build_print_value(gallivm
, "[TES IN][VTX] attr_chan_index: ", wrap(attr_index
));
1389 lp_build_print_value(gallivm
, "[TES IN][VTX] attrib read from map: ", wrap(attrib
));
1390 lp_build_print_value(gallivm
, "[TES IN][VTX] swizzle_index: ", swizzle_index
);
1391 lp_build_print_value(gallivm
, "[TES IN][VTX] Loaded: ", wrap(Val
));
1393 res
= VINSERT(res
, Val
, C(i
));
1396 Value
*attrib
= LOAD(GEP(iface
->pVtxAttribMap
, {C(0), attr_index
}));
1398 Value
*pCpIn
= LOAD(iface
->pTesCtx
, {0, SWR_DS_CONTEXT_pCpIn
}, "pCpIn");
1399 Value
*pCp
= GEP(pCpIn
, {0, ScalarPatch_cp
});
1400 Value
*pVertex
= GEP(pCp
, {(Value
*)C(0), vert_index
});
1401 Value
*pAttrTab
= GEP(pVertex
, {uint32_t(0), uint32_t(0)});
1402 Value
*pAttr
= GEP(pAttrTab
, {(Value
*)C(0), attrib
});
1403 Value
*Val
= LOADV(pAttr
, {C(0), unwrap(swizzle_index
)});
1404 if (verbose_shader
) {
1405 lp_build_print_value(gallivm
, "[TES IN][VTX] attrib_index: ", attrib_index
);
1406 lp_build_print_value(gallivm
, "[TES IN][VTX] attr_chan_index: ", wrap(attr_index
));
1407 lp_build_print_value(gallivm
, "[TES IN][VTX] attrib read from map: ", wrap(attrib
));
1408 lp_build_print_value(gallivm
, "[TES IN][VTX] swizzle_index: ", swizzle_index
);
1409 lp_build_print_value(gallivm
, "[TES IN][VTX] Loaded: ", wrap(Val
));
1411 res
= VBROADCAST(Val
);
1413 if (verbose_shader
) {
1414 lp_build_print_value(gallivm
, "[TES IN][VTX] returning: ", wrap(res
));
1423 BuilderSWR::CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
1425 SWR_GS_STATE
*pGS
= &ctx
->gs
->gsState
;
1426 struct tgsi_shader_info
*info
= &ctx
->gs
->info
.base
;
1428 memset(pGS
, 0, sizeof(*pGS
));
1430 pGS
->gsEnable
= true;
1432 pGS
->numInputAttribs
= (VERTEX_ATTRIB_START_SLOT
- VERTEX_POSITION_SLOT
) + info
->num_inputs
;
1433 pGS
->outputTopology
=
1434 swr_convert_prim_topology(info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
], 0);
1436 /* It's +1 because emit_vertex in swr is always called exactly one time more
1437 * than max_vertices passed in Geometry Shader. We need to allocate more memory
1438 * to avoid crash/memory overwritten.
1440 pGS
->maxNumVerts
= info
->properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
] + 1;
1441 pGS
->instanceCount
= info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
];
1443 // If point primitive then assume to use multiple streams
1444 if(pGS
->outputTopology
== TOP_POINT_LIST
) {
1445 pGS
->isSingleStream
= false;
1447 pGS
->isSingleStream
= true;
1448 pGS
->singleStreamID
= 0;
1451 pGS
->vertexAttribOffset
= VERTEX_POSITION_SLOT
;
1452 pGS
->inputVertStride
= pGS
->numInputAttribs
+ pGS
->vertexAttribOffset
;
1453 pGS
->outputVertexSize
= SWR_VTX_NUM_SLOTS
;
1454 pGS
->controlDataSize
= 8; // GS ouputs max of 8 32B units
1455 pGS
->controlDataOffset
= VERTEX_COUNT_SIZE
;
1456 pGS
->outputVertexOffset
= pGS
->controlDataOffset
+ CONTROL_HEADER_SIZE
;
1458 pGS
->allocationSize
=
1459 VERTEX_COUNT_SIZE
+ // vertex count
1460 CONTROL_HEADER_SIZE
+ // control header
1461 (SWR_VTX_NUM_SLOTS
* 16) * // sizeof vertex
1462 pGS
->maxNumVerts
; // num verts
1464 struct swr_geometry_shader
*gs
= ctx
->gs
;
1466 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1467 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1469 memset(outputs
, 0, sizeof(outputs
));
1471 AttrBuilder attrBuilder
;
1472 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
1474 std::vector
<Type
*> gsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
1475 PointerType::get(mInt8Ty
, 0),
1476 PointerType::get(Gen_SWR_GS_CONTEXT(JM()), 0)};
1477 FunctionType
*vsFuncType
=
1478 FunctionType::get(Type::getVoidTy(JM()->mContext
), gsArgs
, false);
1480 // create new vertex shader function
1481 auto pFunction
= Function::Create(vsFuncType
,
1482 GlobalValue::ExternalLinkage
,
1484 JM()->mpCurrentModule
);
1485 #if LLVM_VERSION_MAJOR < 5
1486 AttributeSet attrSet
= AttributeSet::get(
1487 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
1488 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
1490 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
1493 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
1494 IRB()->SetInsertPoint(block
);
1495 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
1497 auto argitr
= pFunction
->arg_begin();
1498 Value
*hPrivateData
= &*argitr
++;
1499 hPrivateData
->setName("hPrivateData");
1500 Value
*pWorkerData
= &*argitr
++;
1501 pWorkerData
->setName("pWorkerData");
1502 Value
*pGsCtx
= &*argitr
++;
1503 pGsCtx
->setName("gsCtx");
1506 GEP(hPrivateData
, {C(0), C(swr_draw_context_constantGS
)});
1507 consts_ptr
->setName("gs_constants");
1508 Value
*const_sizes_ptr
=
1509 GEP(hPrivateData
, {0, swr_draw_context_num_constantsGS
});
1510 const_sizes_ptr
->setName("num_gs_constants");
1512 struct lp_build_sampler_soa
*sampler
=
1513 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_GEOMETRY
);
1515 struct lp_bld_tgsi_system_values system_values
;
1516 memset(&system_values
, 0, sizeof(system_values
));
1517 system_values
.prim_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_PrimitiveID
}));
1518 system_values
.invocation_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_InstanceID
}));
1520 std::vector
<Constant
*> mapConstants
;
1521 Value
*vtxAttribMap
= ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
1522 for (unsigned slot
= 0; slot
< info
->num_inputs
; slot
++) {
1523 ubyte semantic_name
= info
->input_semantic_name
[slot
];
1524 ubyte semantic_idx
= info
->input_semantic_index
[slot
];
1526 unsigned vs_slot
= locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
);
1528 vs_slot
+= VERTEX_ATTRIB_START_SLOT
;
1530 if (ctx
->vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
1533 if (semantic_name
== TGSI_SEMANTIC_POSITION
)
1534 vs_slot
= VERTEX_POSITION_SLOT
;
1536 STORE(C(vs_slot
), vtxAttribMap
, {0, slot
});
1537 mapConstants
.push_back(C(vs_slot
));
1540 struct lp_build_mask_context mask
;
1541 Value
*mask_val
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_mask
}, "gsMask");
1542 lp_build_mask_begin(&mask
, gallivm
,
1543 lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
1545 // zero out cut buffer so we can load/modify/store bits
1546 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
1548 Value
* pStream
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
1549 #if LLVM_VERSION_MAJOR >= 10
1550 MEMSET(pStream
, C((char)0), VERTEX_COUNT_SIZE
+ CONTROL_HEADER_SIZE
, MaybeAlign(sizeof(float) * KNOB_SIMD_WIDTH
));
1552 MEMSET(pStream
, C((char)0), VERTEX_COUNT_SIZE
+ CONTROL_HEADER_SIZE
, sizeof(float) * KNOB_SIMD_WIDTH
);
1556 struct swr_gs_llvm_iface gs_iface
;
1557 gs_iface
.base
.fetch_input
= ::swr_gs_llvm_fetch_input
;
1558 gs_iface
.base
.emit_vertex
= ::swr_gs_llvm_emit_vertex
;
1559 gs_iface
.base
.end_primitive
= ::swr_gs_llvm_end_primitive
;
1560 gs_iface
.base
.gs_epilogue
= ::swr_gs_llvm_epilogue
;
1561 gs_iface
.pBuilder
= this;
1562 gs_iface
.pGsCtx
= pGsCtx
;
1563 gs_iface
.pGsState
= pGS
;
1564 gs_iface
.num_outputs
= gs
->info
.base
.num_outputs
;
1565 gs_iface
.num_verts_per_prim
=
1566 u_vertices_per_prim((pipe_prim_type
)info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
1567 gs_iface
.info
= info
;
1568 gs_iface
.pVtxAttribMap
= vtxAttribMap
;
1570 struct lp_build_tgsi_params params
;
1571 memset(¶ms
, 0, sizeof(params
));
1572 params
.type
= lp_type_float_vec(32, 32 * 8);
1573 params
.mask
= & mask
;
1574 params
.consts_ptr
= wrap(consts_ptr
);
1575 params
.const_sizes_ptr
= wrap(const_sizes_ptr
);
1576 params
.system_values
= &system_values
;
1577 params
.inputs
= inputs
;
1578 params
.context_ptr
= wrap(hPrivateData
);
1579 params
.sampler
= sampler
;
1580 params
.info
= &gs
->info
.base
;
1581 params
.gs_iface
= &gs_iface
.base
;
1583 lp_build_tgsi_soa(gallivm
,
1588 lp_build_mask_end(&mask
);
1590 sampler
->destroy(sampler
);
1592 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1596 gallivm_verify_function(gallivm
, wrap(pFunction
));
1597 gallivm_compile_module(gallivm
);
1600 (PFN_GS_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
1602 debug_printf("geom shader %p\n", pFunc
);
1603 assert(pFunc
&& "Error: GeomShader = NULL");
1605 JM()->mIsModuleFinalized
= true;
1611 BuilderSWR::CompileTES(struct swr_context
*ctx
, swr_jit_tes_key
&key
)
1613 SWR_TS_STATE
*pTS
= &ctx
->tsState
;
1614 struct tgsi_shader_info
*info
= &ctx
->tes
->info
.base
;
1616 // tessellation is enabled if TES is present
1617 // clear tessellation state here then
1618 memset(pTS
, 0, sizeof(*pTS
));
1620 pTS
->tsEnable
= true;
1622 unsigned tes_prim_mode
= info
->properties
[TGSI_PROPERTY_TES_PRIM_MODE
];
1623 unsigned tes_spacing
= info
->properties
[TGSI_PROPERTY_TES_SPACING
];
1624 bool tes_vertex_order_cw
= info
->properties
[TGSI_PROPERTY_TES_VERTEX_ORDER_CW
];
1625 bool tes_point_mode
= info
->properties
[TGSI_PROPERTY_TES_POINT_MODE
];
1626 SWR_TS_DOMAIN type
= SWR_TS_ISOLINE
;
1627 SWR_TS_PARTITIONING partitioning
= SWR_TS_EVEN_FRACTIONAL
;
1628 SWR_TS_OUTPUT_TOPOLOGY topology
= SWR_TS_OUTPUT_POINT
;
1629 PRIMITIVE_TOPOLOGY postDSTopology
= TOP_POINT_LIST
;
1631 // TESS_TODO: move this to helper functions to improve readability
1632 switch (tes_prim_mode
) {
1633 case PIPE_PRIM_LINES
:
1634 type
= SWR_TS_ISOLINE
;
1635 postDSTopology
= TOP_LINE_LIST
;
1637 case PIPE_PRIM_TRIANGLES
:
1639 postDSTopology
= TOP_TRIANGLE_LIST
;
1641 case PIPE_PRIM_QUADS
:
1643 // See OpenGL spec - quads are tessellated into triangles
1644 postDSTopology
= TOP_TRIANGLE_LIST
;
1650 switch (tes_spacing
) {
1651 case PIPE_TESS_SPACING_FRACTIONAL_ODD
:
1652 partitioning
= SWR_TS_ODD_FRACTIONAL
;
1654 case PIPE_TESS_SPACING_FRACTIONAL_EVEN
:
1655 partitioning
= SWR_TS_EVEN_FRACTIONAL
;
1657 case PIPE_TESS_SPACING_EQUAL
:
1658 partitioning
= SWR_TS_INTEGER
;
1664 if (tes_point_mode
) {
1665 topology
= SWR_TS_OUTPUT_POINT
;
1666 postDSTopology
= TOP_POINT_LIST
;
1668 else if (tes_prim_mode
== PIPE_PRIM_LINES
) {
1669 topology
= SWR_TS_OUTPUT_LINE
;
1671 else if (tes_vertex_order_cw
) {
1672 topology
= SWR_TS_OUTPUT_TRI_CW
;
1675 topology
= SWR_TS_OUTPUT_TRI_CCW
;
1679 pTS
->tsOutputTopology
= topology
;
1680 pTS
->partitioning
= partitioning
;
1681 pTS
->numDsOutputAttribs
= info
->num_outputs
;
1682 pTS
->postDSTopology
= postDSTopology
;
1684 pTS
->dsAllocationSize
= SWR_VTX_NUM_SLOTS
* MAX_NUM_VERTS_PER_PRIM
;
1685 pTS
->vertexAttribOffset
= VERTEX_ATTRIB_START_SLOT
;
1686 pTS
->srcVertexAttribOffset
= VERTEX_ATTRIB_START_SLOT
;
1687 pTS
->dsOutVtxAttribOffset
= VERTEX_ATTRIB_START_SLOT
;
1689 struct swr_tess_evaluation_shader
*tes
= ctx
->tes
;
1691 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1692 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1694 memset(outputs
, 0, sizeof(outputs
));
1696 AttrBuilder attrBuilder
;
1697 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
1699 std::vector
<Type
*> tesArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
1700 PointerType::get(mInt8Ty
, 0),
1701 PointerType::get(Gen_SWR_DS_CONTEXT(JM()), 0)};
1702 FunctionType
*tesFuncType
=
1703 FunctionType::get(Type::getVoidTy(JM()->mContext
), tesArgs
, false);
1705 // create new vertex shader function
1706 auto pFunction
= Function::Create(tesFuncType
,
1707 GlobalValue::ExternalLinkage
,
1709 JM()->mpCurrentModule
);
1711 #if LLVM_VERSION_MAJOR < 5
1712 AttributeSet attrSet
= AttributeSet::get(
1713 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
1714 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
1716 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
1719 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
1720 IRB()->SetInsertPoint(block
);
1721 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
1723 auto argitr
= pFunction
->arg_begin();
1724 Value
*hPrivateData
= &*argitr
++;
1725 hPrivateData
->setName("hPrivateData");
1726 Value
*pWorkerData
= &*argitr
++;
1727 pWorkerData
->setName("pWorkerData");
1728 Value
*pTesCtx
= &*argitr
++;
1729 pTesCtx
->setName("tesCtx");
1732 GEP(hPrivateData
, {C(0), C(swr_draw_context_constantTES
)});
1733 consts_ptr
->setName("tes_constants");
1734 Value
*const_sizes_ptr
=
1735 GEP(hPrivateData
, {0, swr_draw_context_num_constantsTES
});
1736 const_sizes_ptr
->setName("num_tes_constants");
1738 struct lp_build_sampler_soa
*sampler
=
1739 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_TESS_EVAL
);
1741 struct lp_bld_tgsi_system_values system_values
;
1742 memset(&system_values
, 0, sizeof(system_values
));
1744 // Load and calculate system values
1745 // Tessellation coordinates (gl_TessCoord)
1746 Value
*vecOffset
= LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_vectorOffset
}, "vecOffset");
1747 Value
*vecStride
= LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_vectorStride
}, "vecStride");
1748 Value
*vecIndex
= LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_vectorOffset
});
1750 Value
* tess_coord
= ALLOCA(ArrayType::get(mSimdFP32Ty
, 3));
1752 Value
*tessCoordU
= LOADV(LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_pDomainU
}), {vecIndex
}, "tessCoordU");
1753 STORE(tessCoordU
, tess_coord
, {0, 0});
1754 Value
*tessCoordV
= LOADV(LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_pDomainV
}), {vecIndex
}, "tessCoordV");
1755 STORE(tessCoordV
, tess_coord
, {0, 1});
1756 Value
*tessCoordW
= FSUB(FSUB(VIMMED1(1.0f
), tessCoordU
), tessCoordV
, "tessCoordW");
1757 STORE(tessCoordW
, tess_coord
, {0, 2});
1758 system_values
.tess_coord
= wrap(tess_coord
);
1761 system_values
.prim_id
= wrap(VBROADCAST(LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_PrimitiveID
}), "PrimitiveID"));
1763 // Tessellation factors
1764 Value
* pPatch
= LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_pCpIn
});
1765 Value
* pTessFactors
= GEP(pPatch
, {C(0), C(ScalarPatch_tessFactors
)});
1767 assert(SWR_NUM_OUTER_TESS_FACTORS
== 4);
1768 Value
* sys_value_outer_factors
= UndefValue::get(VectorType::get(mFP32Ty
, 4));
1769 for (unsigned i
= 0; i
< SWR_NUM_OUTER_TESS_FACTORS
; i
++) {
1770 Value
* v
= LOAD(pTessFactors
, {0, SWR_TESSELLATION_FACTORS_OuterTessFactors
, i
});
1771 sys_value_outer_factors
= VINSERT(sys_value_outer_factors
, v
, i
, "gl_TessLevelOuter");
1773 system_values
.tess_outer
= wrap(sys_value_outer_factors
);
1775 assert(SWR_NUM_INNER_TESS_FACTORS
== 2);
1776 Value
* sys_value_inner_factors
= UndefValue::get(VectorType::get(mFP32Ty
, 4));
1777 for (unsigned i
= 0; i
< SWR_NUM_INNER_TESS_FACTORS
; i
++) {
1778 Value
* v
= LOAD(pTessFactors
, {0, SWR_TESSELLATION_FACTORS_InnerTessFactors
, i
});
1779 sys_value_inner_factors
= VINSERT(sys_value_inner_factors
, v
, i
, "gl_TessLevelInner");
1781 system_values
.tess_inner
= wrap(sys_value_inner_factors
);
1785 lp_build_print_value(gallivm
, "tess_coord = ", system_values
.tess_coord
);
1788 struct tgsi_shader_info
*pPrevShader
= nullptr;
1791 pPrevShader
= &ctx
->tcs
->info
.base
;
1794 pPrevShader
= &ctx
->vs
->info
.base
;
1797 // Figure out how many per-patch attributes we have
1798 unsigned perPatchAttrs
= 0;
1799 unsigned genericAttrs
= 0;
1800 unsigned tessLevelAttrs
= 0;
1801 unsigned sgvAttrs
= 0;
1802 for (unsigned slot
= 0; slot
< pPrevShader
->num_outputs
; slot
++) {
1803 switch (pPrevShader
->output_semantic_name
[slot
]) {
1804 case TGSI_SEMANTIC_PATCH
:
1807 case TGSI_SEMANTIC_GENERIC
:
1810 case TGSI_SEMANTIC_TESSINNER
:
1811 case TGSI_SEMANTIC_TESSOUTER
:
1814 case TGSI_SEMANTIC_POSITION
:
1815 case TGSI_SEMANTIC_CLIPDIST
:
1816 case TGSI_SEMANTIC_PSIZE
:
1820 assert(!"Unknown semantic input in TES");
1824 std::vector
<Constant
*> mapConstants
;
1825 Value
*vtxAttribMap
= ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
1826 Value
*patchAttribMap
= ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
1827 for (unsigned slot
= 0; slot
< info
->num_inputs
; slot
++) {
1828 ubyte semantic_name
= info
->input_semantic_name
[slot
];
1829 ubyte semantic_idx
= info
->input_semantic_index
[slot
];
1831 // Where in TCS output is my attribute?
1832 // TESS_TODO: revisit after implement pass-through TCS
1833 unsigned tcs_slot
= locate_linkage(semantic_name
, semantic_idx
, pPrevShader
);
1835 // Skip tessellation levels - these go to the tessellator, not TES
1836 switch (semantic_name
) {
1837 case TGSI_SEMANTIC_GENERIC
:
1838 tcs_slot
= tcs_slot
+ VERTEX_ATTRIB_START_SLOT
- sgvAttrs
- tessLevelAttrs
;
1840 case TGSI_SEMANTIC_PATCH
:
1841 tcs_slot
= semantic_idx
;
1843 case TGSI_SEMANTIC_POSITION
:
1844 tcs_slot
= VERTEX_POSITION_SLOT
;
1846 case TGSI_SEMANTIC_CLIPDIST
:
1847 case TGSI_SEMANTIC_PSIZE
:
1850 assert(!"Unexpected semantic found while builiding TES input map");
1852 if (semantic_name
== TGSI_SEMANTIC_PATCH
) {
1853 STORE(C(tcs_slot
), patchAttribMap
, {0, slot
});
1855 STORE(C(tcs_slot
), vtxAttribMap
, {0, slot
});
1857 mapConstants
.push_back(C(tcs_slot
));
1860 // Build execution mask
1861 struct lp_build_mask_context mask
;
1862 Value
*mask_val
= LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_mask
}, "tesMask");
1865 lp_build_print_value(gallivm
, "TES execution mask: ", wrap(mask_val
));
1867 lp_build_mask_begin(&mask
, gallivm
,
1868 lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
1870 struct swr_tes_llvm_iface tes_iface
;
1872 tes_iface
.base
.fetch_vertex_input
= ::swr_tes_llvm_fetch_vtx_input
;
1873 tes_iface
.base
.fetch_patch_input
= ::swr_tes_llvm_fetch_patch_input
;
1875 tes_iface
.pBuilder
= this;
1876 tes_iface
.pTesCtx
= pTesCtx
;
1877 tes_iface
.pTsState
= pTS
;
1878 tes_iface
.num_outputs
= tes
->info
.base
.num_outputs
;
1879 tes_iface
.info
= info
;
1880 tes_iface
.pVtxAttribMap
= vtxAttribMap
;
1881 tes_iface
.pPatchAttribMap
= patchAttribMap
;
1883 struct lp_build_tgsi_params params
;
1884 memset(¶ms
, 0, sizeof(params
));
1885 params
.type
= lp_type_float_vec(32, 32 * 8);
1886 params
.mask
= & mask
;
1887 params
.consts_ptr
= wrap(consts_ptr
);
1888 params
.const_sizes_ptr
= wrap(const_sizes_ptr
);
1889 params
.system_values
= &system_values
;
1890 params
.inputs
= inputs
;
1891 params
.context_ptr
= wrap(hPrivateData
);
1892 params
.sampler
= sampler
;
1893 params
.info
= &tes
->info
.base
;
1894 params
.tes_iface
= &tes_iface
.base
;
1897 lp_build_tgsi_soa(gallivm
,
1902 lp_build_mask_end(&mask
);
1904 sampler
->destroy(sampler
);
1906 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1908 // Write output attributes
1909 Value
*dclOut
= LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_pOutputData
}, "dclOut");
1911 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
1912 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1913 if (!outputs
[attrib
][channel
])
1916 Value
*val
= LOAD(unwrap(outputs
[attrib
][channel
]));;
1917 Value
*attribOffset
=
1918 LOAD(pTesCtx
, {0, SWR_DS_CONTEXT_outVertexAttribOffset
});
1920 // Assume we write possition
1921 Value
* outputSlot
= C(VERTEX_POSITION_SLOT
);
1922 if (tes
->info
.base
.output_semantic_name
[attrib
] != TGSI_SEMANTIC_POSITION
) {
1923 // No, it's a generic attribute, not a position - let's calculate output slot
1924 uint32_t outSlot
= attrib
;
1925 if (tes
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
) {
1926 // this shader will write position, so in shader's term
1927 // output starts at attrib 1, but we will handle that separately,
1928 // so let's fix the outSlot
1931 outputSlot
= ADD(attribOffset
, C(outSlot
));
1934 Value
*attribVecIndex
=
1935 ADD(MUL(vecStride
, MUL(outputSlot
, C(4))), vecOffset
);
1937 uint32_t outputComponent
= 0;
1938 uint32_t curComp
= outputComponent
+ channel
;
1939 auto outValIndex
= ADD(attribVecIndex
, MUL(vecStride
, C(curComp
)));
1940 STOREV(val
, dclOut
, {outValIndex
});
1942 if (verbose_shader
) {
1943 lp_build_printf(gallivm
,
1944 "TES output [%d][%d]",
1947 lp_build_print_value(gallivm
, " = ", wrap(val
));
1954 JM()->DumpToFile(pFunction
, "src");
1955 gallivm_verify_function(gallivm
, wrap(pFunction
));
1957 gallivm_compile_module(gallivm
);
1958 JM()->DumpToFile(pFunction
, "optimized");
1960 PFN_TES_FUNC pFunc
=
1961 (PFN_TES_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
1963 debug_printf("tess evaluation shader %p\n", pFunc
);
1964 assert(pFunc
&& "Error: TessEvaluationShader = NULL");
1966 JM()->DumpAsm(pFunction
, "asm");
1968 JM()->mIsModuleFinalized
= true;
1974 BuilderSWR::CompileTCS(struct swr_context
*ctx
, swr_jit_tcs_key
&key
)
1976 SWR_TS_STATE
*pTS
= &ctx
->tsState
;
1977 struct tgsi_shader_info
*info
= &ctx
->tcs
->info
.base
;
1979 pTS
->numHsInputAttribs
= info
->num_inputs
;
1980 pTS
->numHsOutputAttribs
= info
->num_outputs
;
1982 pTS
->hsAllocationSize
= sizeof(ScalarPatch
);
1984 pTS
->vertexAttribOffset
= VERTEX_ATTRIB_START_SLOT
;
1985 pTS
->srcVertexAttribOffset
= VERTEX_ATTRIB_START_SLOT
;
1987 struct swr_tess_control_shader
*tcs
= ctx
->tcs
;
1989 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1990 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1992 memset(outputs
, 0, sizeof(outputs
));
1994 AttrBuilder attrBuilder
;
1995 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
1997 std::vector
<Type
*> tcsArgs
{
1998 PointerType::get(Gen_swr_draw_context(JM()), 0),
1999 PointerType::get(mInt8Ty
, 0),
2000 PointerType::get(Gen_SWR_HS_CONTEXT(JM()), 0)};
2001 FunctionType
*tcsFuncType
=
2002 FunctionType::get(Type::getVoidTy(JM()->mContext
), tcsArgs
, false);
2004 // create new vertex shader function
2005 auto pFunction
= Function::Create(tcsFuncType
,
2006 GlobalValue::ExternalLinkage
,
2008 JM()->mpCurrentModule
);
2010 #if LLVM_VERSION_MAJOR < 5
2011 AttributeSet attrSet
= AttributeSet::get(
2012 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
2013 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
2015 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
2018 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
2019 IRB()->SetInsertPoint(block
);
2020 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
2022 auto argitr
= pFunction
->arg_begin();
2023 Value
*hPrivateData
= &*argitr
++;
2024 hPrivateData
->setName("hPrivateData");
2025 Value
*pWorkerData
= &*argitr
++;
2026 pWorkerData
->setName("pWorkerData");
2027 Value
*pTcsCtx
= &*argitr
++;
2028 pTcsCtx
->setName("tcsCtx");
2031 GEP(hPrivateData
, {C(0), C(swr_draw_context_constantTCS
)});
2032 consts_ptr
->setName("tcs_constants");
2033 Value
*const_sizes_ptr
=
2034 GEP(hPrivateData
, {0, swr_draw_context_num_constantsTCS
});
2035 const_sizes_ptr
->setName("num_tcs_constants");
2037 struct lp_build_sampler_soa
*sampler
=
2038 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_TESS_CTRL
);
2040 struct lp_bld_tgsi_system_values system_values
;
2041 memset(&system_values
, 0, sizeof(system_values
));
2043 system_values
.prim_id
=
2044 wrap(LOAD(pTcsCtx
, {0, SWR_HS_CONTEXT_PrimitiveID
}));
2046 Constant
*vInvocationId
;
2048 vInvocationId
= C({0, 1, 2, 3, 4, 5, 6, 7});
2051 C({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
2054 system_values
.invocation_id
= wrap(vInvocationId
);
2055 system_values
.vertices_in
= wrap(C(tcs
->vertices_per_patch
));
2057 if (verbose_shader
) {
2058 lp_build_print_value(gallivm
, "TCS::prim_id = ", system_values
.prim_id
);
2059 lp_build_print_value(gallivm
, "TCS::invocation_id = ", system_values
.invocation_id
);
2060 lp_build_print_value(gallivm
, "TCS::vertices_in = ", system_values
.vertices_in
);
2063 std::vector
<Constant
*> mapConstants
;
2064 Value
*vtxAttribMap
=
2065 ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
2067 for (unsigned slot
= 0; slot
< info
->num_inputs
; slot
++) {
2068 ubyte semantic_name
= info
->input_semantic_name
[slot
];
2069 ubyte semantic_idx
= info
->input_semantic_index
[slot
];
2072 locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
);
2074 vs_slot
+= VERTEX_ATTRIB_START_SLOT
;
2076 if (ctx
->vs
->info
.base
.output_semantic_name
[0]
2077 == TGSI_SEMANTIC_POSITION
)
2080 if (semantic_name
== TGSI_SEMANTIC_POSITION
)
2081 vs_slot
= VERTEX_POSITION_SLOT
;
2083 STORE(C(vs_slot
), vtxAttribMap
, {0, slot
});
2084 mapConstants
.push_back(C(vs_slot
));
2087 // Prepare map of output attributes. Needed when shader instance wants
2088 // to read own output or output of other instance, which is allowed in TCS
2089 Value
*vtxOutputAttribMap
=
2090 ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
2091 // Map for per-patch attributes
2092 Value
*patchOutputAttribMap
=
2093 ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
2094 for (unsigned slot
= 0; slot
< info
->num_outputs
; slot
++) {
2095 ubyte name
= info
->output_semantic_name
[slot
];
2096 int32_t idx
= info
->output_semantic_index
[slot
];
2097 if (name
== TGSI_SEMANTIC_PATCH
) {
2098 STORE(C(idx
), patchOutputAttribMap
, {0, slot
});
2100 int32_t target_slot
= slot
;
2101 if (name
== TGSI_SEMANTIC_GENERIC
) {
2102 target_slot
+= VERTEX_ATTRIB_START_SLOT
;
2104 // Now normalize target slot
2105 for (ubyte as
= 0; as
< slot
; as
++) {
2106 ubyte name
= info
->output_semantic_name
[as
];
2108 case TGSI_SEMANTIC_TESSOUTER
:
2109 case TGSI_SEMANTIC_TESSINNER
:
2110 case TGSI_SEMANTIC_PATCH
:
2111 case TGSI_SEMANTIC_POSITION
:
2115 if (name
== TGSI_SEMANTIC_POSITION
) {
2116 target_slot
= VERTEX_POSITION_SLOT
;
2118 STORE(C(target_slot
), vtxOutputAttribMap
, {0, slot
});
2119 mapConstants
.push_back(C(target_slot
));
2123 struct lp_build_mask_context mask
;
2124 Value
*mask_val
= LOAD(pTcsCtx
, {0, SWR_HS_CONTEXT_mask
}, "tcsMask");
2125 lp_build_mask_begin(
2126 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
2128 struct swr_tcs_llvm_iface tcs_iface
;
2130 tcs_iface
.base
.emit_store_output
= ::swr_tcs_llvm_store_output
;
2131 tcs_iface
.base
.emit_fetch_input
= ::swr_tcs_llvm_fetch_input
;
2132 tcs_iface
.base
.emit_fetch_output
= ::swr_tcs_llvm_fetch_output
;
2133 tcs_iface
.base
.emit_barrier
= ::swr_tcs_llvm_emit_barrier
;
2134 tcs_iface
.base
.emit_prologue
= ::swr_tcs_llvm_emit_prologue
;
2135 tcs_iface
.base
.emit_epilogue
= ::swr_tcs_llvm_emit_epilogue
;
2137 tcs_iface
.pBuilder
= this;
2138 tcs_iface
.pTcsCtx
= pTcsCtx
;
2139 tcs_iface
.pTsState
= pTS
;
2140 tcs_iface
.output_vertices
= info
->properties
[TGSI_PROPERTY_TCS_VERTICES_OUT
];
2141 tcs_iface
.info
= info
;
2142 tcs_iface
.pVtxAttribMap
= vtxAttribMap
;
2143 tcs_iface
.pVtxOutputAttribMap
= vtxOutputAttribMap
;
2144 tcs_iface
.pPatchOutputAttribMap
= patchOutputAttribMap
;
2146 struct lp_build_tgsi_params params
;
2147 memset(¶ms
, 0, sizeof(params
));
2148 params
.type
= lp_type_float_vec(32, 32 * 8);
2149 params
.mask
= &mask
;
2150 params
.consts_ptr
= wrap(consts_ptr
);
2151 params
.const_sizes_ptr
= wrap(const_sizes_ptr
);
2152 params
.system_values
= &system_values
;
2153 params
.inputs
= inputs
;
2154 params
.context_ptr
= wrap(hPrivateData
);
2155 params
.sampler
= sampler
;
2156 params
.info
= &tcs
->info
.base
;
2157 params
.tcs_iface
= &tcs_iface
.base
;
2159 lp_build_tgsi_soa(gallivm
, tcs
->pipe
.tokens
, ¶ms
, outputs
);
2161 lp_build_mask_end(&mask
);
2163 sampler
->destroy(sampler
);
2165 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
2168 JM()->DumpToFile(pFunction
, "src");
2169 gallivm_verify_function(gallivm
, wrap(pFunction
));
2170 gallivm_compile_module(gallivm
);
2171 JM()->DumpToFile(pFunction
, "optimized");
2173 PFN_TCS_FUNC pFunc
=
2174 (PFN_TCS_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
2176 debug_printf("tess control shader %p\n", pFunc
);
2177 assert(pFunc
&& "Error: TessControlShader = NULL");
2178 JM()->DumpAsm(pFunction
, "asm");
2180 JM()->mIsModuleFinalized
= true;
2187 swr_compile_gs(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
2190 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
2192 PFN_GS_FUNC func
= builder
.CompileGS(ctx
, key
);
2194 ctx
->gs
->map
.insert(std::make_pair(key
, std::make_unique
<VariantGS
>(builder
.gallivm
, func
)));
2199 swr_compile_tcs(struct swr_context
*ctx
, swr_jit_tcs_key
&key
)
2202 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
2204 PFN_TCS_FUNC func
= builder
.CompileTCS(ctx
, key
);
2206 ctx
->tcs
->map
.insert(
2207 std::make_pair(key
, std::make_unique
<VariantTCS
>(builder
.gallivm
, func
)));
2213 swr_compile_tes(struct swr_context
*ctx
, swr_jit_tes_key
&key
)
2216 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
2218 PFN_TES_FUNC func
= builder
.CompileTES(ctx
, key
);
2220 ctx
->tes
->map
.insert(
2221 std::make_pair(key
, std::make_unique
<VariantTES
>(builder
.gallivm
, func
)));
2227 BuilderSWR::WriteVS(Value
*pVal
, Value
*pVsContext
, Value
*pVtxOutput
, unsigned slot
, unsigned channel
)
2229 #if USE_SIMD16_FRONTEND && !USE_SIMD16_VS
2230 // interleave the simdvertex components into the dest simd16vertex
2231 // slot16offset = slot8offset * 2
2232 // comp16offset = comp8offset * 2 + alternateOffset
2234 Value
*offset
= LOAD(pVsContext
, { 0, SWR_VS_CONTEXT_AlternateOffset
});
2235 Value
*pOut
= GEP(pVtxOutput
, { C(0), C(0), C(slot
* 2), offset
} );
2236 STORE(pVal
, pOut
, {channel
* 2});
2238 Value
*pOut
= GEP(pVtxOutput
, {0, 0, slot
});
2239 STORE(pVal
, pOut
, {0, channel
});
2240 if (verbose_shader
) {
2241 lp_build_printf(gallivm
, "VS: Storing on slot %d, channel %d: ", C(slot
), C(channel
));
2242 lp_build_print_value(gallivm
, "", wrap(pVal
));
2248 BuilderSWR::CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
2250 struct swr_vertex_shader
*swr_vs
= ctx
->vs
;
2252 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
2253 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2255 memset(outputs
, 0, sizeof(outputs
));
2257 AttrBuilder attrBuilder
;
2258 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
2260 std::vector
<Type
*> vsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
2261 PointerType::get(mInt8Ty
, 0),
2262 PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};
2263 FunctionType
*vsFuncType
=
2264 FunctionType::get(Type::getVoidTy(JM()->mContext
), vsArgs
, false);
2266 // create new vertex shader function
2267 auto pFunction
= Function::Create(vsFuncType
,
2268 GlobalValue::ExternalLinkage
,
2270 JM()->mpCurrentModule
);
2271 #if LLVM_VERSION_MAJOR < 5
2272 AttributeSet attrSet
= AttributeSet::get(
2273 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
2274 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
2276 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
2279 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
2280 IRB()->SetInsertPoint(block
);
2281 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
2283 auto argitr
= pFunction
->arg_begin();
2284 Value
*hPrivateData
= &*argitr
++;
2285 hPrivateData
->setName("hPrivateData");
2286 Value
*pWorkerData
= &*argitr
++;
2287 pWorkerData
->setName("pWorkerData");
2288 Value
*pVsCtx
= &*argitr
++;
2289 pVsCtx
->setName("vsCtx");
2291 Value
*consts_ptr
= GEP(hPrivateData
, {C(0), C(swr_draw_context_constantVS
)});
2293 consts_ptr
->setName("vs_constants");
2294 Value
*const_sizes_ptr
=
2295 GEP(hPrivateData
, {0, swr_draw_context_num_constantsVS
});
2296 const_sizes_ptr
->setName("num_vs_constants");
2298 Value
*vtxInput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVin
});
2300 vtxInput
= BITCAST(vtxInput
, PointerType::get(Gen_simd16vertex(JM()), 0));
2303 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
2304 const unsigned mask
= swr_vs
->info
.base
.input_usage_mask
[attrib
];
2305 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
2306 if (mask
& (1 << channel
)) {
2307 inputs
[attrib
][channel
] =
2308 wrap(LOAD(vtxInput
, {0, 0, attrib
, channel
}));
2313 struct lp_build_sampler_soa
*sampler
=
2314 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_VERTEX
);
2316 struct lp_bld_tgsi_system_values system_values
;
2317 memset(&system_values
, 0, sizeof(system_values
));
2318 system_values
.instance_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_InstanceID
}));
2321 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID16
}));
2323 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID
}));
2327 uint32_t vectorWidth
= mVWidth16
;
2329 uint32_t vectorWidth
= mVWidth
;
2332 struct lp_build_tgsi_params params
;
2333 memset(¶ms
, 0, sizeof(params
));
2334 params
.type
= lp_type_float_vec(32, 32 * vectorWidth
);
2335 params
.consts_ptr
= wrap(consts_ptr
);
2336 params
.const_sizes_ptr
= wrap(const_sizes_ptr
);
2337 params
.system_values
= &system_values
;
2338 params
.inputs
= inputs
;
2339 params
.context_ptr
= wrap(hPrivateData
);
2340 params
.sampler
= sampler
;
2341 params
.info
= &swr_vs
->info
.base
;
2343 lp_build_tgsi_soa(gallivm
,
2344 swr_vs
->pipe
.tokens
,
2348 sampler
->destroy(sampler
);
2350 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
2352 Value
*vtxOutput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVout
});
2354 vtxOutput
= BITCAST(vtxOutput
, PointerType::get(Gen_simd16vertex(JM()), 0));
2357 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
2358 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
2359 if (!outputs
[attrib
][channel
])
2365 if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
2366 if (channel
!= VERTEX_SGV_POINT_SIZE_COMP
)
2368 val
= LOAD(unwrap(outputs
[attrib
][0]));
2369 outSlot
= VERTEX_SGV_SLOT
;
2370 } else if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
2371 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
2372 outSlot
= VERTEX_POSITION_SLOT
;
2374 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
2375 outSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
;
2376 if (swr_vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
2380 WriteVS(val
, pVsCtx
, vtxOutput
, outSlot
, channel
);
2384 if (ctx
->rasterizer
->clip_plane_enable
||
2385 swr_vs
->info
.base
.culldist_writemask
) {
2386 unsigned clip_mask
= ctx
->rasterizer
->clip_plane_enable
;
2389 if (swr_vs
->info
.base
.writes_clipvertex
) {
2390 cv
= locate_linkage(TGSI_SEMANTIC_CLIPVERTEX
, 0,
2391 &swr_vs
->info
.base
);
2393 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
2394 if (swr_vs
->info
.base
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
2395 swr_vs
->info
.base
.output_semantic_index
[i
] == 0) {
2401 LLVMValueRef cx
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][0], "");
2402 LLVMValueRef cy
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][1], "");
2403 LLVMValueRef cz
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][2], "");
2404 LLVMValueRef cw
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][3], "");
2406 tgsi_shader_info
*pLastFE
= &ctx
->vs
->info
.base
;
2409 pLastFE
= &ctx
->gs
->info
.base
;
2411 else if (ctx
->tes
) {
2412 pLastFE
= &ctx
->tes
->info
.base
;
2414 else if (ctx
->tcs
) {
2415 pLastFE
= &ctx
->tcs
->info
.base
;
2418 for (unsigned val
= 0; val
< PIPE_MAX_CLIP_PLANES
; val
++) {
2419 // clip distance overrides user clip planes
2420 if ((pLastFE
->clipdist_writemask
& clip_mask
& (1 << val
)) ||
2421 ((pLastFE
->culldist_writemask
<< pLastFE
->num_written_clipdistance
) & (1 << val
))) {
2422 unsigned cv
= locate_linkage(TGSI_SEMANTIC_CLIPDIST
, val
< 4 ? 0 : 1, pLastFE
);
2424 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
], "");
2425 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
2427 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
- 4], "");
2428 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
2433 if (!(clip_mask
& (1 << val
)))
2436 Value
*px
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 0}));
2437 Value
*py
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 1}));
2438 Value
*pz
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 2}));
2439 Value
*pw
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 3}));
2441 Value
*bpx
= VBROADCAST_16(px
);
2442 Value
*bpy
= VBROADCAST_16(py
);
2443 Value
*bpz
= VBROADCAST_16(pz
);
2444 Value
*bpw
= VBROADCAST_16(pw
);
2446 Value
*bpx
= VBROADCAST(px
);
2447 Value
*bpy
= VBROADCAST(py
);
2448 Value
*bpz
= VBROADCAST(pz
);
2449 Value
*bpw
= VBROADCAST(pw
);
2451 Value
*dist
= FADD(FMUL(unwrap(cx
), bpx
),
2452 FADD(FMUL(unwrap(cy
), bpy
),
2453 FADD(FMUL(unwrap(cz
), bpz
),
2454 FMUL(unwrap(cw
), bpw
))));
2457 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
2459 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
2465 JM()->DumpToFile(pFunction
, "vs_function1");
2466 gallivm_verify_function(gallivm
, wrap(pFunction
));
2467 gallivm_compile_module(gallivm
);
2468 JM()->DumpToFile(pFunction
, "vs_function2");
2470 // lp_debug_dump_value(func);
2472 PFN_VERTEX_FUNC pFunc
=
2473 (PFN_VERTEX_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
2475 JM()->DumpAsm(pFunction
, "vs_function_asm");
2476 debug_printf("vert shader %p\n", pFunc
);
2477 assert(pFunc
&& "Error: VertShader = NULL");
2479 JM()->mIsModuleFinalized
= true;
2485 swr_compile_vs(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
2487 if (!ctx
->vs
->pipe
.tokens
)
2491 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
2493 PFN_VERTEX_FUNC func
= builder
.CompileVS(ctx
, key
);
2495 ctx
->vs
->map
.insert(std::make_pair(key
, std::make_unique
<VariantVS
>(builder
.gallivm
, func
)));
2500 swr_so_adjust_attrib(unsigned in_attrib
,
2501 swr_vertex_shader
*swr_vs
)
2503 ubyte semantic_name
;
2506 attrib
= in_attrib
+ VERTEX_ATTRIB_START_SLOT
;
2509 semantic_name
= swr_vs
->info
.base
.output_semantic_name
[in_attrib
];
2510 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
2511 attrib
= VERTEX_POSITION_SLOT
;
2512 } else if (semantic_name
== TGSI_SEMANTIC_PSIZE
) {
2513 attrib
= VERTEX_SGV_SLOT
;
2514 } else if (semantic_name
== TGSI_SEMANTIC_LAYER
) {
2515 attrib
= VERTEX_SGV_SLOT
;
2517 if (swr_vs
->info
.base
.writes_position
) {
2527 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
)
2529 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
2530 if ((info
->output_semantic_name
[i
] == name
)
2531 && (info
->output_semantic_index
[i
] == index
)) {
2540 BuilderSWR::CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
2542 struct swr_fragment_shader
*swr_fs
= ctx
->fs
;
2544 struct tgsi_shader_info
*pPrevShader
;
2546 pPrevShader
= &ctx
->gs
->info
.base
;
2548 pPrevShader
= &ctx
->tes
->info
.base
;
2550 pPrevShader
= &ctx
->vs
->info
.base
;
2552 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
2553 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
2555 memset(inputs
, 0, sizeof(inputs
));
2556 memset(outputs
, 0, sizeof(outputs
));
2558 struct lp_build_sampler_soa
*sampler
= NULL
;
2560 AttrBuilder attrBuilder
;
2561 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
2563 std::vector
<Type
*> fsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
2564 PointerType::get(mInt8Ty
, 0),
2565 PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};
2566 FunctionType
*funcType
=
2567 FunctionType::get(Type::getVoidTy(JM()->mContext
), fsArgs
, false);
2569 auto pFunction
= Function::Create(funcType
,
2570 GlobalValue::ExternalLinkage
,
2572 JM()->mpCurrentModule
);
2573 #if LLVM_VERSION_MAJOR < 5
2574 AttributeSet attrSet
= AttributeSet::get(
2575 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
2576 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
2578 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
2581 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
2582 IRB()->SetInsertPoint(block
);
2583 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
2585 auto args
= pFunction
->arg_begin();
2586 Value
*hPrivateData
= &*args
++;
2587 hPrivateData
->setName("hPrivateData");
2588 Value
*pWorkerData
= &*args
++;
2589 pWorkerData
->setName("pWorkerData");
2590 Value
*pPS
= &*args
++;
2591 pPS
->setName("psCtx");
2593 Value
*consts_ptr
= GEP(hPrivateData
, {0, swr_draw_context_constantFS
});
2594 consts_ptr
->setName("fs_constants");
2595 Value
*const_sizes_ptr
=
2596 GEP(hPrivateData
, {0, swr_draw_context_num_constantsFS
});
2597 const_sizes_ptr
->setName("num_fs_constants");
2599 // load *pAttribs, *pPerspAttribs
2600 Value
*pRawAttribs
= LOAD(pPS
, {0, SWR_PS_CONTEXT_pAttribs
}, "pRawAttribs");
2601 Value
*pPerspAttribs
=
2602 LOAD(pPS
, {0, SWR_PS_CONTEXT_pPerspAttribs
}, "pPerspAttribs");
2604 swr_fs
->constantMask
= 0;
2605 swr_fs
->flatConstantMask
= 0;
2606 swr_fs
->pointSpriteMask
= 0;
2608 for (int attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
2609 const unsigned mask
= swr_fs
->info
.base
.input_usage_mask
[attrib
];
2610 const unsigned interpMode
= swr_fs
->info
.base
.input_interpolate
[attrib
];
2611 const unsigned interpLoc
= swr_fs
->info
.base
.input_interpolate_loc
[attrib
];
2617 Value
*vi
= nullptr, *vj
= nullptr;
2618 switch (interpLoc
) {
2619 case TGSI_INTERPOLATE_LOC_CENTER
:
2620 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_center
}, "i");
2621 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_center
}, "j");
2623 case TGSI_INTERPOLATE_LOC_CENTROID
:
2624 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_centroid
}, "i");
2625 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_centroid
}, "j");
2627 case TGSI_INTERPOLATE_LOC_SAMPLE
:
2628 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_sample
}, "i");
2629 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_sample
}, "j");
2634 Value
*vw
= nullptr, *pAttribs
;
2635 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
2636 interpMode
== TGSI_INTERPOLATE_COLOR
) {
2637 pAttribs
= pPerspAttribs
;
2638 switch (interpLoc
) {
2639 case TGSI_INTERPOLATE_LOC_CENTER
:
2640 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}));
2642 case TGSI_INTERPOLATE_LOC_CENTROID
:
2643 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_centroid
}));
2645 case TGSI_INTERPOLATE_LOC_SAMPLE
:
2646 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_sample
}));
2650 pAttribs
= pRawAttribs
;
2656 ubyte semantic_name
= swr_fs
->info
.base
.input_semantic_name
[attrib
];
2657 ubyte semantic_idx
= swr_fs
->info
.base
.input_semantic_index
[attrib
];
2659 if (semantic_name
== TGSI_SEMANTIC_FACE
) {
2661 UI_TO_FP(LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), mFP32Ty
);
2662 ff
= FSUB(FMUL(ff
, C(2.0f
)), C(1.0f
));
2663 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vFrontFace");
2665 inputs
[attrib
][0] = wrap(ff
);
2666 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
2667 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
2668 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
2670 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
) { // gl_FragCoord
2671 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
2672 TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
) {
2673 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_center
}, "vX"));
2674 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_center
}, "vY"));
2676 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
}, "vX"));
2677 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
}, "vY"));
2679 inputs
[attrib
][2] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vZ
}, "vZ"));
2681 wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}, "vOneOverW"));
2683 } else if (semantic_name
== TGSI_SEMANTIC_LAYER
) { // gl_Layer
2684 Value
*ff
= LOAD(pPS
, {0, SWR_PS_CONTEXT_renderTargetArrayIndex
});
2685 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vRenderTargetArrayIndex");
2686 inputs
[attrib
][0] = wrap(ff
);
2687 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
2688 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
2689 inputs
[attrib
][3] = wrap(VIMMED1(0.0f
));
2691 } else if (semantic_name
== TGSI_SEMANTIC_VIEWPORT_INDEX
) { // gl_ViewportIndex
2692 Value
*ff
= LOAD(pPS
, {0, SWR_PS_CONTEXT_viewportIndex
});
2693 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vViewportIndex");
2694 inputs
[attrib
][0] = wrap(ff
);
2695 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
2696 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
2697 inputs
[attrib
][3] = wrap(VIMMED1(0.0f
));
2700 unsigned linkedAttrib
=
2701 locate_linkage(semantic_name
, semantic_idx
, pPrevShader
) - 1;
2703 uint32_t extraAttribs
= 0;
2704 if (semantic_name
== TGSI_SEMANTIC_PRIMID
&& !ctx
->gs
) {
2705 /* non-gs generated primID - need to grab from swizzleMap override */
2706 linkedAttrib
= pPrevShader
->num_outputs
- 1;
2707 swr_fs
->constantMask
|= 1 << linkedAttrib
;
2709 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
2710 key
.sprite_coord_enable
& (1 << semantic_idx
)) {
2711 /* we add an extra attrib to the backendState in swr_update_derived. */
2712 linkedAttrib
= pPrevShader
->num_outputs
+ extraAttribs
- 1;
2713 swr_fs
->pointSpriteMask
|= (1 << linkedAttrib
);
2715 } else if (linkedAttrib
== 0xFFFFFFFF) {
2716 inputs
[attrib
][0] = wrap(VIMMED1(0.0f
));
2717 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
2718 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
2719 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
2720 /* If we're reading in color and 2-sided lighting is enabled, we have
2723 if (semantic_name
!= TGSI_SEMANTIC_COLOR
|| !key
.light_twoside
)
2726 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
2727 swr_fs
->constantMask
|= 1 << linkedAttrib
;
2728 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
2729 swr_fs
->flatConstantMask
|= 1 << linkedAttrib
;
2733 unsigned bcolorAttrib
= 0xFFFFFFFF;
2734 Value
*offset
= NULL
;
2735 if (semantic_name
== TGSI_SEMANTIC_COLOR
&& key
.light_twoside
) {
2736 bcolorAttrib
= locate_linkage(
2737 TGSI_SEMANTIC_BCOLOR
, semantic_idx
, pPrevShader
) - 1;
2738 /* Neither front nor back colors were available. Nothing to load. */
2739 if (bcolorAttrib
== 0xFFFFFFFF && linkedAttrib
== 0xFFFFFFFF)
2741 /* If there is no front color, just always use the back color. */
2742 if (linkedAttrib
== 0xFFFFFFFF)
2743 linkedAttrib
= bcolorAttrib
;
2745 if (bcolorAttrib
!= 0xFFFFFFFF) {
2746 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
2747 swr_fs
->constantMask
|= 1 << bcolorAttrib
;
2748 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
2749 swr_fs
->flatConstantMask
|= 1 << bcolorAttrib
;
2752 unsigned diff
= 12 * (bcolorAttrib
- linkedAttrib
);
2756 XOR(C(1), LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), "backFace");
2758 offset
= MUL(back
, C(diff
));
2759 offset
->setName("offset");
2764 for (int channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
2765 if (mask
& (1 << channel
)) {
2766 Value
*indexA
= C(linkedAttrib
* 12 + channel
);
2767 Value
*indexB
= C(linkedAttrib
* 12 + channel
+ 4);
2768 Value
*indexC
= C(linkedAttrib
* 12 + channel
+ 8);
2771 indexA
= ADD(indexA
, offset
);
2772 indexB
= ADD(indexB
, offset
);
2773 indexC
= ADD(indexC
, offset
);
2776 Value
*va
= VBROADCAST(LOAD(GEP(pAttribs
, indexA
)));
2777 Value
*vb
= VBROADCAST(LOAD(GEP(pAttribs
, indexB
)));
2778 Value
*vc
= VBROADCAST(LOAD(GEP(pAttribs
, indexC
)));
2780 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
2781 inputs
[attrib
][channel
] = wrap(va
);
2783 Value
*vk
= FSUB(FSUB(VIMMED1(1.0f
), vi
), vj
);
2787 Value
*interp
= FMUL(va
, vi
);
2788 Value
*interp1
= FMUL(vb
, vj
);
2789 interp
= FADD(interp
, interp1
);
2790 interp
= FADD(interp
, vc
);
2791 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
2792 interpMode
== TGSI_INTERPOLATE_COLOR
)
2793 interp
= FMUL(interp
, vw
);
2794 inputs
[attrib
][channel
] = wrap(interp
);
2800 sampler
= swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_FRAGMENT
);
2802 struct lp_bld_tgsi_system_values system_values
;
2803 memset(&system_values
, 0, sizeof(system_values
));
2805 struct lp_build_mask_context mask
;
2806 bool uses_mask
= false;
2808 if (swr_fs
->info
.base
.uses_kill
||
2809 key
.poly_stipple_enable
) {
2810 Value
*vActiveMask
= NULL
;
2811 if (swr_fs
->info
.base
.uses_kill
) {
2812 vActiveMask
= LOAD(pPS
, {0, SWR_PS_CONTEXT_activeMask
}, "activeMask");
2814 if (key
.poly_stipple_enable
) {
2815 // first get fragment xy coords and clip to stipple bounds
2816 Value
*vXf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
});
2817 Value
*vYf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
});
2818 Value
*vXu
= FP_TO_UI(vXf
, mSimdInt32Ty
);
2819 Value
*vYu
= FP_TO_UI(vYf
, mSimdInt32Ty
);
2821 // stipple pattern is 32x32, which means that one line of stipple
2822 // is stored in one word:
2823 // vXstipple is bit offset inside 32-bit stipple word
2824 // vYstipple is word index is stipple array
2825 Value
*vXstipple
= AND(vXu
, VIMMED1(0x1f)); // & (32-1)
2826 Value
*vYstipple
= AND(vYu
, VIMMED1(0x1f)); // & (32-1)
2828 // grab stipple pattern base address
2829 Value
*stipplePtr
= GEP(hPrivateData
, {0, swr_draw_context_polyStipple
, 0});
2830 stipplePtr
= BITCAST(stipplePtr
, mInt8PtrTy
);
2832 // peform a gather to grab stipple words for each lane
2833 Value
*vStipple
= GATHERDD(VUNDEF_I(), stipplePtr
, vYstipple
,
2834 VIMMED1(0xffffffff), 4);
2836 // create a mask with one bit corresponding to the x stipple
2837 // and AND it with the pattern, to see if we have a bit
2838 Value
*vBitMask
= LSHR(VIMMED1(0x80000000), vXstipple
);
2839 Value
*vStippleMask
= AND(vStipple
, vBitMask
);
2840 vStippleMask
= ICMP_NE(vStippleMask
, VIMMED1(0));
2841 vStippleMask
= VMASK(vStippleMask
);
2843 if (swr_fs
->info
.base
.uses_kill
) {
2844 vActiveMask
= AND(vActiveMask
, vStippleMask
);
2846 vActiveMask
= vStippleMask
;
2849 lp_build_mask_begin(
2850 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(vActiveMask
));
2854 struct lp_build_tgsi_params params
;
2855 memset(¶ms
, 0, sizeof(params
));
2856 params
.type
= lp_type_float_vec(32, 32 * 8);
2857 params
.mask
= uses_mask
? &mask
: NULL
;
2858 params
.consts_ptr
= wrap(consts_ptr
);
2859 params
.const_sizes_ptr
= wrap(const_sizes_ptr
);
2860 params
.system_values
= &system_values
;
2861 params
.inputs
= inputs
;
2862 params
.context_ptr
= wrap(hPrivateData
);
2863 params
.sampler
= sampler
;
2864 params
.info
= &swr_fs
->info
.base
;
2866 lp_build_tgsi_soa(gallivm
,
2867 swr_fs
->pipe
.tokens
,
2871 sampler
->destroy(sampler
);
2873 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
2875 for (uint32_t attrib
= 0; attrib
< swr_fs
->info
.base
.num_outputs
;
2877 switch (swr_fs
->info
.base
.output_semantic_name
[attrib
]) {
2878 case TGSI_SEMANTIC_POSITION
: {
2881 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][2], "");
2882 STORE(unwrap(outZ
), pPS
, {0, SWR_PS_CONTEXT_vZ
});
2885 case TGSI_SEMANTIC_COLOR
: {
2886 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
2887 if (!outputs
[attrib
][channel
])
2891 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][channel
], "");
2892 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
2893 swr_fs
->info
.base
.output_semantic_index
[attrib
] == 0) {
2894 for (uint32_t rt
= 0; rt
< key
.nr_cbufs
; rt
++) {
2897 {0, SWR_PS_CONTEXT_shaded
, rt
, channel
});
2903 SWR_PS_CONTEXT_shaded
,
2904 swr_fs
->info
.base
.output_semantic_index
[attrib
],
2912 "unknown output from FS %s[%d]\n",
2913 tgsi_semantic_names
[swr_fs
->info
.base
2914 .output_semantic_name
[attrib
]],
2915 swr_fs
->info
.base
.output_semantic_index
[attrib
]);
2921 LLVMValueRef mask_result
= 0;
2923 mask_result
= lp_build_mask_end(&mask
);
2926 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
2929 STORE(unwrap(mask_result
), pPS
, {0, SWR_PS_CONTEXT_activeMask
});
2934 gallivm_verify_function(gallivm
, wrap(pFunction
));
2936 gallivm_compile_module(gallivm
);
2938 // after the gallivm passes, we have to lower the core's intrinsics
2939 llvm::legacy::FunctionPassManager
lowerPass(JM()->mpCurrentModule
);
2940 lowerPass
.add(createLowerX86Pass(this));
2941 lowerPass
.run(*pFunction
);
2943 PFN_PIXEL_KERNEL kernel
=
2944 (PFN_PIXEL_KERNEL
)gallivm_jit_function(gallivm
, wrap(pFunction
));
2945 debug_printf("frag shader %p\n", kernel
);
2946 assert(kernel
&& "Error: FragShader = NULL");
2948 JM()->mIsModuleFinalized
= true;
2954 swr_compile_fs(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
2956 if (!ctx
->fs
->pipe
.tokens
)
2960 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
2962 PFN_PIXEL_KERNEL func
= builder
.CompileFS(ctx
, key
);
2964 ctx
->fs
->map
.insert(std::make_pair(key
, std::make_unique
<VariantFS
>(builder
.gallivm
, func
)));