gallium/swr: fix gcc warnings
[mesa.git] / src / gallium / drivers / swr / swr_shader.cpp
1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 ***************************************************************************/
23
24 #include <llvm/Config/llvm-config.h>
25
26 #if LLVM_VERSION_MAJOR < 7
27 // llvm redefines DEBUG
28 #pragma push_macro("DEBUG")
29 #undef DEBUG
30 #endif
31
32 #include "JitManager.h"
33 #include "llvm-c/Core.h"
34 #include "llvm/Support/CBindingWrapping.h"
35 #include "llvm/IR/LegacyPassManager.h"
36
37 #if LLVM_VERSION_MAJOR < 7
38 #pragma pop_macro("DEBUG")
39 #endif
40
41 #include "state.h"
42 #include "gen_state_llvm.h"
43 #include "builder.h"
44 #include "functionpasses/passes.h"
45
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"
55
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"
62
63
64 /////////////////////////////////////////////////////////////////////////
65
66 #include <stdio.h>
67 #include <inttypes.h>
68
69 #include "util/u_debug.h"
70 #include "util/u_memory.h"
71 #include "util/u_string.h"
72
73 #include "gallivm/lp_bld_type.h"
74
75 #ifdef DEBUG
76 constexpr bool verbose_shader = true;
77 #else
78 constexpr bool verbose_shader = false;
79 #endif
80
81 using namespace SwrJit;
82 using namespace llvm;
83
84 static unsigned
85 locate_linkage(ubyte name, ubyte index, struct tgsi_shader_info *info);
86
87 bool operator==(const swr_jit_fs_key &lhs, const swr_jit_fs_key &rhs)
88 {
89 return !memcmp(&lhs, &rhs, sizeof(lhs));
90 }
91
92 bool operator==(const swr_jit_vs_key &lhs, const swr_jit_vs_key &rhs)
93 {
94 return !memcmp(&lhs, &rhs, sizeof(lhs));
95 }
96
97 bool operator==(const swr_jit_fetch_key &lhs, const swr_jit_fetch_key &rhs)
98 {
99 return !memcmp(&lhs, &rhs, sizeof(lhs));
100 }
101
102 bool operator==(const swr_jit_gs_key &lhs, const swr_jit_gs_key &rhs)
103 {
104 return !memcmp(&lhs, &rhs, sizeof(lhs));
105 }
106
107 bool operator==(const swr_jit_tcs_key &lhs, const swr_jit_tcs_key &rhs)
108 {
109 return !memcmp(&lhs, &rhs, sizeof(lhs));
110 }
111
112 bool operator==(const swr_jit_tes_key &lhs, const swr_jit_tes_key &rhs)
113 {
114 return !memcmp(&lhs, &rhs, sizeof(lhs));
115 }
116
117
118 static void
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)
123 {
124 key.nr_samplers = info.base.file_max[TGSI_FILE_SAMPLER] + 1;
125
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]);
131 }
132 }
133
134 /*
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).
138 */
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);
148 if (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;
155 }
156 }
157 }
158 } else {
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);
166 if (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;
173 }
174 }
175 }
176 }
177 }
178
179 void
180 swr_generate_fs_key(struct swr_jit_fs_key &key,
181 struct swr_context *ctx,
182 swr_fragment_shader *swr_fs)
183 {
184 memset(&key, 0, sizeof(key));
185
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;
189
190 struct tgsi_shader_info *pPrevShader;
191 if (ctx->gs)
192 pPrevShader = &ctx->gs->info.base;
193 else if (ctx->tes)
194 pPrevShader = &ctx->tes->info.base;
195 else
196 pPrevShader = &ctx->vs->info.base;
197
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));
204
205 swr_generate_sampler_key(swr_fs->info, ctx, PIPE_SHADER_FRAGMENT, key);
206
207 key.poly_stipple_enable = ctx->rasterizer->poly_stipple_enable &&
208 ctx->poly_stipple.prim_is_poly;
209 }
210
211 void
212 swr_generate_vs_key(struct swr_jit_vs_key &key,
213 struct swr_context *ctx,
214 swr_vertex_shader *swr_vs)
215 {
216 memset(&key, 0, sizeof(key));
217
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;
222
223 swr_generate_sampler_key(swr_vs->info, ctx, PIPE_SHADER_VERTEX, key);
224 }
225
226 void
227 swr_generate_fetch_key(struct swr_jit_fetch_key &key,
228 struct swr_vertex_element_state *velems)
229 {
230 memset(&key, 0, sizeof(key));
231
232 key.fsState = velems->fsState;
233 }
234
235 void
236 swr_generate_gs_key(struct swr_jit_gs_key &key,
237 struct swr_context *ctx,
238 swr_geometry_shader *swr_gs)
239 {
240 memset(&key, 0, sizeof(key));
241
242 struct tgsi_shader_info *pPrevShader = nullptr;
243
244 if (ctx->tes) {
245 pPrevShader = &ctx->tes->info.base;
246 } else {
247 pPrevShader = &ctx->vs->info.base;
248 }
249
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));
256
257 swr_generate_sampler_key(swr_gs->info, ctx, PIPE_SHADER_GEOMETRY, key);
258 }
259
260 void
261 swr_generate_tcs_key(struct swr_jit_tcs_key &key,
262 struct swr_context *ctx,
263 swr_tess_control_shader *swr_tcs)
264 {
265 memset(&key, 0, sizeof(key));
266
267 struct tgsi_shader_info *pPrevShader = &ctx->vs->info.base;
268
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));
275
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;
280
281 swr_generate_sampler_key(swr_tcs->info, ctx, PIPE_SHADER_TESS_CTRL, key);
282 }
283
284 void
285 swr_generate_tes_key(struct swr_jit_tes_key &key,
286 struct swr_context *ctx,
287 swr_tess_evaluation_shader *swr_tes)
288 {
289 memset(&key, 0, sizeof(key));
290
291 struct tgsi_shader_info *pPrevShader = nullptr;
292
293 if (ctx->tcs) {
294 pPrevShader = &ctx->tcs->info.base;
295 }
296 else {
297 pPrevShader = &ctx->vs->info.base;
298 }
299
300 SWR_ASSERT(pPrevShader != nullptr, "TES: No TCS or VS defined");
301
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));
308
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;
313
314 swr_generate_sampler_key(swr_tes->info, ctx, PIPE_SHADER_TESS_EVAL, key);
315 }
316
317 struct BuilderSWR : public Builder {
318 BuilderSWR(JitManager *pJitMgr, const char *pName)
319 : Builder(pJitMgr)
320 {
321 pJitMgr->SetupNewModule();
322 gallivm = gallivm_create(pName, wrap(&JM()->mContext));
323 pJitMgr->mpCurrentModule = unwrap(gallivm->module);
324 }
325
326 ~BuilderSWR() {
327 gallivm_free_ir(gallivm);
328 }
329
330 void WriteVS(Value *pVal, Value *pVsContext, Value *pVtxOutput,
331 unsigned slot, unsigned channel);
332
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);
339
340 // GS-specific emit functions
341 LLVMValueRef
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);
349 void
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);
355
356 void
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);
363
364 void
365 swr_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base,
366 LLVMValueRef total_emitted_vertices_vec,
367 LLVMValueRef emitted_prims_vec);
368
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);
372
373 LLVMValueRef
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);
381
382 LLVMValueRef
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,
390 uint32_t name);
391
392 void
393 swr_tcs_llvm_store_output(const struct lp_build_tcs_iface *tcs_iface,
394 struct lp_build_tgsi_context * bld_base,
395 unsigned name,
396 boolean is_vindex_indirect,
397 LLVMValueRef vertex_index,
398 boolean is_aindex_indirect,
399 LLVMValueRef attrib_index,
400 LLVMValueRef swizzle_index,
401 LLVMValueRef value);
402
403 // Barrier implementation (available only in TCS)
404 void
405 swr_tcs_llvm_emit_barrier(const struct lp_build_tcs_iface *tcs_iface,
406 struct lp_build_tgsi_context *bld_base);
407
408 // TES-specific emit functions
409 LLVMValueRef
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);
417
418 LLVMValueRef
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);
424 };
425
426 struct swr_gs_llvm_iface {
427 struct lp_build_gs_iface base;
428 struct tgsi_shader_info *info;
429
430 BuilderSWR *pBuilder;
431
432 Value *pGsCtx;
433 SWR_GS_STATE *pGsState;
434 uint32_t num_outputs;
435 uint32_t num_verts_per_prim;
436
437 Value *pVtxAttribMap;
438 };
439
440 struct swr_tcs_llvm_iface {
441 struct lp_build_tcs_iface base;
442 struct tgsi_shader_info *info;
443
444 BuilderSWR *pBuilder;
445
446 Value *pTcsCtx;
447 SWR_TS_STATE *pTsState;
448
449 uint32_t output_vertices;
450
451 struct lp_build_for_loop_state loop_state;
452
453 Value *pVtxAttribMap;
454 Value *pVtxOutputAttribMap;
455 Value *pPatchOutputAttribMap;
456 };
457
458 struct swr_tes_llvm_iface {
459 struct lp_build_tes_iface base;
460 struct tgsi_shader_info *info;
461
462 BuilderSWR *pBuilder;
463
464 Value *pTesCtx;
465 SWR_TS_STATE *pTsState;
466
467 uint32_t num_outputs;
468
469 Value *pVtxAttribMap;
470 Value *pPatchAttribMap;
471 };
472
473 // trampoline functions so we can use the builder llvm construction methods
474 static LLVMValueRef
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)
482 {
483 swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_iface;
484
485 return iface->pBuilder->swr_gs_llvm_fetch_input(gs_iface, bld,
486 is_vindex_indirect,
487 vertex_index,
488 is_aindex_indirect,
489 attrib_index,
490 swizzle_index);
491 }
492
493 static void
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)
499 {
500 swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
501
502 iface->pBuilder->swr_gs_llvm_emit_vertex(gs_base, bld,
503 outputs,
504 emitted_vertices_vec,
505 stream_id);
506 }
507
508 static void
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)
515 {
516 swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
517
518 iface->pBuilder->swr_gs_llvm_end_primitive(gs_base, bld,
519 total_emitted_vertices_vec_ptr,
520 verts_per_prim_vec,
521 emitted_prims_vec,
522 mask_vec);
523 }
524
525 static void
526 swr_gs_llvm_epilogue(const struct lp_build_gs_iface *gs_base,
527 LLVMValueRef total_emitted_vertices_vec,
528 LLVMValueRef emitted_prims_vec)
529 {
530 swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
531
532 iface->pBuilder->swr_gs_llvm_epilogue(gs_base,
533 total_emitted_vertices_vec,
534 emitted_prims_vec);
535 }
536
537 static LLVMValueRef
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)
545 {
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;
548
549 return iface->pBuilder->swr_tcs_llvm_fetch_input(tcs_iface, bld_base,
550 is_vindex_indirect,
551 vertex_index,
552 is_aindex_indirect,
553 attrib_index,
554 swizzle_index);
555 }
556
557 static LLVMValueRef
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,
565 uint32_t name)
566 {
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;
569
570 return iface->pBuilder->swr_tcs_llvm_fetch_output(tcs_iface, bld_base,
571 is_vindex_indirect,
572 vertex_index,
573 is_aindex_indirect,
574 attrib_index,
575 swizzle_index,
576 name);
577 }
578
579
580 static void
581 swr_tcs_llvm_emit_prologue(struct lp_build_context* bld)
582 {
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);
586 }
587
588 static void
589 swr_tcs_llvm_emit_epilogue(struct lp_build_context* bld)
590 {
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);
594 }
595
596 static
597 void swr_tcs_llvm_store_output(const struct lp_build_tcs_iface *tcs_iface,
598 struct lp_build_context * bld,
599 unsigned name,
600 boolean is_vindex_indirect,
601 LLVMValueRef vertex_index,
602 boolean is_aindex_indirect,
603 LLVMValueRef attrib_index,
604 LLVMValueRef swizzle_index,
605 LLVMValueRef value)
606 {
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;
609
610 iface->pBuilder->swr_tcs_llvm_store_output(tcs_iface,
611 bld_base,
612 name,
613 is_vindex_indirect,
614 vertex_index,
615 is_aindex_indirect,
616 attrib_index,
617 swizzle_index,
618 value);
619 }
620
621
622 static
623 void swr_tcs_llvm_emit_barrier(struct lp_build_context *bld)
624 {
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;
627
628 iface->pBuilder->swr_tcs_llvm_emit_barrier(bld_base->tcs_iface, &bld_base->bld_base);
629 }
630
631
632 static LLVMValueRef
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)
640 {
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;
643
644 return iface->pBuilder->swr_tes_llvm_fetch_vtx_input(tes_iface, bld_base,
645 is_vindex_indirect,
646 vertex_index,
647 is_aindex_indirect,
648 attrib_index,
649 swizzle_index);
650 }
651
652 static LLVMValueRef
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)
658 {
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;
661
662 return iface->pBuilder->swr_tes_llvm_fetch_patch_input(tes_iface, bld_base,
663 is_aindex_indirect,
664 attrib_index,
665 swizzle_index);
666 }
667
668 LLVMValueRef
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)
676 {
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);
680
681 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
682
683 if (is_vindex_indirect || is_aindex_indirect) {
684 int i;
685 Value *res = unwrap(bld->zero);
686 struct lp_type type = bld->type;
687
688 for (i = 0; i < type.length; i++) {
689 Value *vert_chan_index = vert_index;
690 Value *attr_chan_index = attr_index;
691
692 if (is_vindex_indirect) {
693 vert_chan_index = VEXTRACT(vert_index, C(i));
694 }
695 if (is_aindex_indirect) {
696 attr_chan_index = VEXTRACT(attr_index, C(i));
697 }
698
699 Value *attrib =
700 LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_chan_index}));
701
702 Value *pVertex = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pVerts});
703 Value *pInputVertStride = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_inputVertStride});
704
705 Value *pVector = ADD(MUL(vert_chan_index, pInputVertStride), attrib);
706 Value *pInput = LOAD(GEP(pVertex, {pVector, unwrap(swizzle_index)}));
707
708 Value *value = VEXTRACT(pInput, C(i));
709 res = VINSERT(res, value, C(i));
710 }
711
712 return wrap(res);
713 } else {
714 Value *attrib = LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_index}));
715
716 Value *pVertex = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_pVerts});
717 Value *pInputVertStride = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_inputVertStride});
718
719 Value *pVector = ADD(MUL(vert_index, pInputVertStride), attrib);
720
721 Value *pInput = LOAD(GEP(pVertex, {pVector, unwrap(swizzle_index)}));
722
723 return wrap(pInput);
724 }
725 }
726
727 // GS output stream layout
728 #define VERTEX_COUNT_SIZE 32
729 #define CONTROL_HEADER_SIZE (8*32)
730
731 void
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)
737 {
738 swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
739
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));
745
746 Value *vMask = LOAD(iface->pGsCtx, {0, SWR_GS_CONTEXT_mask});
747 Value *vMask1 = TRUNC(vMask, VectorType::get(mInt1Ty, mVWidth));
748
749 Value *pStack = STACKSAVE();
750 Value *pTmpPtr = ALLOCA(mFP32Ty, C(4)); // used for dummy write for lane masking
751
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;
766 } else {
767 attribSlot = VERTEX_ATTRIB_START_SLOT + attrib;
768 if (iface->info->writes_position) {
769 attribSlot--;
770 }
771 }
772
773 Value *pOutputOffset = ADD(pVertexOffset, VIMMED1(headerSize + attribSize * attribSlot)); // + sgvChannel ?
774
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);
780
781 Value *pLaneMask = VEXTRACT(vMask1, C(lane));
782 pStreamOffset = SELECT(pLaneMask, pStreamOffset, pTmpPtr);
783
784 for (uint32_t channel = 0; channel < 4; ++channel) {
785 Value *vData;
786
787 if (attribSlot == VERTEX_SGV_SLOT)
788 vData = LOAD(unwrap(outputs[attrib][0]));
789 else
790 vData = LOAD(unwrap(outputs[attrib][channel]));
791
792 if (attribSlot != VERTEX_SGV_SLOT ||
793 sgvChannel == channel) {
794 vData = VEXTRACT(vData, C(lane));
795 STORE(vData, pStreamOffset);
796 }
797 pStreamOffset = GEP(pStreamOffset, C(1));
798 }
799 }
800 }
801
802 /* When the output type is not points, the geometry shader may not
803 * output data to multiple streams. So early exit here.
804 */
805 if(iface->pGsState->outputTopology != TOP_POINT_LIST) {
806 STACKRESTORE(pStack);
807 return;
808 }
809
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 // ----------------- ----------------- ----
815
816 // Calculate where need to put stream id for current vert
817 // in 1 byte "box".
818 Value *pShiftControl = MUL(unwrap(emitted_vertices_vec), VIMMED1(2));
819
820 // Calculate in which box put stream id for current vert.
821 Value *pOffsetControl = LSHR(unwrap(emitted_vertices_vec), VIMMED1(2));
822
823 // Skip count header
824 Value *pStreamIdOffset = ADD(pOffsetControl, VIMMED1(VERTEX_COUNT_SIZE));
825
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});
829
830 Value *pStreamOffset = GEP(pStream, VEXTRACT(pStreamIdOffset, C(lane)));
831
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);
834
835 // Shift it to correct position in byte "box"
836 vVal = SHL(vVal, pShift);
837
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);
843 }
844
845 STACKRESTORE(pStack);
846 }
847
848 void
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)
855 {
856 swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
857
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.
862 */
863 if (iface->pGsState->outputTopology == TOP_POINT_LIST) {
864 return;
865 }
866
867 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
868
869 Value *vMask = LOAD(iface->pGsCtx, { 0, SWR_GS_CONTEXT_mask });
870 Value *vMask1 = TRUNC(vMask, VectorType::get(mInt1Ty, 8));
871
872 uint32_t vertsPerPrim = iface->num_verts_per_prim;
873
874 Value *vCount =
875 ADD(MUL(unwrap(emitted_prims_vec), VIMMED1(vertsPerPrim)),
876 unwrap(verts_per_prim_vec));
877
878 vCount = unwrap(total_emitted_vertices_vec);
879
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));
884
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)));
888
889 vValue = TRUNC(vValue, VectorType::get(mInt8Ty, 8));
890
891 Value *pStack = STACKSAVE();
892 Value *pTmpPtr = ALLOCA(mInt8Ty, C(4)); // used for dummy read/write for lane masking
893
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);
898
899 Value *pLaneMask = VEXTRACT(vMask1, C(lane));
900 pStreamOffset = SELECT(pLaneMask, pStreamOffset, pTmpPtr);
901
902 Value *vVal = LOAD(pStreamOffset);
903 vVal = OR(vVal, VEXTRACT(vValue, C(lane)));
904 STORE(vVal, pStreamOffset);
905 }
906
907 STACKRESTORE(pStack);
908 }
909
910 void
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)
914 {
915 swr_gs_llvm_iface *iface = (swr_gs_llvm_iface*)gs_base;
916
917 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
918
919 // Store emit count to each output stream in the first DWORD
920 for (uint32_t lane = 0; lane < mVWidth; ++lane)
921 {
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);
926 }
927 }
928
929 void
930 BuilderSWR::swr_tcs_llvm_emit_prologue(struct lp_build_tgsi_soa_context* bld)
931 {
932 swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld->tcs_iface;
933
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),
937 LLVMIntULT,
938 lp_build_const_int32(gallivm, iface->output_vertices),
939 lp_build_const_int32(gallivm, 1));
940
941 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
942 bld->system_values.invocation_id = wrap(VBROADCAST(unwrap(iface->loop_state.counter)));
943
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);
947 }
948 }
949
950 void
951 BuilderSWR::swr_tcs_llvm_emit_epilogue(struct lp_build_tgsi_soa_context* bld)
952 {
953 swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)bld->tcs_iface;
954
955 if (verbose_shader) {
956 lp_build_printf(gallivm, "Epilogue LOOP: Iteration %d END\n", iface->loop_state.counter);
957 }
958 lp_build_for_loop_end(&iface->loop_state);
959 }
960
961 LLVMValueRef
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)
969 {
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);
973
974 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
975
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);
980 }
981
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);
986 }
987 }
988
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);
993 }
994 }
995
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));
999 }
1000
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) });
1004
1005 LLVMValueRef res = wrap(LOAD(pBase));
1006
1007 if (verbose_shader) {
1008 lp_build_print_value(gallivm, "TCS input fetched: ", res);
1009 }
1010 return res;
1011 }
1012
1013 LLVMValueRef
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,
1021 uint32_t name)
1022 {
1023 swr_tcs_llvm_iface *iface = (swr_tcs_llvm_iface*)tcs_iface;
1024
1025 Value *vert_index = unwrap(vertex_index);
1026 Value *attr_index = unwrap(attrib_index);
1027
1028 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
1029
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);
1034 }
1035
1036 if (is_vindex_indirect) {
1037 vert_index = VEXTRACT(vert_index, C(0));
1038 if (verbose_shader)
1039 {
1040 lp_build_print_value(gallivm, "TCSo: Extracted vertex index: ", vertex_index);
1041 }
1042 }
1043
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);
1048 }
1049 }
1050
1051 Value* res = unwrap(bld_base->base.zero);
1052
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});
1056
1057 if (name == TGSI_SEMANTIC_TESSOUTER || name == TGSI_SEMANTIC_TESSINNER) {
1058
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});
1063 } else {
1064 tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_InnerTessFactors});
1065 }
1066 Value* tessFactor = GEP(tessFactorArray, {C(0), unwrap(swizzle_index)});
1067 res = VINSERT(res, LOAD(tessFactor), C(lane));
1068
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));
1076 }
1077 } else {
1078 // Generic attribute
1079 Value *attrib =
1080 LOAD(GEP(iface->pVtxOutputAttribMap, {C(0), attr_index}));
1081 if (verbose_shader)
1082 {
1083 lp_build_print_value(gallivm, "TCSo: Attrib index from map: ", wrap(attrib));
1084 }
1085 Value* attr_chan = GEP(pCpOut, {C(0), C(ScalarPatch_cp), vert_index,
1086 C(ScalarCPoint_attrib), attrib, unwrap(swizzle_index)});
1087
1088 res = VINSERT(res, LOAD(attr_chan), C(lane));
1089 }
1090 }
1091
1092 if (verbose_shader) {
1093 lp_build_print_value(gallivm, "TCSo: output fetched: ", wrap(res));
1094 }
1095 return wrap(res);
1096 }
1097
1098 void
1099 BuilderSWR::swr_tcs_llvm_store_output(const struct lp_build_tcs_iface *tcs_iface,
1100 struct lp_build_tgsi_context *bld_base,
1101 unsigned name,
1102 boolean is_vindex_indirect,
1103 LLVMValueRef vertex_index,
1104 boolean is_aindex_indirect,
1105 LLVMValueRef attrib_index,
1106 LLVMValueRef swizzle_index,
1107 LLVMValueRef value)
1108 {
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;
1111
1112 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
1113
1114 if (verbose_shader) {
1115 lp_build_printf(gallivm, "[TCS OUT] =============================================\n");
1116 }
1117
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);
1121 }
1122
1123 Value *vert_index = unwrap(vertex_index);
1124 Value *attr_index = unwrap(attrib_index);
1125
1126 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
1127
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);
1132 }
1133
1134 if (is_vindex_indirect) {
1135 vert_index = VEXTRACT(vert_index, C(0));
1136 if (verbose_shader)
1137 {
1138 lp_build_print_value(gallivm, "[TCS OUT] Extracted vertex index: ", vertex_index);
1139 }
1140 }
1141
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));
1146 }
1147 }
1148
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});
1152
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});
1158 } else {
1159 tessFactorArray = GEP(tessFactors, {(uint32_t)0, SWR_TESSELLATION_FACTORS_InnerTessFactors});
1160 }
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);
1168 }
1169 STORE(valueToStore, tessFactor);
1170 if (verbose_shader) {
1171 lp_build_print_value(gallivm, "[TCS OUT][FACTOR] Stored value: ", wrap(valueToStore));
1172 }
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));
1181 }
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);
1189 }
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));
1200 }
1201 }
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));
1205 }
1206 } else {
1207 Value* value_to_store = VEXTRACT(unwrap(value), C(lane));
1208 Value* attrib = LOAD(GEP(iface->pVtxOutputAttribMap, {C(0), attr_index}));
1209
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));
1217 }
1218
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)});
1222
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);
1230 }
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));
1234 }
1235 }
1236 }
1237 }
1238
1239
1240
1241 void
1242 BuilderSWR::swr_tcs_llvm_emit_barrier(const struct lp_build_tcs_iface *tcs_iface,
1243 struct lp_build_tgsi_context *bld_base)
1244 {
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;
1247
1248 if (verbose_shader) {
1249 lp_build_printf(gallivm, "Barrier LOOP: Iteration %d END\n", iface->loop_state.counter);
1250 }
1251
1252 // End previous loop
1253 lp_build_for_loop_end(&iface->loop_state);
1254
1255 // Start new one
1256 lp_build_for_loop_begin(&iface->loop_state, gallivm,
1257 lp_build_const_int32(gallivm, 0),
1258 LLVMIntULT,
1259 lp_build_const_int32(gallivm, iface->output_vertices),
1260 lp_build_const_int32(gallivm, 1));
1261
1262
1263 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
1264
1265 bld->system_values.invocation_id = wrap(VBROADCAST(unwrap(iface->loop_state.counter)));
1266
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);
1270 }
1271 }
1272
1273
1274 LLVMValueRef
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)
1280 {
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);
1284
1285 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
1286
1287 if (verbose_shader) {
1288 lp_build_printf(gallivm, "[TES IN][PATCH] --------------------------------------\n");
1289 }
1290
1291 if (is_aindex_indirect) {
1292 int i;
1293 struct lp_type type = bld_base->base.type;
1294
1295 for (i = 0; i < type.length; i++) {
1296 Value *attr_chan_index = attr_index;
1297
1298 if (is_aindex_indirect) {
1299 attr_chan_index = VEXTRACT(attr_index, C(i));
1300 }
1301
1302 Value *attrib =
1303 LOAD(GEP(iface->pPatchAttribMap, {C(0), attr_chan_index}));
1304
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));
1315 }
1316 res = VINSERT(res, Val, C(i));
1317 }
1318 } else {
1319 Value *attrib = LOAD(GEP(iface->pPatchAttribMap, {C(0), attr_index}));
1320
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));
1331 }
1332 res = VBROADCAST(Val);
1333 }
1334 if (verbose_shader) {
1335 lp_build_print_value(gallivm, "[TES IN][PATCH] returning: ", wrap(res));
1336 }
1337 return wrap(res);
1338 }
1339
1340
1341
1342 LLVMValueRef
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)
1350 {
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);
1354
1355 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
1356
1357 if (verbose_shader) {
1358 lp_build_printf(gallivm, "[TES IN][VTX] --------------------------------------\n");
1359 }
1360
1361 Value *res = unwrap(bld_base->base.zero);
1362 if (is_vindex_indirect || is_aindex_indirect) {
1363 int i;
1364 struct lp_type type = bld_base->base.type;
1365
1366 for (i = 0; i < type.length; i++) {
1367 Value *vert_chan_index = vert_index;
1368 Value *attr_chan_index = attr_index;
1369
1370 if (is_vindex_indirect) {
1371 vert_chan_index = VEXTRACT(vert_index, C(i));
1372 }
1373 if (is_aindex_indirect) {
1374 attr_chan_index = VEXTRACT(attr_index, C(i));
1375 }
1376
1377 Value *attrib =
1378 LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_chan_index}));
1379
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));
1392 }
1393 res = VINSERT(res, Val, C(i));
1394 }
1395 } else {
1396 Value *attrib = LOAD(GEP(iface->pVtxAttribMap, {C(0), attr_index}));
1397
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));
1410 }
1411 res = VBROADCAST(Val);
1412 }
1413 if (verbose_shader) {
1414 lp_build_print_value(gallivm, "[TES IN][VTX] returning: ", wrap(res));
1415 }
1416 return wrap(res);
1417 }
1418
1419
1420
1421
1422 PFN_GS_FUNC
1423 BuilderSWR::CompileGS(struct swr_context *ctx, swr_jit_gs_key &key)
1424 {
1425 SWR_GS_STATE *pGS = &ctx->gs->gsState;
1426 struct tgsi_shader_info *info = &ctx->gs->info.base;
1427
1428 memset(pGS, 0, sizeof(*pGS));
1429
1430 pGS->gsEnable = true;
1431
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);
1435
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.
1439 */
1440 pGS->maxNumVerts = info->properties[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES] + 1;
1441 pGS->instanceCount = info->properties[TGSI_PROPERTY_GS_INVOCATIONS];
1442
1443 // If point primitive then assume to use multiple streams
1444 if(pGS->outputTopology == TOP_POINT_LIST) {
1445 pGS->isSingleStream = false;
1446 } else {
1447 pGS->isSingleStream = true;
1448 pGS->singleStreamID = 0;
1449 }
1450
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;
1457
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
1463
1464 struct swr_geometry_shader *gs = ctx->gs;
1465
1466 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
1467 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
1468
1469 memset(outputs, 0, sizeof(outputs));
1470
1471 AttrBuilder attrBuilder;
1472 attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));
1473
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);
1479
1480 // create new vertex shader function
1481 auto pFunction = Function::Create(vsFuncType,
1482 GlobalValue::ExternalLinkage,
1483 "GS",
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);
1489 #else
1490 pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);
1491 #endif
1492
1493 BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);
1494 IRB()->SetInsertPoint(block);
1495 LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));
1496
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");
1504
1505 Value *consts_ptr =
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");
1511
1512 struct lp_build_sampler_soa *sampler =
1513 swr_sampler_soa_create(key.sampler, PIPE_SHADER_GEOMETRY);
1514
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}));
1519
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];
1525
1526 unsigned vs_slot = locate_linkage(semantic_name, semantic_idx, &ctx->vs->info.base);
1527
1528 vs_slot += VERTEX_ATTRIB_START_SLOT;
1529
1530 if (ctx->vs->info.base.output_semantic_name[0] == TGSI_SEMANTIC_POSITION)
1531 vs_slot--;
1532
1533 if (semantic_name == TGSI_SEMANTIC_POSITION)
1534 vs_slot = VERTEX_POSITION_SLOT;
1535
1536 STORE(C(vs_slot), vtxAttribMap, {0, slot});
1537 mapConstants.push_back(C(vs_slot));
1538 }
1539
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));
1544
1545 // zero out cut buffer so we can load/modify/store bits
1546 for (uint32_t lane = 0; lane < mVWidth; ++lane)
1547 {
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));
1551 #else
1552 MEMSET(pStream, C((char)0), VERTEX_COUNT_SIZE + CONTROL_HEADER_SIZE, sizeof(float) * KNOB_SIMD_WIDTH);
1553 #endif
1554 }
1555
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;
1569
1570 struct lp_build_tgsi_params params;
1571 memset(&params, 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;
1582
1583 lp_build_tgsi_soa(gallivm,
1584 gs->pipe.tokens,
1585 &params,
1586 outputs);
1587
1588 lp_build_mask_end(&mask);
1589
1590 sampler->destroy(sampler);
1591
1592 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
1593
1594 RET_VOID();
1595
1596 gallivm_verify_function(gallivm, wrap(pFunction));
1597 gallivm_compile_module(gallivm);
1598
1599 PFN_GS_FUNC pFunc =
1600 (PFN_GS_FUNC)gallivm_jit_function(gallivm, wrap(pFunction));
1601
1602 debug_printf("geom shader %p\n", pFunc);
1603 assert(pFunc && "Error: GeomShader = NULL");
1604
1605 JM()->mIsModuleFinalized = true;
1606
1607 return pFunc;
1608 }
1609
1610 PFN_TES_FUNC
1611 BuilderSWR::CompileTES(struct swr_context *ctx, swr_jit_tes_key &key)
1612 {
1613 SWR_TS_STATE *pTS = &ctx->tsState;
1614 struct tgsi_shader_info *info = &ctx->tes->info.base;
1615
1616 // tessellation is enabled if TES is present
1617 // clear tessellation state here then
1618 memset(pTS, 0, sizeof(*pTS));
1619
1620 pTS->tsEnable = true;
1621
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;
1630
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;
1636 break;
1637 case PIPE_PRIM_TRIANGLES:
1638 type = SWR_TS_TRI;
1639 postDSTopology = TOP_TRIANGLE_LIST;
1640 break;
1641 case PIPE_PRIM_QUADS:
1642 type = SWR_TS_QUAD;
1643 // See OpenGL spec - quads are tessellated into triangles
1644 postDSTopology = TOP_TRIANGLE_LIST;
1645 break;
1646 default:
1647 assert(0);
1648 }
1649
1650 switch (tes_spacing) {
1651 case PIPE_TESS_SPACING_FRACTIONAL_ODD:
1652 partitioning = SWR_TS_ODD_FRACTIONAL;
1653 break;
1654 case PIPE_TESS_SPACING_FRACTIONAL_EVEN:
1655 partitioning = SWR_TS_EVEN_FRACTIONAL;
1656 break;
1657 case PIPE_TESS_SPACING_EQUAL:
1658 partitioning = SWR_TS_INTEGER;
1659 break;
1660 default:
1661 assert(0);
1662 }
1663
1664 if (tes_point_mode) {
1665 topology = SWR_TS_OUTPUT_POINT;
1666 postDSTopology = TOP_POINT_LIST;
1667 }
1668 else if (tes_prim_mode == PIPE_PRIM_LINES) {
1669 topology = SWR_TS_OUTPUT_LINE;
1670 }
1671 else if (tes_vertex_order_cw) {
1672 topology = SWR_TS_OUTPUT_TRI_CW;
1673 }
1674 else {
1675 topology = SWR_TS_OUTPUT_TRI_CCW;
1676 }
1677
1678 pTS->domain = type;
1679 pTS->tsOutputTopology = topology;
1680 pTS->partitioning = partitioning;
1681 pTS->numDsOutputAttribs = info->num_outputs;
1682 pTS->postDSTopology = postDSTopology;
1683
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;
1688
1689 struct swr_tess_evaluation_shader *tes = ctx->tes;
1690
1691 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
1692 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
1693
1694 memset(outputs, 0, sizeof(outputs));
1695
1696 AttrBuilder attrBuilder;
1697 attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));
1698
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);
1704
1705 // create new vertex shader function
1706 auto pFunction = Function::Create(tesFuncType,
1707 GlobalValue::ExternalLinkage,
1708 "TES",
1709 JM()->mpCurrentModule);
1710
1711 #if LLVM_VERSION_MAJOR < 5
1712 AttributeSet attrSet = AttributeSet::get(
1713 JM()->mContext, AttributeSet::FunctionIndex, attrBuilder);
1714 pFunction->addAttributes(AttributeSet::FunctionIndex, attrSet);
1715 #else
1716 pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);
1717 #endif
1718
1719 BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);
1720 IRB()->SetInsertPoint(block);
1721 LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));
1722
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");
1730
1731 Value *consts_ptr =
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");
1737
1738 struct lp_build_sampler_soa *sampler =
1739 swr_sampler_soa_create(key.sampler, PIPE_SHADER_TESS_EVAL);
1740
1741 struct lp_bld_tgsi_system_values system_values;
1742 memset(&system_values, 0, sizeof(system_values));
1743
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});
1749
1750 Value* tess_coord = ALLOCA(ArrayType::get(mSimdFP32Ty, 3));
1751
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);
1759
1760 // Primitive ID
1761 system_values.prim_id = wrap(VBROADCAST(LOAD(pTesCtx, {0, SWR_DS_CONTEXT_PrimitiveID}), "PrimitiveID"));
1762
1763 // Tessellation factors
1764 Value* pPatch = LOAD(pTesCtx, {0, SWR_DS_CONTEXT_pCpIn});
1765 Value* pTessFactors = GEP(pPatch, {C(0), C(ScalarPatch_tessFactors)});
1766
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");
1772 }
1773 system_values.tess_outer = wrap(sys_value_outer_factors);
1774
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");
1780 }
1781 system_values.tess_inner = wrap(sys_value_inner_factors);
1782
1783 if (verbose_shader)
1784 {
1785 lp_build_print_value(gallivm, "tess_coord = ", system_values.tess_coord);
1786 }
1787
1788 struct tgsi_shader_info *pPrevShader = nullptr;
1789
1790 if (ctx->tcs) {
1791 pPrevShader = &ctx->tcs->info.base;
1792 }
1793 else {
1794 pPrevShader = &ctx->vs->info.base;
1795 }
1796
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:
1805 perPatchAttrs++;
1806 break;
1807 case TGSI_SEMANTIC_GENERIC:
1808 genericAttrs++;
1809 break;
1810 case TGSI_SEMANTIC_TESSINNER:
1811 case TGSI_SEMANTIC_TESSOUTER:
1812 tessLevelAttrs++;
1813 break;
1814 case TGSI_SEMANTIC_POSITION:
1815 case TGSI_SEMANTIC_CLIPDIST:
1816 case TGSI_SEMANTIC_PSIZE:
1817 sgvAttrs++;
1818 break;
1819 default:
1820 assert(!"Unknown semantic input in TES");
1821 }
1822 }
1823
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];
1830
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);
1834
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;
1839 break;
1840 case TGSI_SEMANTIC_PATCH:
1841 tcs_slot = semantic_idx;
1842 break;
1843 case TGSI_SEMANTIC_POSITION:
1844 tcs_slot = VERTEX_POSITION_SLOT;
1845 break;
1846 case TGSI_SEMANTIC_CLIPDIST:
1847 case TGSI_SEMANTIC_PSIZE:
1848 break;
1849 default:
1850 assert(!"Unexpected semantic found while builiding TES input map");
1851 }
1852 if (semantic_name == TGSI_SEMANTIC_PATCH) {
1853 STORE(C(tcs_slot), patchAttribMap, {0, slot});
1854 } else {
1855 STORE(C(tcs_slot), vtxAttribMap, {0, slot});
1856 }
1857 mapConstants.push_back(C(tcs_slot));
1858 }
1859
1860 // Build execution mask
1861 struct lp_build_mask_context mask;
1862 Value *mask_val = LOAD(pTesCtx, {0, SWR_DS_CONTEXT_mask}, "tesMask");
1863
1864 if (verbose_shader)
1865 lp_build_print_value(gallivm, "TES execution mask: ", wrap(mask_val));
1866
1867 lp_build_mask_begin(&mask, gallivm,
1868 lp_type_float_vec(32, 32 * 8), wrap(mask_val));
1869
1870 struct swr_tes_llvm_iface tes_iface;
1871
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;
1874
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;
1882
1883 struct lp_build_tgsi_params params;
1884 memset(&params, 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;
1895
1896 // Build LLVM IR
1897 lp_build_tgsi_soa(gallivm,
1898 tes->pipe.tokens,
1899 &params,
1900 outputs);
1901
1902 lp_build_mask_end(&mask);
1903
1904 sampler->destroy(sampler);
1905
1906 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
1907
1908 // Write output attributes
1909 Value *dclOut = LOAD(pTesCtx, {0, SWR_DS_CONTEXT_pOutputData}, "dclOut");
1910
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])
1914 continue;
1915
1916 Value *val = LOAD(unwrap(outputs[attrib][channel]));;
1917 Value *attribOffset =
1918 LOAD(pTesCtx, {0, SWR_DS_CONTEXT_outVertexAttribOffset});
1919
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
1929 outSlot--;
1930 }
1931 outputSlot = ADD(attribOffset, C(outSlot));
1932 }
1933
1934 Value *attribVecIndex =
1935 ADD(MUL(vecStride, MUL(outputSlot, C(4))), vecOffset);
1936
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});
1941
1942 if (verbose_shader) {
1943 lp_build_printf(gallivm,
1944 "TES output [%d][%d]",
1945 C(attrib),
1946 C(channel));
1947 lp_build_print_value(gallivm, " = ", wrap(val));
1948 }
1949 }
1950 }
1951
1952 RET_VOID();
1953
1954 JM()->DumpToFile(pFunction, "src");
1955 gallivm_verify_function(gallivm, wrap(pFunction));
1956
1957 gallivm_compile_module(gallivm);
1958 JM()->DumpToFile(pFunction, "optimized");
1959
1960 PFN_TES_FUNC pFunc =
1961 (PFN_TES_FUNC)gallivm_jit_function(gallivm, wrap(pFunction));
1962
1963 debug_printf("tess evaluation shader %p\n", pFunc);
1964 assert(pFunc && "Error: TessEvaluationShader = NULL");
1965
1966 JM()->DumpAsm(pFunction, "asm");
1967
1968 JM()->mIsModuleFinalized = true;
1969
1970 return pFunc;
1971 }
1972
1973 PFN_TCS_FUNC
1974 BuilderSWR::CompileTCS(struct swr_context *ctx, swr_jit_tcs_key &key)
1975 {
1976 SWR_TS_STATE *pTS = &ctx->tsState;
1977 struct tgsi_shader_info *info = &ctx->tcs->info.base;
1978
1979 pTS->numHsInputAttribs = info->num_inputs;
1980 pTS->numHsOutputAttribs = info->num_outputs;
1981
1982 pTS->hsAllocationSize = sizeof(ScalarPatch);
1983
1984 pTS->vertexAttribOffset = VERTEX_ATTRIB_START_SLOT;
1985 pTS->srcVertexAttribOffset = VERTEX_ATTRIB_START_SLOT;
1986
1987 struct swr_tess_control_shader *tcs = ctx->tcs;
1988
1989 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
1990 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
1991
1992 memset(outputs, 0, sizeof(outputs));
1993
1994 AttrBuilder attrBuilder;
1995 attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));
1996
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);
2003
2004 // create new vertex shader function
2005 auto pFunction = Function::Create(tcsFuncType,
2006 GlobalValue::ExternalLinkage,
2007 "TCS",
2008 JM()->mpCurrentModule);
2009
2010 #if LLVM_VERSION_MAJOR < 5
2011 AttributeSet attrSet = AttributeSet::get(
2012 JM()->mContext, AttributeSet::FunctionIndex, attrBuilder);
2013 pFunction->addAttributes(AttributeSet::FunctionIndex, attrSet);
2014 #else
2015 pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);
2016 #endif
2017
2018 BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);
2019 IRB()->SetInsertPoint(block);
2020 LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));
2021
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");
2029
2030 Value *consts_ptr =
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");
2036
2037 struct lp_build_sampler_soa *sampler =
2038 swr_sampler_soa_create(key.sampler, PIPE_SHADER_TESS_CTRL);
2039
2040 struct lp_bld_tgsi_system_values system_values;
2041 memset(&system_values, 0, sizeof(system_values));
2042
2043 system_values.prim_id =
2044 wrap(LOAD(pTcsCtx, {0, SWR_HS_CONTEXT_PrimitiveID}));
2045
2046 Constant *vInvocationId;
2047 if (mVWidth == 8) {
2048 vInvocationId = C({0, 1, 2, 3, 4, 5, 6, 7});
2049 } else {
2050 vInvocationId =
2051 C({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
2052 }
2053
2054 system_values.invocation_id = wrap(vInvocationId);
2055 system_values.vertices_in = wrap(C(tcs->vertices_per_patch));
2056
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);
2061 }
2062
2063 std::vector<Constant *> mapConstants;
2064 Value *vtxAttribMap =
2065 ALLOCA(ArrayType::get(mInt32Ty, PIPE_MAX_SHADER_INPUTS));
2066
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];
2070
2071 unsigned vs_slot =
2072 locate_linkage(semantic_name, semantic_idx, &ctx->vs->info.base);
2073
2074 vs_slot += VERTEX_ATTRIB_START_SLOT;
2075
2076 if (ctx->vs->info.base.output_semantic_name[0]
2077 == TGSI_SEMANTIC_POSITION)
2078 vs_slot--;
2079
2080 if (semantic_name == TGSI_SEMANTIC_POSITION)
2081 vs_slot = VERTEX_POSITION_SLOT;
2082
2083 STORE(C(vs_slot), vtxAttribMap, {0, slot});
2084 mapConstants.push_back(C(vs_slot));
2085 }
2086
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});
2099 } else {
2100 int32_t target_slot = slot;
2101 if (name == TGSI_SEMANTIC_GENERIC) {
2102 target_slot += VERTEX_ATTRIB_START_SLOT;
2103 }
2104 // Now normalize target slot
2105 for (ubyte as = 0; as < slot; as++) {
2106 ubyte name = info->output_semantic_name[as];
2107 switch (name) {
2108 case TGSI_SEMANTIC_TESSOUTER:
2109 case TGSI_SEMANTIC_TESSINNER:
2110 case TGSI_SEMANTIC_PATCH:
2111 case TGSI_SEMANTIC_POSITION:
2112 target_slot--;
2113 }
2114 }
2115 if (name == TGSI_SEMANTIC_POSITION) {
2116 target_slot = VERTEX_POSITION_SLOT;
2117 }
2118 STORE(C(target_slot), vtxOutputAttribMap, {0, slot});
2119 mapConstants.push_back(C(target_slot));
2120 }
2121 }
2122
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));
2127
2128 struct swr_tcs_llvm_iface tcs_iface;
2129
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;
2136
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;
2145
2146 struct lp_build_tgsi_params params;
2147 memset(&params, 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;
2158
2159 lp_build_tgsi_soa(gallivm, tcs->pipe.tokens, &params, outputs);
2160
2161 lp_build_mask_end(&mask);
2162
2163 sampler->destroy(sampler);
2164
2165 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
2166 RET_VOID();
2167
2168 JM()->DumpToFile(pFunction, "src");
2169 gallivm_verify_function(gallivm, wrap(pFunction));
2170 gallivm_compile_module(gallivm);
2171 JM()->DumpToFile(pFunction, "optimized");
2172
2173 PFN_TCS_FUNC pFunc =
2174 (PFN_TCS_FUNC)gallivm_jit_function(gallivm, wrap(pFunction));
2175
2176 debug_printf("tess control shader %p\n", pFunc);
2177 assert(pFunc && "Error: TessControlShader = NULL");
2178 JM()->DumpAsm(pFunction, "asm");
2179
2180 JM()->mIsModuleFinalized = true;
2181
2182 return pFunc;
2183 }
2184
2185
2186 PFN_GS_FUNC
2187 swr_compile_gs(struct swr_context *ctx, swr_jit_gs_key &key)
2188 {
2189 BuilderSWR builder(
2190 reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),
2191 "GS");
2192 PFN_GS_FUNC func = builder.CompileGS(ctx, key);
2193
2194 ctx->gs->map.insert(std::make_pair(key, std::make_unique<VariantGS>(builder.gallivm, func)));
2195 return func;
2196 }
2197
2198 PFN_TCS_FUNC
2199 swr_compile_tcs(struct swr_context *ctx, swr_jit_tcs_key &key)
2200 {
2201 BuilderSWR builder(
2202 reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),
2203 "TCS");
2204 PFN_TCS_FUNC func = builder.CompileTCS(ctx, key);
2205
2206 ctx->tcs->map.insert(
2207 std::make_pair(key, std::make_unique<VariantTCS>(builder.gallivm, func)));
2208
2209 return func;
2210 }
2211
2212 PFN_TES_FUNC
2213 swr_compile_tes(struct swr_context *ctx, swr_jit_tes_key &key)
2214 {
2215 BuilderSWR builder(
2216 reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),
2217 "TES");
2218 PFN_TES_FUNC func = builder.CompileTES(ctx, key);
2219
2220 ctx->tes->map.insert(
2221 std::make_pair(key, std::make_unique<VariantTES>(builder.gallivm, func)));
2222
2223 return func;
2224 }
2225
2226 void
2227 BuilderSWR::WriteVS(Value *pVal, Value *pVsContext, Value *pVtxOutput, unsigned slot, unsigned channel)
2228 {
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
2233
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});
2237 #else
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));
2243 }
2244 #endif
2245 }
2246
2247 PFN_VERTEX_FUNC
2248 BuilderSWR::CompileVS(struct swr_context *ctx, swr_jit_vs_key &key)
2249 {
2250 struct swr_vertex_shader *swr_vs = ctx->vs;
2251
2252 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
2253 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
2254
2255 memset(outputs, 0, sizeof(outputs));
2256
2257 AttrBuilder attrBuilder;
2258 attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));
2259
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);
2265
2266 // create new vertex shader function
2267 auto pFunction = Function::Create(vsFuncType,
2268 GlobalValue::ExternalLinkage,
2269 "VS",
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);
2275 #else
2276 pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);
2277 #endif
2278
2279 BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);
2280 IRB()->SetInsertPoint(block);
2281 LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));
2282
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");
2290
2291 Value *consts_ptr = GEP(hPrivateData, {C(0), C(swr_draw_context_constantVS)});
2292
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");
2297
2298 Value *vtxInput = LOAD(pVsCtx, {0, SWR_VS_CONTEXT_pVin});
2299 #if USE_SIMD16_VS
2300 vtxInput = BITCAST(vtxInput, PointerType::get(Gen_simd16vertex(JM()), 0));
2301 #endif
2302
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}));
2309 }
2310 }
2311 }
2312
2313 struct lp_build_sampler_soa *sampler =
2314 swr_sampler_soa_create(key.sampler, PIPE_SHADER_VERTEX);
2315
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}));
2319
2320 #if USE_SIMD16_VS
2321 system_values.vertex_id = wrap(LOAD(pVsCtx, {0, SWR_VS_CONTEXT_VertexID16}));
2322 #else
2323 system_values.vertex_id = wrap(LOAD(pVsCtx, {0, SWR_VS_CONTEXT_VertexID}));
2324 #endif
2325
2326 #if USE_SIMD16_VS
2327 uint32_t vectorWidth = mVWidth16;
2328 #else
2329 uint32_t vectorWidth = mVWidth;
2330 #endif
2331
2332 struct lp_build_tgsi_params params;
2333 memset(&params, 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;
2342
2343 lp_build_tgsi_soa(gallivm,
2344 swr_vs->pipe.tokens,
2345 &params,
2346 outputs);
2347
2348 sampler->destroy(sampler);
2349
2350 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
2351
2352 Value *vtxOutput = LOAD(pVsCtx, {0, SWR_VS_CONTEXT_pVout});
2353 #if USE_SIMD16_VS
2354 vtxOutput = BITCAST(vtxOutput, PointerType::get(Gen_simd16vertex(JM()), 0));
2355 #endif
2356
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])
2360 continue;
2361
2362 Value *val;
2363 uint32_t outSlot;
2364
2365 if (swr_vs->info.base.output_semantic_name[attrib] == TGSI_SEMANTIC_PSIZE) {
2366 if (channel != VERTEX_SGV_POINT_SIZE_COMP)
2367 continue;
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;
2373 } else {
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)
2377 outSlot--;
2378 }
2379
2380 WriteVS(val, pVsCtx, vtxOutput, outSlot, channel);
2381 }
2382 }
2383
2384 if (ctx->rasterizer->clip_plane_enable ||
2385 swr_vs->info.base.culldist_writemask) {
2386 unsigned clip_mask = ctx->rasterizer->clip_plane_enable;
2387
2388 unsigned cv = 0;
2389 if (swr_vs->info.base.writes_clipvertex) {
2390 cv = locate_linkage(TGSI_SEMANTIC_CLIPVERTEX, 0,
2391 &swr_vs->info.base);
2392 } else {
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) {
2396 cv = i;
2397 break;
2398 }
2399 }
2400 }
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], "");
2405
2406 tgsi_shader_info *pLastFE = &ctx->vs->info.base;
2407
2408 if (ctx->gs) {
2409 pLastFE = &ctx->gs->info.base;
2410 }
2411 else if (ctx->tes) {
2412 pLastFE = &ctx->tes->info.base;
2413 }
2414 else if (ctx->tcs) {
2415 pLastFE = &ctx->tcs->info.base;
2416 }
2417
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);
2423 if (val < 4) {
2424 LLVMValueRef dist = LLVMBuildLoad(gallivm->builder, outputs[cv][val], "");
2425 WriteVS(unwrap(dist), pVsCtx, vtxOutput, VERTEX_CLIPCULL_DIST_LO_SLOT, val);
2426 } else {
2427 LLVMValueRef dist = LLVMBuildLoad(gallivm->builder, outputs[cv][val - 4], "");
2428 WriteVS(unwrap(dist), pVsCtx, vtxOutput, VERTEX_CLIPCULL_DIST_HI_SLOT, val - 4);
2429 }
2430 continue;
2431 }
2432
2433 if (!(clip_mask & (1 << val)))
2434 continue;
2435
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}));
2440 #if USE_SIMD16_VS
2441 Value *bpx = VBROADCAST_16(px);
2442 Value *bpy = VBROADCAST_16(py);
2443 Value *bpz = VBROADCAST_16(pz);
2444 Value *bpw = VBROADCAST_16(pw);
2445 #else
2446 Value *bpx = VBROADCAST(px);
2447 Value *bpy = VBROADCAST(py);
2448 Value *bpz = VBROADCAST(pz);
2449 Value *bpw = VBROADCAST(pw);
2450 #endif
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))));
2455
2456 if (val < 4)
2457 WriteVS(dist, pVsCtx, vtxOutput, VERTEX_CLIPCULL_DIST_LO_SLOT, val);
2458 else
2459 WriteVS(dist, pVsCtx, vtxOutput, VERTEX_CLIPCULL_DIST_HI_SLOT, val - 4);
2460 }
2461 }
2462
2463 RET_VOID();
2464
2465 JM()->DumpToFile(pFunction, "vs_function1");
2466 gallivm_verify_function(gallivm, wrap(pFunction));
2467 gallivm_compile_module(gallivm);
2468 JM()->DumpToFile(pFunction, "vs_function2");
2469
2470 // lp_debug_dump_value(func);
2471
2472 PFN_VERTEX_FUNC pFunc =
2473 (PFN_VERTEX_FUNC)gallivm_jit_function(gallivm, wrap(pFunction));
2474
2475 JM()->DumpAsm(pFunction, "vs_function_asm");
2476 debug_printf("vert shader %p\n", pFunc);
2477 assert(pFunc && "Error: VertShader = NULL");
2478
2479 JM()->mIsModuleFinalized = true;
2480
2481 return pFunc;
2482 }
2483
2484 PFN_VERTEX_FUNC
2485 swr_compile_vs(struct swr_context *ctx, swr_jit_vs_key &key)
2486 {
2487 if (!ctx->vs->pipe.tokens)
2488 return NULL;
2489
2490 BuilderSWR builder(
2491 reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),
2492 "VS");
2493 PFN_VERTEX_FUNC func = builder.CompileVS(ctx, key);
2494
2495 ctx->vs->map.insert(std::make_pair(key, std::make_unique<VariantVS>(builder.gallivm, func)));
2496 return func;
2497 }
2498
2499 unsigned
2500 swr_so_adjust_attrib(unsigned in_attrib,
2501 swr_vertex_shader *swr_vs)
2502 {
2503 ubyte semantic_name;
2504 unsigned attrib;
2505
2506 attrib = in_attrib + VERTEX_ATTRIB_START_SLOT;
2507
2508 if (swr_vs) {
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;
2516 } else {
2517 if (swr_vs->info.base.writes_position) {
2518 attrib--;
2519 }
2520 }
2521 }
2522
2523 return attrib;
2524 }
2525
2526 static unsigned
2527 locate_linkage(ubyte name, ubyte index, struct tgsi_shader_info *info)
2528 {
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)) {
2532 return i;
2533 }
2534 }
2535
2536 return 0xFFFFFFFF;
2537 }
2538
2539 PFN_PIXEL_KERNEL
2540 BuilderSWR::CompileFS(struct swr_context *ctx, swr_jit_fs_key &key)
2541 {
2542 struct swr_fragment_shader *swr_fs = ctx->fs;
2543
2544 struct tgsi_shader_info *pPrevShader;
2545 if (ctx->gs)
2546 pPrevShader = &ctx->gs->info.base;
2547 else if (ctx->tes)
2548 pPrevShader = &ctx->tes->info.base;
2549 else
2550 pPrevShader = &ctx->vs->info.base;
2551
2552 LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS];
2553 LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS][TGSI_NUM_CHANNELS];
2554
2555 memset(inputs, 0, sizeof(inputs));
2556 memset(outputs, 0, sizeof(outputs));
2557
2558 struct lp_build_sampler_soa *sampler = NULL;
2559
2560 AttrBuilder attrBuilder;
2561 attrBuilder.addStackAlignmentAttr(JM()->mVWidth * sizeof(float));
2562
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);
2568
2569 auto pFunction = Function::Create(funcType,
2570 GlobalValue::ExternalLinkage,
2571 "FS",
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);
2577 #else
2578 pFunction->addAttributes(AttributeList::FunctionIndex, attrBuilder);
2579 #endif
2580
2581 BasicBlock *block = BasicBlock::Create(JM()->mContext, "entry", pFunction);
2582 IRB()->SetInsertPoint(block);
2583 LLVMPositionBuilderAtEnd(gallivm->builder, wrap(block));
2584
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");
2592
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");
2598
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");
2603
2604 swr_fs->constantMask = 0;
2605 swr_fs->flatConstantMask = 0;
2606 swr_fs->pointSpriteMask = 0;
2607
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];
2612
2613 if (!mask)
2614 continue;
2615
2616 // load i,j
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");
2622 break;
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");
2626 break;
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");
2630 break;
2631 }
2632
2633 // load/compute w
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}));
2641 break;
2642 case TGSI_INTERPOLATE_LOC_CENTROID:
2643 vw = VRCP(LOAD(pPS, {0, SWR_PS_CONTEXT_vOneOverW, PixelPositions_centroid}));
2644 break;
2645 case TGSI_INTERPOLATE_LOC_SAMPLE:
2646 vw = VRCP(LOAD(pPS, {0, SWR_PS_CONTEXT_vOneOverW, PixelPositions_sample}));
2647 break;
2648 }
2649 } else {
2650 pAttribs = pRawAttribs;
2651 vw = VIMMED1(1.f);
2652 }
2653
2654 vw->setName("w");
2655
2656 ubyte semantic_name = swr_fs->info.base.input_semantic_name[attrib];
2657 ubyte semantic_idx = swr_fs->info.base.input_semantic_index[attrib];
2658
2659 if (semantic_name == TGSI_SEMANTIC_FACE) {
2660 Value *ff =
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");
2664
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));
2669 continue;
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"));
2675 } else {
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"));
2678 }
2679 inputs[attrib][2] = wrap(LOAD(pPS, {0, SWR_PS_CONTEXT_vZ}, "vZ"));
2680 inputs[attrib][3] =
2681 wrap(LOAD(pPS, {0, SWR_PS_CONTEXT_vOneOverW, PixelPositions_center}, "vOneOverW"));
2682 continue;
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));
2690 continue;
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));
2698 continue;
2699 }
2700 unsigned linkedAttrib =
2701 locate_linkage(semantic_name, semantic_idx, pPrevShader) - 1;
2702
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;
2708 extraAttribs++;
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);
2714 extraAttribs++;
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
2721 * to keep going.
2722 */
2723 if (semantic_name != TGSI_SEMANTIC_COLOR || !key.light_twoside)
2724 continue;
2725 } else {
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;
2730 }
2731 }
2732
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)
2740 continue;
2741 /* If there is no front color, just always use the back color. */
2742 if (linkedAttrib == 0xFFFFFFFF)
2743 linkedAttrib = bcolorAttrib;
2744
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;
2750 }
2751
2752 unsigned diff = 12 * (bcolorAttrib - linkedAttrib);
2753
2754 if (diff) {
2755 Value *back =
2756 XOR(C(1), LOAD(pPS, {0, SWR_PS_CONTEXT_frontFace}), "backFace");
2757
2758 offset = MUL(back, C(diff));
2759 offset->setName("offset");
2760 }
2761 }
2762 }
2763
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);
2769
2770 if (offset) {
2771 indexA = ADD(indexA, offset);
2772 indexB = ADD(indexB, offset);
2773 indexC = ADD(indexC, offset);
2774 }
2775
2776 Value *va = VBROADCAST(LOAD(GEP(pAttribs, indexA)));
2777 Value *vb = VBROADCAST(LOAD(GEP(pAttribs, indexB)));
2778 Value *vc = VBROADCAST(LOAD(GEP(pAttribs, indexC)));
2779
2780 if (interpMode == TGSI_INTERPOLATE_CONSTANT) {
2781 inputs[attrib][channel] = wrap(va);
2782 } else {
2783 Value *vk = FSUB(FSUB(VIMMED1(1.0f), vi), vj);
2784
2785 vc = FMUL(vk, vc);
2786
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);
2795 }
2796 }
2797 }
2798 }
2799
2800 sampler = swr_sampler_soa_create(key.sampler, PIPE_SHADER_FRAGMENT);
2801
2802 struct lp_bld_tgsi_system_values system_values;
2803 memset(&system_values, 0, sizeof(system_values));
2804
2805 struct lp_build_mask_context mask;
2806 bool uses_mask = false;
2807
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");
2813 }
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);
2820
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)
2827
2828 // grab stipple pattern base address
2829 Value *stipplePtr = GEP(hPrivateData, {0, swr_draw_context_polyStipple, 0});
2830 stipplePtr = BITCAST(stipplePtr, mInt8PtrTy);
2831
2832 // peform a gather to grab stipple words for each lane
2833 Value *vStipple = GATHERDD(VUNDEF_I(), stipplePtr, vYstipple,
2834 VIMMED1(0xffffffff), 4);
2835
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);
2842
2843 if (swr_fs->info.base.uses_kill) {
2844 vActiveMask = AND(vActiveMask, vStippleMask);
2845 } else {
2846 vActiveMask = vStippleMask;
2847 }
2848 }
2849 lp_build_mask_begin(
2850 &mask, gallivm, lp_type_float_vec(32, 32 * 8), wrap(vActiveMask));
2851 uses_mask = true;
2852 }
2853
2854 struct lp_build_tgsi_params params;
2855 memset(&params, 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;
2865
2866 lp_build_tgsi_soa(gallivm,
2867 swr_fs->pipe.tokens,
2868 &params,
2869 outputs);
2870
2871 sampler->destroy(sampler);
2872
2873 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
2874
2875 for (uint32_t attrib = 0; attrib < swr_fs->info.base.num_outputs;
2876 attrib++) {
2877 switch (swr_fs->info.base.output_semantic_name[attrib]) {
2878 case TGSI_SEMANTIC_POSITION: {
2879 // write z
2880 LLVMValueRef outZ =
2881 LLVMBuildLoad(gallivm->builder, outputs[attrib][2], "");
2882 STORE(unwrap(outZ), pPS, {0, SWR_PS_CONTEXT_vZ});
2883 break;
2884 }
2885 case TGSI_SEMANTIC_COLOR: {
2886 for (uint32_t channel = 0; channel < TGSI_NUM_CHANNELS; channel++) {
2887 if (!outputs[attrib][channel])
2888 continue;
2889
2890 LLVMValueRef out =
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++) {
2895 STORE(unwrap(out),
2896 pPS,
2897 {0, SWR_PS_CONTEXT_shaded, rt, channel});
2898 }
2899 } else {
2900 STORE(unwrap(out),
2901 pPS,
2902 {0,
2903 SWR_PS_CONTEXT_shaded,
2904 swr_fs->info.base.output_semantic_index[attrib],
2905 channel});
2906 }
2907 }
2908 break;
2909 }
2910 default: {
2911 fprintf(stderr,
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]);
2916 break;
2917 }
2918 }
2919 }
2920
2921 LLVMValueRef mask_result = 0;
2922 if (uses_mask) {
2923 mask_result = lp_build_mask_end(&mask);
2924 }
2925
2926 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm->builder)));
2927
2928 if (uses_mask) {
2929 STORE(unwrap(mask_result), pPS, {0, SWR_PS_CONTEXT_activeMask});
2930 }
2931
2932 RET_VOID();
2933
2934 gallivm_verify_function(gallivm, wrap(pFunction));
2935
2936 gallivm_compile_module(gallivm);
2937
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);
2942
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");
2947
2948 JM()->mIsModuleFinalized = true;
2949
2950 return kernel;
2951 }
2952
2953 PFN_PIXEL_KERNEL
2954 swr_compile_fs(struct swr_context *ctx, swr_jit_fs_key &key)
2955 {
2956 if (!ctx->fs->pipe.tokens)
2957 return NULL;
2958
2959 BuilderSWR builder(
2960 reinterpret_cast<JitManager *>(swr_screen(ctx->pipe.screen)->hJitMgr),
2961 "FS");
2962 PFN_PIXEL_KERNEL func = builder.CompileFS(ctx, key);
2963
2964 ctx->fs->map.insert(std::make_pair(key, std::make_unique<VariantFS>(builder.gallivm, func)));
2965 return func;
2966 }