1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ***************************************************************************/
24 // llvm redefines DEBUG
25 #pragma push_macro("DEBUG")
27 #include "JitManager.h"
28 #include "llvm-c/Core.h"
29 #include "llvm/Support/CBindingWrapping.h"
30 #pragma pop_macro("DEBUG")
33 #include "gen_state_llvm.h"
36 #include "tgsi/tgsi_strings.h"
37 #include "util/u_format.h"
38 #include "util/u_prim.h"
39 #include "gallivm/lp_bld_init.h"
40 #include "gallivm/lp_bld_flow.h"
41 #include "gallivm/lp_bld_struct.h"
42 #include "gallivm/lp_bld_tgsi.h"
44 #include "swr_context.h"
45 #include "gen_swr_context_llvm.h"
46 #include "swr_resource.h"
47 #include "swr_state.h"
48 #include "swr_screen.h"
50 using namespace SwrJit
;
54 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
);
56 bool operator==(const swr_jit_fs_key
&lhs
, const swr_jit_fs_key
&rhs
)
58 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
61 bool operator==(const swr_jit_vs_key
&lhs
, const swr_jit_vs_key
&rhs
)
63 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
66 bool operator==(const swr_jit_fetch_key
&lhs
, const swr_jit_fetch_key
&rhs
)
68 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
71 bool operator==(const swr_jit_gs_key
&lhs
, const swr_jit_gs_key
&rhs
)
73 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
77 swr_generate_sampler_key(const struct lp_tgsi_info
&info
,
78 struct swr_context
*ctx
,
79 enum pipe_shader_type shader_type
,
80 struct swr_jit_sampler_key
&key
)
82 key
.nr_samplers
= info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
84 for (unsigned i
= 0; i
< key
.nr_samplers
; i
++) {
85 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
86 lp_sampler_static_sampler_state(
87 &key
.sampler
[i
].sampler_state
,
88 ctx
->samplers
[shader_type
][i
]);
93 * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes
94 * are dx10-style? Can't really have mixed opcodes, at least not
95 * if we want to skip the holes here (without rescanning tgsi).
97 if (info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
98 key
.nr_sampler_views
=
99 info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
100 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
101 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER_VIEW
] & (1 << i
)) {
102 const struct pipe_sampler_view
*view
=
103 ctx
->sampler_views
[shader_type
][i
];
104 lp_sampler_static_texture_state(
105 &key
.sampler
[i
].texture_state
, view
);
107 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
108 const struct util_format_description
*desc
=
109 util_format_description(view
->format
);
110 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
111 !util_format_has_depth(desc
))
112 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
117 key
.nr_sampler_views
= key
.nr_samplers
;
118 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
119 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
120 const struct pipe_sampler_view
*view
=
121 ctx
->sampler_views
[shader_type
][i
];
122 lp_sampler_static_texture_state(
123 &key
.sampler
[i
].texture_state
, view
);
125 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
126 const struct util_format_description
*desc
=
127 util_format_description(view
->format
);
128 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
129 !util_format_has_depth(desc
))
130 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
138 swr_generate_fs_key(struct swr_jit_fs_key
&key
,
139 struct swr_context
*ctx
,
140 swr_fragment_shader
*swr_fs
)
142 memset(&key
, 0, sizeof(key
));
144 key
.nr_cbufs
= ctx
->framebuffer
.nr_cbufs
;
145 key
.light_twoside
= ctx
->rasterizer
->light_twoside
;
146 key
.sprite_coord_enable
= ctx
->rasterizer
->sprite_coord_enable
;
148 struct tgsi_shader_info
*pPrevShader
;
150 pPrevShader
= &ctx
->gs
->info
.base
;
152 pPrevShader
= &ctx
->vs
->info
.base
;
154 memcpy(&key
.vs_output_semantic_name
,
155 &pPrevShader
->output_semantic_name
,
156 sizeof(key
.vs_output_semantic_name
));
157 memcpy(&key
.vs_output_semantic_idx
,
158 &pPrevShader
->output_semantic_index
,
159 sizeof(key
.vs_output_semantic_idx
));
161 swr_generate_sampler_key(swr_fs
->info
, ctx
, PIPE_SHADER_FRAGMENT
, key
);
163 key
.poly_stipple_enable
= ctx
->rasterizer
->poly_stipple_enable
&&
164 ctx
->poly_stipple
.prim_is_poly
;
168 swr_generate_vs_key(struct swr_jit_vs_key
&key
,
169 struct swr_context
*ctx
,
170 swr_vertex_shader
*swr_vs
)
172 memset(&key
, 0, sizeof(key
));
174 key
.clip_plane_mask
=
175 swr_vs
->info
.base
.clipdist_writemask
?
176 swr_vs
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
177 ctx
->rasterizer
->clip_plane_enable
;
179 swr_generate_sampler_key(swr_vs
->info
, ctx
, PIPE_SHADER_VERTEX
, key
);
183 swr_generate_fetch_key(struct swr_jit_fetch_key
&key
,
184 struct swr_vertex_element_state
*velems
)
186 memset(&key
, 0, sizeof(key
));
188 key
.fsState
= velems
->fsState
;
192 swr_generate_gs_key(struct swr_jit_gs_key
&key
,
193 struct swr_context
*ctx
,
194 swr_geometry_shader
*swr_gs
)
196 memset(&key
, 0, sizeof(key
));
198 struct tgsi_shader_info
*pPrevShader
= &ctx
->vs
->info
.base
;
200 memcpy(&key
.vs_output_semantic_name
,
201 &pPrevShader
->output_semantic_name
,
202 sizeof(key
.vs_output_semantic_name
));
203 memcpy(&key
.vs_output_semantic_idx
,
204 &pPrevShader
->output_semantic_index
,
205 sizeof(key
.vs_output_semantic_idx
));
207 swr_generate_sampler_key(swr_gs
->info
, ctx
, PIPE_SHADER_GEOMETRY
, key
);
210 struct BuilderSWR
: public Builder
{
211 BuilderSWR(JitManager
*pJitMgr
, const char *pName
)
214 pJitMgr
->SetupNewModule();
215 gallivm
= gallivm_create(pName
, wrap(&JM()->mContext
));
216 pJitMgr
->mpCurrentModule
= unwrap(gallivm
->module
);
220 gallivm_free_ir(gallivm
);
223 void WriteVS(Value
*pVal
, Value
*pVsContext
, Value
*pVtxOutput
,
224 unsigned slot
, unsigned channel
);
226 struct gallivm_state
*gallivm
;
227 PFN_VERTEX_FUNC
CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
);
228 PFN_PIXEL_KERNEL
CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
);
229 PFN_GS_FUNC
CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
);
232 swr_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
233 struct lp_build_tgsi_context
* bld_base
,
234 boolean is_vindex_indirect
,
235 LLVMValueRef vertex_index
,
236 boolean is_aindex_indirect
,
237 LLVMValueRef attrib_index
,
238 LLVMValueRef swizzle_index
);
240 swr_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
241 struct lp_build_tgsi_context
* bld_base
,
242 LLVMValueRef (*outputs
)[4],
243 LLVMValueRef emitted_vertices_vec
);
246 swr_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
247 struct lp_build_tgsi_context
* bld_base
,
248 LLVMValueRef verts_per_prim_vec
,
249 LLVMValueRef emitted_prims_vec
);
252 swr_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
253 struct lp_build_tgsi_context
* bld_base
,
254 LLVMValueRef total_emitted_vertices_vec
,
255 LLVMValueRef emitted_prims_vec
);
259 struct swr_gs_llvm_iface
{
260 struct lp_build_tgsi_gs_iface base
;
261 struct tgsi_shader_info
*info
;
263 BuilderSWR
*pBuilder
;
266 SWR_GS_STATE
*pGsState
;
267 uint32_t num_outputs
;
268 uint32_t num_verts_per_prim
;
270 Value
*pVtxAttribMap
;
273 // trampoline functions so we can use the builder llvm construction methods
275 swr_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
276 struct lp_build_tgsi_context
* bld_base
,
277 boolean is_vindex_indirect
,
278 LLVMValueRef vertex_index
,
279 boolean is_aindex_indirect
,
280 LLVMValueRef attrib_index
,
281 LLVMValueRef swizzle_index
)
283 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_iface
;
285 return iface
->pBuilder
->swr_gs_llvm_fetch_input(gs_iface
, bld_base
,
294 swr_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
295 struct lp_build_tgsi_context
* bld_base
,
296 LLVMValueRef (*outputs
)[4],
297 LLVMValueRef emitted_vertices_vec
)
299 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
301 iface
->pBuilder
->swr_gs_llvm_emit_vertex(gs_base
, bld_base
,
303 emitted_vertices_vec
);
307 swr_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
308 struct lp_build_tgsi_context
* bld_base
,
309 LLVMValueRef verts_per_prim_vec
,
310 LLVMValueRef emitted_prims_vec
)
312 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
314 iface
->pBuilder
->swr_gs_llvm_end_primitive(gs_base
, bld_base
,
320 swr_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
321 struct lp_build_tgsi_context
* bld_base
,
322 LLVMValueRef total_emitted_vertices_vec
,
323 LLVMValueRef emitted_prims_vec
)
325 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
327 iface
->pBuilder
->swr_gs_llvm_epilogue(gs_base
, bld_base
,
328 total_emitted_vertices_vec
,
333 BuilderSWR::swr_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
334 struct lp_build_tgsi_context
* bld_base
,
335 boolean is_vindex_indirect
,
336 LLVMValueRef vertex_index
,
337 boolean is_aindex_indirect
,
338 LLVMValueRef attrib_index
,
339 LLVMValueRef swizzle_index
)
341 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_iface
;
343 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
345 assert(is_vindex_indirect
== false && is_aindex_indirect
== false);
348 LOAD(GEP(iface
->pVtxAttribMap
, {C(0), unwrap(attrib_index
)}));
351 LOAD(GEP(iface
->pGsCtx
,
353 C(SWR_GS_CONTEXT_vert
),
354 unwrap(vertex_index
),
357 unwrap(swizzle_index
)}));
363 BuilderSWR::swr_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
364 struct lp_build_tgsi_context
* bld_base
,
365 LLVMValueRef (*outputs
)[4],
366 LLVMValueRef emitted_vertices_vec
)
368 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
369 SWR_GS_STATE
*pGS
= iface
->pGsState
;
371 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
373 #if USE_SIMD16_FRONTEND
374 const uint32_t simdVertexStride
= sizeof(simdvertex
) * 2;
375 const uint32_t numSimdBatches
= (pGS
->maxNumVerts
+ (mVWidth
* 2) - 1) / (mVWidth
* 2);
377 const uint32_t simdVertexStride
= sizeof(simdvertex
);
378 const uint32_t numSimdBatches
= (pGS
->maxNumVerts
+ mVWidth
- 1) / mVWidth
;
380 const uint32_t inputPrimStride
= numSimdBatches
* simdVertexStride
;
382 Value
*pStream
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_pStream
});
383 Value
*vMask
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_mask
});
384 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, 8));
386 Value
*vOffsets
= C({
394 inputPrimStride
* 7 } );
396 #if USE_SIMD16_FRONTEND
397 const uint32_t simdShift
= log2(mVWidth
* 2);
398 Value
*vSimdSlot
= AND(unwrap(emitted_vertices_vec
), (mVWidth
* 2) - 1);
400 const uint32_t simdShift
= log2(mVWidth
);
401 Value
*vSimdSlot
= AND(unwrap(emitted_vertices_vec
), mVWidth
- 1);
403 Value
*vVertexSlot
= ASHR(unwrap(emitted_vertices_vec
), simdShift
);
405 for (uint32_t attrib
= 0; attrib
< iface
->num_outputs
; ++attrib
) {
406 uint32_t attribSlot
= attrib
;
407 uint32_t sgvChannel
= 0;
408 if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
409 attribSlot
= VERTEX_SGV_SLOT
;
410 sgvChannel
= VERTEX_SGV_POINT_SIZE_COMP
;
411 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_LAYER
) {
412 attribSlot
= VERTEX_SGV_SLOT
;
413 sgvChannel
= VERTEX_SGV_RTAI_COMP
;
414 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
415 attribSlot
= VERTEX_POSITION_SLOT
;
417 attribSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
- 1;
420 #if USE_SIMD16_FRONTEND
421 Value
*vOffsetsAttrib
=
422 ADD(vOffsets
, MUL(vVertexSlot
, VIMMED1((uint32_t)sizeof(simdvertex
) * 2)));
424 ADD(vOffsetsAttrib
, VIMMED1((uint32_t)(attribSlot
*sizeof(simdvector
) * 2)));
426 Value
*vOffsetsAttrib
=
427 ADD(vOffsets
, MUL(vVertexSlot
, VIMMED1((uint32_t)sizeof(simdvertex
))));
429 ADD(vOffsetsAttrib
, VIMMED1((uint32_t)(attribSlot
*sizeof(simdvector
))));
432 ADD(vOffsetsAttrib
, MUL(vSimdSlot
, VIMMED1((uint32_t)sizeof(float))));
434 for (uint32_t channel
= 0; channel
< 4; ++channel
) {
435 Value
*vPtrs
= GEP(pStream
, vOffsetsAttrib
);
438 if (attribSlot
== VERTEX_SGV_SLOT
)
439 vData
= LOAD(unwrap(outputs
[attrib
][0]));
441 vData
= LOAD(unwrap(outputs
[attrib
][channel
]));
443 if (attribSlot
!= VERTEX_SGV_SLOT
||
444 sgvChannel
== channel
) {
445 vPtrs
= BITCAST(vPtrs
,
446 VectorType::get(PointerType::get(mFP32Ty
, 0), 8));
448 MASKED_SCATTER(vData
, vPtrs
, 32, vMask1
);
451 #if USE_SIMD16_FRONTEND
453 ADD(vOffsetsAttrib
, VIMMED1((uint32_t)sizeof(simdscalar
) * 2));
456 ADD(vOffsetsAttrib
, VIMMED1((uint32_t)sizeof(simdscalar
)));
463 BuilderSWR::swr_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
464 struct lp_build_tgsi_context
* bld_base
,
465 LLVMValueRef verts_per_prim_vec
,
466 LLVMValueRef emitted_prims_vec
)
468 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
469 SWR_GS_STATE
*pGS
= iface
->pGsState
;
471 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
474 LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pCutOrStreamIdBuffer
});
475 Value
*vMask
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_mask
});
476 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, 8));
478 uint32_t vertsPerPrim
= iface
->num_verts_per_prim
;
481 ADD(MUL(unwrap(emitted_prims_vec
), VIMMED1(vertsPerPrim
)),
482 unwrap(verts_per_prim_vec
));
484 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
485 vCount
= LOAD(unwrap(bld
->total_emitted_vertices_vec_ptr
));
487 struct lp_exec_mask
*exec_mask
= &bld
->exec_mask
;
488 Value
*mask
= unwrap(lp_build_mask_value(bld
->mask
));
489 if (exec_mask
->has_mask
)
490 mask
= AND(mask
, unwrap(exec_mask
->exec_mask
));
492 Value
*cmpMask
= VMASK(ICMP_NE(unwrap(verts_per_prim_vec
), VIMMED1(0)));
493 mask
= AND(mask
, cmpMask
);
494 vMask1
= TRUNC(mask
, VectorType::get(mInt1Ty
, 8));
496 const uint32_t cutPrimStride
=
497 (pGS
->maxNumVerts
+ JM()->mVWidth
- 1) / JM()->mVWidth
;
498 Value
*vOffsets
= C({
499 (uint32_t)(cutPrimStride
* 0),
500 (uint32_t)(cutPrimStride
* 1),
501 (uint32_t)(cutPrimStride
* 2),
502 (uint32_t)(cutPrimStride
* 3),
503 (uint32_t)(cutPrimStride
* 4),
504 (uint32_t)(cutPrimStride
* 5),
505 (uint32_t)(cutPrimStride
* 6),
506 (uint32_t)(cutPrimStride
* 7) } );
508 vCount
= SUB(vCount
, VIMMED1(1));
509 Value
*vOffset
= ADD(UDIV(vCount
, VIMMED1(8)), vOffsets
);
510 Value
*vValue
= SHL(VIMMED1(1), UREM(vCount
, VIMMED1(8)));
512 vValue
= TRUNC(vValue
, VectorType::get(mInt8Ty
, 8));
514 Value
*vPtrs
= GEP(pCutBuffer
, vOffset
);
516 BITCAST(vPtrs
, VectorType::get(PointerType::get(mInt8Ty
, 0), JM()->mVWidth
));
518 Value
*vGather
= MASKED_GATHER(vPtrs
, 32, vMask1
);
519 vValue
= OR(vGather
, vValue
);
520 MASKED_SCATTER(vValue
, vPtrs
, 32, vMask1
);
524 BuilderSWR::swr_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
525 struct lp_build_tgsi_context
* bld_base
,
526 LLVMValueRef total_emitted_vertices_vec
,
527 LLVMValueRef emitted_prims_vec
)
529 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
531 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
533 STORE(unwrap(total_emitted_vertices_vec
), iface
->pGsCtx
, {0, SWR_GS_CONTEXT_vertexCount
});
537 BuilderSWR::CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
539 SWR_GS_STATE
*pGS
= &ctx
->gs
->gsState
;
540 struct tgsi_shader_info
*info
= &ctx
->gs
->info
.base
;
542 pGS
->gsEnable
= true;
544 pGS
->numInputAttribs
= info
->num_inputs
;
545 pGS
->outputTopology
=
546 swr_convert_prim_topology(info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
547 pGS
->maxNumVerts
= info
->properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
548 pGS
->instanceCount
= info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
];
550 pGS
->emitsRenderTargetArrayIndex
= info
->writes_layer
;
551 pGS
->emitsViewportArrayIndex
= info
->writes_viewport_index
;
553 // XXX: single stream for now...
554 pGS
->isSingleStream
= true;
555 pGS
->singleStreamID
= 0;
557 struct swr_geometry_shader
*gs
= ctx
->gs
;
559 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
560 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
562 memset(outputs
, 0, sizeof(outputs
));
564 AttrBuilder attrBuilder
;
565 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
567 std::vector
<Type
*> gsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
568 PointerType::get(Gen_SWR_GS_CONTEXT(JM()), 0)};
569 FunctionType
*vsFuncType
=
570 FunctionType::get(Type::getVoidTy(JM()->mContext
), gsArgs
, false);
572 // create new vertex shader function
573 auto pFunction
= Function::Create(vsFuncType
,
574 GlobalValue::ExternalLinkage
,
576 JM()->mpCurrentModule
);
577 #if HAVE_LLVM < 0x0500
578 AttributeSet attrSet
= AttributeSet::get(
579 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
580 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
582 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
585 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
586 IRB()->SetInsertPoint(block
);
587 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
589 auto argitr
= pFunction
->arg_begin();
590 Value
*hPrivateData
= &*argitr
++;
591 hPrivateData
->setName("hPrivateData");
592 Value
*pGsCtx
= &*argitr
++;
593 pGsCtx
->setName("gsCtx");
596 GEP(hPrivateData
, {C(0), C(swr_draw_context_constantGS
)});
597 consts_ptr
->setName("gs_constants");
598 Value
*const_sizes_ptr
=
599 GEP(hPrivateData
, {0, swr_draw_context_num_constantsGS
});
600 const_sizes_ptr
->setName("num_gs_constants");
602 struct lp_build_sampler_soa
*sampler
=
603 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_GEOMETRY
);
605 struct lp_bld_tgsi_system_values system_values
;
606 memset(&system_values
, 0, sizeof(system_values
));
607 system_values
.prim_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_PrimitiveID
}));
608 system_values
.instance_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_InstanceID
}));
610 std::vector
<Constant
*> mapConstants
;
611 Value
*vtxAttribMap
= ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
612 for (unsigned slot
= 0; slot
< info
->num_inputs
; slot
++) {
613 ubyte semantic_name
= info
->input_semantic_name
[slot
];
614 ubyte semantic_idx
= info
->input_semantic_index
[slot
];
616 unsigned vs_slot
= locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
);
618 vs_slot
+= VERTEX_ATTRIB_START_SLOT
;
620 if (ctx
->vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
623 if (semantic_name
== TGSI_SEMANTIC_POSITION
)
624 vs_slot
= VERTEX_POSITION_SLOT
;
626 STORE(C(vs_slot
), vtxAttribMap
, {0, slot
});
627 mapConstants
.push_back(C(vs_slot
));
630 struct lp_build_mask_context mask
;
631 Value
*mask_val
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_mask
}, "gsMask");
632 lp_build_mask_begin(&mask
, gallivm
,
633 lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
635 // zero out cut buffer so we can load/modify/store bits
636 MEMSET(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_pCutOrStreamIdBuffer
}),
638 pGS
->instanceCount
* ((pGS
->maxNumVerts
+ 7) / 8) * JM()->mVWidth
,
639 sizeof(float) * KNOB_SIMD_WIDTH
);
641 struct swr_gs_llvm_iface gs_iface
;
642 gs_iface
.base
.fetch_input
= ::swr_gs_llvm_fetch_input
;
643 gs_iface
.base
.emit_vertex
= ::swr_gs_llvm_emit_vertex
;
644 gs_iface
.base
.end_primitive
= ::swr_gs_llvm_end_primitive
;
645 gs_iface
.base
.gs_epilogue
= ::swr_gs_llvm_epilogue
;
646 gs_iface
.pBuilder
= this;
647 gs_iface
.pGsCtx
= pGsCtx
;
648 gs_iface
.pGsState
= pGS
;
649 gs_iface
.num_outputs
= gs
->info
.base
.num_outputs
;
650 gs_iface
.num_verts_per_prim
=
651 u_vertices_per_prim((pipe_prim_type
)info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
652 gs_iface
.info
= info
;
653 gs_iface
.pVtxAttribMap
= vtxAttribMap
;
655 lp_build_tgsi_soa(gallivm
,
657 lp_type_float_vec(32, 32 * 8),
660 wrap(const_sizes_ptr
),
664 wrap(hPrivateData
), // (sampler context)
670 lp_build_mask_end(&mask
);
672 sampler
->destroy(sampler
);
674 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
678 gallivm_verify_function(gallivm
, wrap(pFunction
));
679 gallivm_compile_module(gallivm
);
682 (PFN_GS_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
684 debug_printf("geom shader %p\n", pFunc
);
685 assert(pFunc
&& "Error: GeomShader = NULL");
687 JM()->mIsModuleFinalized
= true;
693 swr_compile_gs(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
696 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
698 PFN_GS_FUNC func
= builder
.CompileGS(ctx
, key
);
700 ctx
->gs
->map
.insert(std::make_pair(key
, make_unique
<VariantGS
>(builder
.gallivm
, func
)));
705 BuilderSWR::WriteVS(Value
*pVal
, Value
*pVsContext
, Value
*pVtxOutput
, unsigned slot
, unsigned channel
)
707 #if USE_SIMD16_FRONTEND
708 // interleave the simdvertex components into the dest simd16vertex
709 // slot16offset = slot8offset * 2
710 // comp16offset = comp8offset * 2 + alternateOffset
712 Value
*offset
= LOAD(pVsContext
, { 0, SWR_VS_CONTEXT_AlternateOffset
});
713 Value
*pOut
= GEP(pVtxOutput
, { C(0), C(0), C(slot
* 2), offset
} );
714 STORE(pVal
, pOut
, {channel
* 2});
716 Value
*pOut
= GEP(pVtxOutput
, {0, 0, slot
});
717 STORE(pVal
, pOut
, {0, channel
});
722 BuilderSWR::CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
724 struct swr_vertex_shader
*swr_vs
= ctx
->vs
;
726 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
727 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
729 memset(outputs
, 0, sizeof(outputs
));
731 AttrBuilder attrBuilder
;
732 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
734 std::vector
<Type
*> vsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
735 PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};
736 FunctionType
*vsFuncType
=
737 FunctionType::get(Type::getVoidTy(JM()->mContext
), vsArgs
, false);
739 // create new vertex shader function
740 auto pFunction
= Function::Create(vsFuncType
,
741 GlobalValue::ExternalLinkage
,
743 JM()->mpCurrentModule
);
744 #if HAVE_LLVM < 0x0500
745 AttributeSet attrSet
= AttributeSet::get(
746 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
747 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
749 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
752 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
753 IRB()->SetInsertPoint(block
);
754 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
756 auto argitr
= pFunction
->arg_begin();
757 Value
*hPrivateData
= &*argitr
++;
758 hPrivateData
->setName("hPrivateData");
759 Value
*pVsCtx
= &*argitr
++;
760 pVsCtx
->setName("vsCtx");
762 Value
*consts_ptr
= GEP(hPrivateData
, {C(0), C(swr_draw_context_constantVS
)});
764 consts_ptr
->setName("vs_constants");
765 Value
*const_sizes_ptr
=
766 GEP(hPrivateData
, {0, swr_draw_context_num_constantsVS
});
767 const_sizes_ptr
->setName("num_vs_constants");
769 Value
*vtxInput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVin
});
771 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
772 const unsigned mask
= swr_vs
->info
.base
.input_usage_mask
[attrib
];
773 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
774 if (mask
& (1 << channel
)) {
775 inputs
[attrib
][channel
] =
776 wrap(LOAD(vtxInput
, {0, 0, attrib
, channel
}));
781 struct lp_build_sampler_soa
*sampler
=
782 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_VERTEX
);
784 struct lp_bld_tgsi_system_values system_values
;
785 memset(&system_values
, 0, sizeof(system_values
));
786 system_values
.instance_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_InstanceID
}));
787 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID
}));
789 lp_build_tgsi_soa(gallivm
,
791 lp_type_float_vec(32, 32 * 8),
794 wrap(const_sizes_ptr
),
798 wrap(hPrivateData
), // (sampler context)
802 NULL
); // geometry shader face
804 sampler
->destroy(sampler
);
806 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
808 Value
*vtxOutput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVout
});
810 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
811 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
812 if (!outputs
[attrib
][channel
])
818 if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
819 if (channel
!= VERTEX_SGV_POINT_SIZE_COMP
)
821 val
= LOAD(unwrap(outputs
[attrib
][0]));
822 outSlot
= VERTEX_SGV_SLOT
;
823 } else if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
824 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
825 outSlot
= VERTEX_POSITION_SLOT
;
827 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
828 outSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
;
829 if (swr_vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
833 WriteVS(val
, pVsCtx
, vtxOutput
, outSlot
, channel
);
837 if (ctx
->rasterizer
->clip_plane_enable
||
838 swr_vs
->info
.base
.culldist_writemask
) {
839 unsigned clip_mask
= ctx
->rasterizer
->clip_plane_enable
;
842 if (swr_vs
->info
.base
.writes_clipvertex
) {
843 cv
= locate_linkage(TGSI_SEMANTIC_CLIPVERTEX
, 0,
846 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
847 if (swr_vs
->info
.base
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
848 swr_vs
->info
.base
.output_semantic_index
[i
] == 0) {
854 LLVMValueRef cx
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][0], "");
855 LLVMValueRef cy
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][1], "");
856 LLVMValueRef cz
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][2], "");
857 LLVMValueRef cw
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][3], "");
859 for (unsigned val
= 0; val
< PIPE_MAX_CLIP_PLANES
; val
++) {
860 // clip distance overrides user clip planes
861 if ((swr_vs
->info
.base
.clipdist_writemask
& clip_mask
& (1 << val
)) ||
862 ((swr_vs
->info
.base
.culldist_writemask
<< swr_vs
->info
.base
.num_written_clipdistance
) & (1 << val
))) {
863 unsigned cv
= locate_linkage(TGSI_SEMANTIC_CLIPDIST
, val
< 4 ? 0 : 1,
866 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
], "");
867 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
869 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
- 4], "");
870 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
875 if (!(clip_mask
& (1 << val
)))
878 Value
*px
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 0}));
879 Value
*py
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 1}));
880 Value
*pz
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 2}));
881 Value
*pw
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 3}));
882 Value
*dist
= FADD(FMUL(unwrap(cx
), VBROADCAST(px
)),
883 FADD(FMUL(unwrap(cy
), VBROADCAST(py
)),
884 FADD(FMUL(unwrap(cz
), VBROADCAST(pz
)),
885 FMUL(unwrap(cw
), VBROADCAST(pw
)))));
888 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
890 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
896 gallivm_verify_function(gallivm
, wrap(pFunction
));
897 gallivm_compile_module(gallivm
);
899 // lp_debug_dump_value(func);
901 PFN_VERTEX_FUNC pFunc
=
902 (PFN_VERTEX_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
904 debug_printf("vert shader %p\n", pFunc
);
905 assert(pFunc
&& "Error: VertShader = NULL");
907 JM()->mIsModuleFinalized
= true;
913 swr_compile_vs(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
915 if (!ctx
->vs
->pipe
.tokens
)
919 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
921 PFN_VERTEX_FUNC func
= builder
.CompileVS(ctx
, key
);
923 ctx
->vs
->map
.insert(std::make_pair(key
, make_unique
<VariantVS
>(builder
.gallivm
, func
)));
928 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
)
930 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
931 if ((info
->output_semantic_name
[i
] == name
)
932 && (info
->output_semantic_index
[i
] == index
)) {
941 BuilderSWR::CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
943 struct swr_fragment_shader
*swr_fs
= ctx
->fs
;
945 struct tgsi_shader_info
*pPrevShader
;
947 pPrevShader
= &ctx
->gs
->info
.base
;
949 pPrevShader
= &ctx
->vs
->info
.base
;
951 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
952 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
954 memset(inputs
, 0, sizeof(inputs
));
955 memset(outputs
, 0, sizeof(outputs
));
957 struct lp_build_sampler_soa
*sampler
= NULL
;
959 AttrBuilder attrBuilder
;
960 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
962 std::vector
<Type
*> fsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
963 PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};
964 FunctionType
*funcType
=
965 FunctionType::get(Type::getVoidTy(JM()->mContext
), fsArgs
, false);
967 auto pFunction
= Function::Create(funcType
,
968 GlobalValue::ExternalLinkage
,
970 JM()->mpCurrentModule
);
971 #if HAVE_LLVM < 0x0500
972 AttributeSet attrSet
= AttributeSet::get(
973 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
974 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
976 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
979 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
980 IRB()->SetInsertPoint(block
);
981 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
983 auto args
= pFunction
->arg_begin();
984 Value
*hPrivateData
= &*args
++;
985 hPrivateData
->setName("hPrivateData");
986 Value
*pPS
= &*args
++;
987 pPS
->setName("psCtx");
989 Value
*consts_ptr
= GEP(hPrivateData
, {0, swr_draw_context_constantFS
});
990 consts_ptr
->setName("fs_constants");
991 Value
*const_sizes_ptr
=
992 GEP(hPrivateData
, {0, swr_draw_context_num_constantsFS
});
993 const_sizes_ptr
->setName("num_fs_constants");
995 // load *pAttribs, *pPerspAttribs
996 Value
*pRawAttribs
= LOAD(pPS
, {0, SWR_PS_CONTEXT_pAttribs
}, "pRawAttribs");
997 Value
*pPerspAttribs
=
998 LOAD(pPS
, {0, SWR_PS_CONTEXT_pPerspAttribs
}, "pPerspAttribs");
1000 swr_fs
->constantMask
= 0;
1001 swr_fs
->flatConstantMask
= 0;
1002 swr_fs
->pointSpriteMask
= 0;
1004 for (int attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
1005 const unsigned mask
= swr_fs
->info
.base
.input_usage_mask
[attrib
];
1006 const unsigned interpMode
= swr_fs
->info
.base
.input_interpolate
[attrib
];
1007 const unsigned interpLoc
= swr_fs
->info
.base
.input_interpolate_loc
[attrib
];
1013 Value
*vi
= nullptr, *vj
= nullptr;
1014 switch (interpLoc
) {
1015 case TGSI_INTERPOLATE_LOC_CENTER
:
1016 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_center
}, "i");
1017 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_center
}, "j");
1019 case TGSI_INTERPOLATE_LOC_CENTROID
:
1020 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_centroid
}, "i");
1021 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_centroid
}, "j");
1023 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1024 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_sample
}, "i");
1025 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_sample
}, "j");
1030 Value
*vw
= nullptr, *pAttribs
;
1031 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
1032 interpMode
== TGSI_INTERPOLATE_COLOR
) {
1033 pAttribs
= pPerspAttribs
;
1034 switch (interpLoc
) {
1035 case TGSI_INTERPOLATE_LOC_CENTER
:
1036 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}));
1038 case TGSI_INTERPOLATE_LOC_CENTROID
:
1039 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_centroid
}));
1041 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1042 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_sample
}));
1046 pAttribs
= pRawAttribs
;
1052 ubyte semantic_name
= swr_fs
->info
.base
.input_semantic_name
[attrib
];
1053 ubyte semantic_idx
= swr_fs
->info
.base
.input_semantic_index
[attrib
];
1055 if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1057 UI_TO_FP(LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), mFP32Ty
);
1058 ff
= FSUB(FMUL(ff
, C(2.0f
)), C(1.0f
));
1059 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vFrontFace");
1061 inputs
[attrib
][0] = wrap(ff
);
1062 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1063 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1064 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
1066 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
) { // gl_FragCoord
1067 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
1068 TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
) {
1069 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_center
}, "vX"));
1070 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_center
}, "vY"));
1072 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
}, "vX"));
1073 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
}, "vY"));
1075 inputs
[attrib
][2] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vZ
}, "vZ"));
1077 wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}, "vOneOverW"));
1081 unsigned linkedAttrib
=
1082 locate_linkage(semantic_name
, semantic_idx
, pPrevShader
) - 1;
1084 uint32_t extraAttribs
= 0;
1085 if (semantic_name
== TGSI_SEMANTIC_PRIMID
&& !ctx
->gs
) {
1086 /* non-gs generated primID - need to grab from swizzleMap override */
1087 linkedAttrib
= pPrevShader
->num_outputs
- 1;
1088 swr_fs
->constantMask
|= 1 << linkedAttrib
;
1090 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1091 key
.sprite_coord_enable
& (1 << semantic_idx
)) {
1092 /* we add an extra attrib to the backendState in swr_update_derived. */
1093 linkedAttrib
= pPrevShader
->num_outputs
+ extraAttribs
- 1;
1094 swr_fs
->pointSpriteMask
|= (1 << linkedAttrib
);
1096 } else if (linkedAttrib
== 0xFFFFFFFF) {
1097 inputs
[attrib
][0] = wrap(VIMMED1(0.0f
));
1098 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1099 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1100 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
1101 /* If we're reading in color and 2-sided lighting is enabled, we have
1104 if (semantic_name
!= TGSI_SEMANTIC_COLOR
|| !key
.light_twoside
)
1107 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1108 swr_fs
->constantMask
|= 1 << linkedAttrib
;
1109 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1110 swr_fs
->flatConstantMask
|= 1 << linkedAttrib
;
1114 unsigned bcolorAttrib
= 0xFFFFFFFF;
1115 Value
*offset
= NULL
;
1116 if (semantic_name
== TGSI_SEMANTIC_COLOR
&& key
.light_twoside
) {
1117 bcolorAttrib
= locate_linkage(
1118 TGSI_SEMANTIC_BCOLOR
, semantic_idx
, pPrevShader
) - 1;
1119 /* Neither front nor back colors were available. Nothing to load. */
1120 if (bcolorAttrib
== 0xFFFFFFFF && linkedAttrib
== 0xFFFFFFFF)
1122 /* If there is no front color, just always use the back color. */
1123 if (linkedAttrib
== 0xFFFFFFFF)
1124 linkedAttrib
= bcolorAttrib
;
1126 if (bcolorAttrib
!= 0xFFFFFFFF) {
1127 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1128 swr_fs
->constantMask
|= 1 << bcolorAttrib
;
1129 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1130 swr_fs
->flatConstantMask
|= 1 << bcolorAttrib
;
1133 unsigned diff
= 12 * (bcolorAttrib
- linkedAttrib
);
1137 XOR(C(1), LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), "backFace");
1139 offset
= MUL(back
, C(diff
));
1140 offset
->setName("offset");
1145 for (int channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1146 if (mask
& (1 << channel
)) {
1147 Value
*indexA
= C(linkedAttrib
* 12 + channel
);
1148 Value
*indexB
= C(linkedAttrib
* 12 + channel
+ 4);
1149 Value
*indexC
= C(linkedAttrib
* 12 + channel
+ 8);
1152 indexA
= ADD(indexA
, offset
);
1153 indexB
= ADD(indexB
, offset
);
1154 indexC
= ADD(indexC
, offset
);
1157 Value
*va
= VBROADCAST(LOAD(GEP(pAttribs
, indexA
)));
1158 Value
*vb
= VBROADCAST(LOAD(GEP(pAttribs
, indexB
)));
1159 Value
*vc
= VBROADCAST(LOAD(GEP(pAttribs
, indexC
)));
1161 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1162 inputs
[attrib
][channel
] = wrap(va
);
1164 Value
*vk
= FSUB(FSUB(VIMMED1(1.0f
), vi
), vj
);
1168 Value
*interp
= FMUL(va
, vi
);
1169 Value
*interp1
= FMUL(vb
, vj
);
1170 interp
= FADD(interp
, interp1
);
1171 interp
= FADD(interp
, vc
);
1172 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
1173 interpMode
== TGSI_INTERPOLATE_COLOR
)
1174 interp
= FMUL(interp
, vw
);
1175 inputs
[attrib
][channel
] = wrap(interp
);
1181 sampler
= swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_FRAGMENT
);
1183 struct lp_bld_tgsi_system_values system_values
;
1184 memset(&system_values
, 0, sizeof(system_values
));
1186 struct lp_build_mask_context mask
;
1187 bool uses_mask
= false;
1189 if (swr_fs
->info
.base
.uses_kill
||
1190 key
.poly_stipple_enable
) {
1191 Value
*vActiveMask
= NULL
;
1192 if (swr_fs
->info
.base
.uses_kill
) {
1193 vActiveMask
= LOAD(pPS
, {0, SWR_PS_CONTEXT_activeMask
}, "activeMask");
1195 if (key
.poly_stipple_enable
) {
1196 // first get fragment xy coords and clip to stipple bounds
1197 Value
*vXf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
});
1198 Value
*vYf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
});
1199 Value
*vXu
= FP_TO_UI(vXf
, mSimdInt32Ty
);
1200 Value
*vYu
= FP_TO_UI(vYf
, mSimdInt32Ty
);
1202 // stipple pattern is 32x32, which means that one line of stipple
1203 // is stored in one word:
1204 // vXstipple is bit offset inside 32-bit stipple word
1205 // vYstipple is word index is stipple array
1206 Value
*vXstipple
= AND(vXu
, VIMMED1(0x1f)); // & (32-1)
1207 Value
*vYstipple
= AND(vYu
, VIMMED1(0x1f)); // & (32-1)
1209 // grab stipple pattern base address
1210 Value
*stipplePtr
= GEP(hPrivateData
, {0, swr_draw_context_polyStipple
, 0});
1211 stipplePtr
= BITCAST(stipplePtr
, mInt8PtrTy
);
1213 // peform a gather to grab stipple words for each lane
1214 Value
*vStipple
= GATHERDD(VUNDEF_I(), stipplePtr
, vYstipple
,
1215 VIMMED1(0xffffffff), C((char)4));
1217 // create a mask with one bit corresponding to the x stipple
1218 // and AND it with the pattern, to see if we have a bit
1219 Value
*vBitMask
= LSHR(VIMMED1(0x80000000), vXstipple
);
1220 Value
*vStippleMask
= AND(vStipple
, vBitMask
);
1221 vStippleMask
= ICMP_NE(vStippleMask
, VIMMED1(0));
1222 vStippleMask
= VMASK(vStippleMask
);
1224 if (swr_fs
->info
.base
.uses_kill
) {
1225 vActiveMask
= AND(vActiveMask
, vStippleMask
);
1227 vActiveMask
= vStippleMask
;
1230 lp_build_mask_begin(
1231 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(vActiveMask
));
1235 lp_build_tgsi_soa(gallivm
,
1236 swr_fs
->pipe
.tokens
,
1237 lp_type_float_vec(32, 32 * 8),
1238 uses_mask
? &mask
: NULL
, // mask
1240 wrap(const_sizes_ptr
),
1245 NULL
, // thread data
1248 NULL
); // geometry shader face
1250 sampler
->destroy(sampler
);
1252 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1254 for (uint32_t attrib
= 0; attrib
< swr_fs
->info
.base
.num_outputs
;
1256 switch (swr_fs
->info
.base
.output_semantic_name
[attrib
]) {
1257 case TGSI_SEMANTIC_POSITION
: {
1260 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][2], "");
1261 STORE(unwrap(outZ
), pPS
, {0, SWR_PS_CONTEXT_vZ
});
1264 case TGSI_SEMANTIC_COLOR
: {
1265 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1266 if (!outputs
[attrib
][channel
])
1270 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][channel
], "");
1271 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
1272 swr_fs
->info
.base
.output_semantic_index
[attrib
] == 0) {
1273 for (uint32_t rt
= 0; rt
< key
.nr_cbufs
; rt
++) {
1276 {0, SWR_PS_CONTEXT_shaded
, rt
, channel
});
1282 SWR_PS_CONTEXT_shaded
,
1283 swr_fs
->info
.base
.output_semantic_index
[attrib
],
1291 "unknown output from FS %s[%d]\n",
1292 tgsi_semantic_names
[swr_fs
->info
.base
1293 .output_semantic_name
[attrib
]],
1294 swr_fs
->info
.base
.output_semantic_index
[attrib
]);
1300 LLVMValueRef mask_result
= 0;
1302 mask_result
= lp_build_mask_end(&mask
);
1305 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1308 STORE(unwrap(mask_result
), pPS
, {0, SWR_PS_CONTEXT_activeMask
});
1313 gallivm_verify_function(gallivm
, wrap(pFunction
));
1315 gallivm_compile_module(gallivm
);
1317 PFN_PIXEL_KERNEL kernel
=
1318 (PFN_PIXEL_KERNEL
)gallivm_jit_function(gallivm
, wrap(pFunction
));
1319 debug_printf("frag shader %p\n", kernel
);
1320 assert(kernel
&& "Error: FragShader = NULL");
1322 JM()->mIsModuleFinalized
= true;
1328 swr_compile_fs(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
1330 if (!ctx
->fs
->pipe
.tokens
)
1334 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
1336 PFN_PIXEL_KERNEL func
= builder
.CompileFS(ctx
, key
);
1338 ctx
->fs
->map
.insert(std::make_pair(key
, make_unique
<VariantFS
>(builder
.gallivm
, func
)));