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 #if HAVE_LLVM < 0x0500
52 typedef AttributeSet AttributeList
;
56 using namespace SwrJit
;
60 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
);
62 bool operator==(const swr_jit_fs_key
&lhs
, const swr_jit_fs_key
&rhs
)
64 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
67 bool operator==(const swr_jit_vs_key
&lhs
, const swr_jit_vs_key
&rhs
)
69 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
72 bool operator==(const swr_jit_fetch_key
&lhs
, const swr_jit_fetch_key
&rhs
)
74 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
77 bool operator==(const swr_jit_gs_key
&lhs
, const swr_jit_gs_key
&rhs
)
79 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
83 swr_generate_sampler_key(const struct lp_tgsi_info
&info
,
84 struct swr_context
*ctx
,
85 enum pipe_shader_type shader_type
,
86 struct swr_jit_sampler_key
&key
)
88 key
.nr_samplers
= info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
90 for (unsigned i
= 0; i
< key
.nr_samplers
; i
++) {
91 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
92 lp_sampler_static_sampler_state(
93 &key
.sampler
[i
].sampler_state
,
94 ctx
->samplers
[shader_type
][i
]);
99 * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes
100 * are dx10-style? Can't really have mixed opcodes, at least not
101 * if we want to skip the holes here (without rescanning tgsi).
103 if (info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
104 key
.nr_sampler_views
=
105 info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
106 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
107 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER_VIEW
] & (1 << i
)) {
108 const struct pipe_sampler_view
*view
=
109 ctx
->sampler_views
[shader_type
][i
];
110 lp_sampler_static_texture_state(
111 &key
.sampler
[i
].texture_state
, view
);
113 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
114 const struct util_format_description
*desc
=
115 util_format_description(view
->format
);
116 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
117 !util_format_has_depth(desc
))
118 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
123 key
.nr_sampler_views
= key
.nr_samplers
;
124 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
125 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
126 const struct pipe_sampler_view
*view
=
127 ctx
->sampler_views
[shader_type
][i
];
128 lp_sampler_static_texture_state(
129 &key
.sampler
[i
].texture_state
, view
);
131 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
132 const struct util_format_description
*desc
=
133 util_format_description(view
->format
);
134 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
135 !util_format_has_depth(desc
))
136 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
144 swr_generate_fs_key(struct swr_jit_fs_key
&key
,
145 struct swr_context
*ctx
,
146 swr_fragment_shader
*swr_fs
)
148 memset(&key
, 0, sizeof(key
));
150 key
.nr_cbufs
= ctx
->framebuffer
.nr_cbufs
;
151 key
.light_twoside
= ctx
->rasterizer
->light_twoside
;
152 key
.sprite_coord_enable
= ctx
->rasterizer
->sprite_coord_enable
;
154 struct tgsi_shader_info
*pPrevShader
;
156 pPrevShader
= &ctx
->gs
->info
.base
;
158 pPrevShader
= &ctx
->vs
->info
.base
;
160 memcpy(&key
.vs_output_semantic_name
,
161 &pPrevShader
->output_semantic_name
,
162 sizeof(key
.vs_output_semantic_name
));
163 memcpy(&key
.vs_output_semantic_idx
,
164 &pPrevShader
->output_semantic_index
,
165 sizeof(key
.vs_output_semantic_idx
));
167 swr_generate_sampler_key(swr_fs
->info
, ctx
, PIPE_SHADER_FRAGMENT
, key
);
171 swr_generate_vs_key(struct swr_jit_vs_key
&key
,
172 struct swr_context
*ctx
,
173 swr_vertex_shader
*swr_vs
)
175 memset(&key
, 0, sizeof(key
));
177 key
.clip_plane_mask
=
178 swr_vs
->info
.base
.clipdist_writemask
?
179 swr_vs
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
180 ctx
->rasterizer
->clip_plane_enable
;
182 swr_generate_sampler_key(swr_vs
->info
, ctx
, PIPE_SHADER_VERTEX
, key
);
186 swr_generate_fetch_key(struct swr_jit_fetch_key
&key
,
187 struct swr_vertex_element_state
*velems
)
189 memset(&key
, 0, sizeof(key
));
191 key
.fsState
= velems
->fsState
;
195 swr_generate_gs_key(struct swr_jit_gs_key
&key
,
196 struct swr_context
*ctx
,
197 swr_geometry_shader
*swr_gs
)
199 memset(&key
, 0, sizeof(key
));
201 struct tgsi_shader_info
*pPrevShader
= &ctx
->vs
->info
.base
;
203 memcpy(&key
.vs_output_semantic_name
,
204 &pPrevShader
->output_semantic_name
,
205 sizeof(key
.vs_output_semantic_name
));
206 memcpy(&key
.vs_output_semantic_idx
,
207 &pPrevShader
->output_semantic_index
,
208 sizeof(key
.vs_output_semantic_idx
));
210 swr_generate_sampler_key(swr_gs
->info
, ctx
, PIPE_SHADER_GEOMETRY
, key
);
213 struct BuilderSWR
: public Builder
{
214 BuilderSWR(JitManager
*pJitMgr
, const char *pName
)
217 pJitMgr
->SetupNewModule();
218 gallivm
= gallivm_create(pName
, wrap(&JM()->mContext
));
219 pJitMgr
->mpCurrentModule
= unwrap(gallivm
->module
);
223 gallivm_free_ir(gallivm
);
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 const uint32_t simdVertexStride
= sizeof(simdvertex
);
374 const uint32_t numSimdBatches
= (pGS
->maxNumVerts
+ 7) / 8;
375 const uint32_t inputPrimStride
= numSimdBatches
* simdVertexStride
;
377 Value
*pStream
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_pStream
});
378 Value
*vMask
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_mask
});
379 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, 8));
381 Value
*vOffsets
= C({
389 inputPrimStride
* 7 } );
391 Value
*vVertexSlot
= ASHR(unwrap(emitted_vertices_vec
), 3);
392 Value
*vSimdSlot
= AND(unwrap(emitted_vertices_vec
), 7);
394 for (uint32_t attrib
= 0; attrib
< iface
->num_outputs
; ++attrib
) {
395 uint32_t attribSlot
= attrib
;
396 if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
)
397 attribSlot
= VERTEX_POINT_SIZE_SLOT
;
398 else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_PRIMID
)
399 attribSlot
= VERTEX_PRIMID_SLOT
;
400 else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_LAYER
)
401 attribSlot
= VERTEX_RTAI_SLOT
;
403 Value
*vOffsetsAttrib
=
404 ADD(vOffsets
, MUL(vVertexSlot
, VIMMED1((uint32_t)sizeof(simdvertex
))));
406 ADD(vOffsetsAttrib
, VIMMED1((uint32_t)(attribSlot
*sizeof(simdvector
))));
408 ADD(vOffsetsAttrib
, MUL(vSimdSlot
, VIMMED1((uint32_t)sizeof(float))));
410 for (uint32_t channel
= 0; channel
< 4; ++channel
) {
411 Value
*vData
= LOAD(unwrap(outputs
[attrib
][channel
]));
412 Value
*vPtrs
= GEP(pStream
, vOffsetsAttrib
);
414 vPtrs
= BITCAST(vPtrs
,
415 VectorType::get(PointerType::get(mFP32Ty
, 0), 8));
417 MASKED_SCATTER(vData
, vPtrs
, 32, vMask1
);
420 ADD(vOffsetsAttrib
, VIMMED1((uint32_t)sizeof(simdscalar
)));
426 BuilderSWR::swr_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
427 struct lp_build_tgsi_context
* bld_base
,
428 LLVMValueRef verts_per_prim_vec
,
429 LLVMValueRef emitted_prims_vec
)
431 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
432 SWR_GS_STATE
*pGS
= iface
->pGsState
;
434 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
437 LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pCutOrStreamIdBuffer
});
438 Value
*vMask
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_mask
});
439 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, 8));
441 uint32_t vertsPerPrim
= iface
->num_verts_per_prim
;
444 ADD(MUL(unwrap(emitted_prims_vec
), VIMMED1(vertsPerPrim
)),
445 unwrap(verts_per_prim_vec
));
447 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
448 vCount
= LOAD(unwrap(bld
->total_emitted_vertices_vec_ptr
));
450 struct lp_exec_mask
*exec_mask
= &bld
->exec_mask
;
451 Value
*mask
= unwrap(lp_build_mask_value(bld
->mask
));
452 if (exec_mask
->has_mask
)
453 mask
= AND(mask
, unwrap(exec_mask
->exec_mask
));
455 Value
*cmpMask
= VMASK(ICMP_NE(unwrap(verts_per_prim_vec
), VIMMED1(0)));
456 mask
= AND(mask
, cmpMask
);
457 vMask1
= TRUNC(mask
, VectorType::get(mInt1Ty
, 8));
459 const uint32_t cutPrimStride
=
460 (pGS
->maxNumVerts
+ JM()->mVWidth
- 1) / JM()->mVWidth
;
461 Value
*vOffsets
= C({
462 (uint32_t)(cutPrimStride
* 0),
463 (uint32_t)(cutPrimStride
* 1),
464 (uint32_t)(cutPrimStride
* 2),
465 (uint32_t)(cutPrimStride
* 3),
466 (uint32_t)(cutPrimStride
* 4),
467 (uint32_t)(cutPrimStride
* 5),
468 (uint32_t)(cutPrimStride
* 6),
469 (uint32_t)(cutPrimStride
* 7) } );
471 vCount
= SUB(vCount
, VIMMED1(1));
472 Value
*vOffset
= ADD(UDIV(vCount
, VIMMED1(8)), vOffsets
);
473 Value
*vValue
= SHL(VIMMED1(1), UREM(vCount
, VIMMED1(8)));
475 vValue
= TRUNC(vValue
, VectorType::get(mInt8Ty
, 8));
477 Value
*vPtrs
= GEP(pCutBuffer
, vOffset
);
479 BITCAST(vPtrs
, VectorType::get(PointerType::get(mInt8Ty
, 0), JM()->mVWidth
));
481 Value
*vGather
= MASKED_GATHER(vPtrs
, 32, vMask1
);
482 vValue
= OR(vGather
, vValue
);
483 MASKED_SCATTER(vValue
, vPtrs
, 32, vMask1
);
487 BuilderSWR::swr_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
488 struct lp_build_tgsi_context
* bld_base
,
489 LLVMValueRef total_emitted_vertices_vec
,
490 LLVMValueRef emitted_prims_vec
)
492 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
493 SWR_GS_STATE
*pGS
= iface
->pGsState
;
495 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
497 STORE(unwrap(total_emitted_vertices_vec
), iface
->pGsCtx
, {0, SWR_GS_CONTEXT_vertexCount
});
501 BuilderSWR::CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
503 SWR_GS_STATE
*pGS
= &ctx
->gs
->gsState
;
504 struct tgsi_shader_info
*info
= &ctx
->gs
->info
.base
;
506 pGS
->gsEnable
= true;
508 pGS
->numInputAttribs
= info
->num_inputs
;
509 pGS
->outputTopology
=
510 swr_convert_prim_topology(info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
511 pGS
->maxNumVerts
= info
->properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
512 pGS
->instanceCount
= info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
];
514 pGS
->emitsRenderTargetArrayIndex
= info
->writes_layer
;
515 pGS
->emitsPrimitiveID
= info
->writes_primid
;
516 pGS
->emitsViewportArrayIndex
= info
->writes_viewport_index
;
518 // XXX: single stream for now...
519 pGS
->isSingleStream
= true;
520 pGS
->singleStreamID
= 0;
522 struct swr_geometry_shader
*gs
= ctx
->gs
;
524 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
525 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
527 memset(outputs
, 0, sizeof(outputs
));
529 AttrBuilder attrBuilder
;
530 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
531 AttributeList attrSet
= AttributeList::get(
532 JM()->mContext
, AttributeList::FunctionIndex
, attrBuilder
);
534 std::vector
<Type
*> gsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
535 PointerType::get(Gen_SWR_GS_CONTEXT(JM()), 0)};
536 FunctionType
*vsFuncType
=
537 FunctionType::get(Type::getVoidTy(JM()->mContext
), gsArgs
, false);
539 // create new vertex shader function
540 auto pFunction
= Function::Create(vsFuncType
,
541 GlobalValue::ExternalLinkage
,
543 JM()->mpCurrentModule
);
544 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrSet
);
546 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
547 IRB()->SetInsertPoint(block
);
548 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
550 auto argitr
= pFunction
->arg_begin();
551 Value
*hPrivateData
= &*argitr
++;
552 hPrivateData
->setName("hPrivateData");
553 Value
*pGsCtx
= &*argitr
++;
554 pGsCtx
->setName("gsCtx");
557 GEP(hPrivateData
, {C(0), C(swr_draw_context_constantGS
)});
558 consts_ptr
->setName("gs_constants");
559 Value
*const_sizes_ptr
=
560 GEP(hPrivateData
, {0, swr_draw_context_num_constantsGS
});
561 const_sizes_ptr
->setName("num_gs_constants");
563 struct lp_build_sampler_soa
*sampler
=
564 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_GEOMETRY
);
566 struct lp_bld_tgsi_system_values system_values
;
567 memset(&system_values
, 0, sizeof(system_values
));
568 system_values
.prim_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_PrimitiveID
}));
569 system_values
.instance_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_InstanceID
}));
571 std::vector
<Constant
*> mapConstants
;
572 Value
*vtxAttribMap
= ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
573 for (unsigned slot
= 0; slot
< info
->num_inputs
; slot
++) {
574 ubyte semantic_name
= info
->input_semantic_name
[slot
];
575 ubyte semantic_idx
= info
->input_semantic_index
[slot
];
578 locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
) + 1;
580 STORE(C(vs_slot
), vtxAttribMap
, {0, slot
});
581 mapConstants
.push_back(C(vs_slot
));
584 struct lp_build_mask_context mask
;
585 Value
*mask_val
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_mask
}, "gsMask");
586 lp_build_mask_begin(&mask
, gallivm
,
587 lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
589 // zero out cut buffer so we can load/modify/store bits
590 MEMSET(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_pCutOrStreamIdBuffer
}),
592 pGS
->instanceCount
* ((pGS
->maxNumVerts
+ 7) / 8) * JM()->mVWidth
,
593 sizeof(float) * KNOB_SIMD_WIDTH
);
595 struct swr_gs_llvm_iface gs_iface
;
596 gs_iface
.base
.fetch_input
= ::swr_gs_llvm_fetch_input
;
597 gs_iface
.base
.emit_vertex
= ::swr_gs_llvm_emit_vertex
;
598 gs_iface
.base
.end_primitive
= ::swr_gs_llvm_end_primitive
;
599 gs_iface
.base
.gs_epilogue
= ::swr_gs_llvm_epilogue
;
600 gs_iface
.pBuilder
= this;
601 gs_iface
.pGsCtx
= pGsCtx
;
602 gs_iface
.pGsState
= pGS
;
603 gs_iface
.num_outputs
= gs
->info
.base
.num_outputs
;
604 gs_iface
.num_verts_per_prim
=
605 u_vertices_per_prim((pipe_prim_type
)info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
606 gs_iface
.info
= info
;
607 gs_iface
.pVtxAttribMap
= vtxAttribMap
;
609 lp_build_tgsi_soa(gallivm
,
611 lp_type_float_vec(32, 32 * 8),
614 wrap(const_sizes_ptr
),
618 wrap(hPrivateData
), // (sampler context)
624 lp_build_mask_end(&mask
);
626 sampler
->destroy(sampler
);
628 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
632 gallivm_verify_function(gallivm
, wrap(pFunction
));
633 gallivm_compile_module(gallivm
);
636 (PFN_GS_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
638 debug_printf("geom shader %p\n", pFunc
);
639 assert(pFunc
&& "Error: GeomShader = NULL");
641 JM()->mIsModuleFinalized
= true;
647 swr_compile_gs(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
650 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
652 PFN_GS_FUNC func
= builder
.CompileGS(ctx
, key
);
654 ctx
->gs
->map
.insert(std::make_pair(key
, make_unique
<VariantGS
>(builder
.gallivm
, func
)));
659 BuilderSWR::CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
661 struct swr_vertex_shader
*swr_vs
= ctx
->vs
;
663 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
664 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
666 memset(outputs
, 0, sizeof(outputs
));
668 AttrBuilder attrBuilder
;
669 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
670 AttributeList attrSet
= AttributeList::get(
671 JM()->mContext
, AttributeList::FunctionIndex
, attrBuilder
);
673 std::vector
<Type
*> vsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
674 PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};
675 FunctionType
*vsFuncType
=
676 FunctionType::get(Type::getVoidTy(JM()->mContext
), vsArgs
, false);
678 // create new vertex shader function
679 auto pFunction
= Function::Create(vsFuncType
,
680 GlobalValue::ExternalLinkage
,
682 JM()->mpCurrentModule
);
683 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrSet
);
685 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
686 IRB()->SetInsertPoint(block
);
687 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
689 auto argitr
= pFunction
->arg_begin();
690 Value
*hPrivateData
= &*argitr
++;
691 hPrivateData
->setName("hPrivateData");
692 Value
*pVsCtx
= &*argitr
++;
693 pVsCtx
->setName("vsCtx");
695 Value
*consts_ptr
= GEP(hPrivateData
, {C(0), C(swr_draw_context_constantVS
)});
697 consts_ptr
->setName("vs_constants");
698 Value
*const_sizes_ptr
=
699 GEP(hPrivateData
, {0, swr_draw_context_num_constantsVS
});
700 const_sizes_ptr
->setName("num_vs_constants");
702 Value
*vtxInput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVin
});
704 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
705 const unsigned mask
= swr_vs
->info
.base
.input_usage_mask
[attrib
];
706 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
707 if (mask
& (1 << channel
)) {
708 inputs
[attrib
][channel
] =
709 wrap(LOAD(vtxInput
, {0, 0, attrib
, channel
}));
714 struct lp_build_sampler_soa
*sampler
=
715 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_VERTEX
);
717 struct lp_bld_tgsi_system_values system_values
;
718 memset(&system_values
, 0, sizeof(system_values
));
719 system_values
.instance_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_InstanceID
}));
720 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID
}));
722 lp_build_tgsi_soa(gallivm
,
724 lp_type_float_vec(32, 32 * 8),
727 wrap(const_sizes_ptr
),
731 wrap(hPrivateData
), // (sampler context)
735 NULL
); // geometry shader face
737 sampler
->destroy(sampler
);
739 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
741 Value
*vtxOutput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVout
});
743 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
744 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
745 if (!outputs
[attrib
][channel
])
748 Value
*val
= LOAD(unwrap(outputs
[attrib
][channel
]));
750 uint32_t outSlot
= attrib
;
751 if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
)
752 outSlot
= VERTEX_POINT_SIZE_SLOT
;
753 STORE(val
, vtxOutput
, {0, 0, outSlot
, channel
});
757 if (ctx
->rasterizer
->clip_plane_enable
||
758 swr_vs
->info
.base
.culldist_writemask
) {
759 unsigned clip_mask
= ctx
->rasterizer
->clip_plane_enable
;
762 if (swr_vs
->info
.base
.writes_clipvertex
) {
763 cv
= 1 + locate_linkage(TGSI_SEMANTIC_CLIPVERTEX
, 0,
766 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
767 if (swr_vs
->info
.base
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
768 swr_vs
->info
.base
.output_semantic_index
[i
] == 0) {
774 LLVMValueRef cx
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][0], "");
775 LLVMValueRef cy
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][1], "");
776 LLVMValueRef cz
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][2], "");
777 LLVMValueRef cw
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][3], "");
779 for (unsigned val
= 0; val
< PIPE_MAX_CLIP_PLANES
; val
++) {
780 // clip distance overrides user clip planes
781 if ((swr_vs
->info
.base
.clipdist_writemask
& clip_mask
& (1 << val
)) ||
782 ((swr_vs
->info
.base
.culldist_writemask
<< swr_vs
->info
.base
.num_written_clipdistance
) & (1 << val
))) {
783 unsigned cv
= 1 + locate_linkage(TGSI_SEMANTIC_CLIPDIST
, val
< 4 ? 0 : 1,
786 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
], "");
787 STORE(unwrap(dist
), vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
});
789 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
- 4], "");
790 STORE(unwrap(dist
), vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4});
795 if (!(clip_mask
& (1 << val
)))
798 Value
*px
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 0}));
799 Value
*py
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 1}));
800 Value
*pz
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 2}));
801 Value
*pw
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 3}));
802 Value
*dist
= FADD(FMUL(unwrap(cx
), VBROADCAST(px
)),
803 FADD(FMUL(unwrap(cy
), VBROADCAST(py
)),
804 FADD(FMUL(unwrap(cz
), VBROADCAST(pz
)),
805 FMUL(unwrap(cw
), VBROADCAST(pw
)))));
808 STORE(dist
, vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
});
810 STORE(dist
, vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4});
816 gallivm_verify_function(gallivm
, wrap(pFunction
));
817 gallivm_compile_module(gallivm
);
819 // lp_debug_dump_value(func);
821 PFN_VERTEX_FUNC pFunc
=
822 (PFN_VERTEX_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
824 debug_printf("vert shader %p\n", pFunc
);
825 assert(pFunc
&& "Error: VertShader = NULL");
827 JM()->mIsModuleFinalized
= true;
833 swr_compile_vs(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
835 if (!ctx
->vs
->pipe
.tokens
)
839 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
841 PFN_VERTEX_FUNC func
= builder
.CompileVS(ctx
, key
);
843 ctx
->vs
->map
.insert(std::make_pair(key
, make_unique
<VariantVS
>(builder
.gallivm
, func
)));
848 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
)
850 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
851 if ((info
->output_semantic_name
[i
] == name
)
852 && (info
->output_semantic_index
[i
] == index
)) {
853 return i
- 1; // position is not part of the linkage
861 BuilderSWR::CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
863 struct swr_fragment_shader
*swr_fs
= ctx
->fs
;
865 struct tgsi_shader_info
*pPrevShader
;
867 pPrevShader
= &ctx
->gs
->info
.base
;
869 pPrevShader
= &ctx
->vs
->info
.base
;
871 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
872 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
874 memset(inputs
, 0, sizeof(inputs
));
875 memset(outputs
, 0, sizeof(outputs
));
877 struct lp_build_sampler_soa
*sampler
= NULL
;
879 AttrBuilder attrBuilder
;
880 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
881 AttributeList attrSet
= AttributeList::get(
882 JM()->mContext
, AttributeList::FunctionIndex
, attrBuilder
);
884 std::vector
<Type
*> fsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
885 PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};
886 FunctionType
*funcType
=
887 FunctionType::get(Type::getVoidTy(JM()->mContext
), fsArgs
, false);
889 auto pFunction
= Function::Create(funcType
,
890 GlobalValue::ExternalLinkage
,
892 JM()->mpCurrentModule
);
893 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrSet
);
895 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
896 IRB()->SetInsertPoint(block
);
897 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
899 auto args
= pFunction
->arg_begin();
900 Value
*hPrivateData
= &*args
++;
901 hPrivateData
->setName("hPrivateData");
902 Value
*pPS
= &*args
++;
903 pPS
->setName("psCtx");
905 Value
*consts_ptr
= GEP(hPrivateData
, {0, swr_draw_context_constantFS
});
906 consts_ptr
->setName("fs_constants");
907 Value
*const_sizes_ptr
=
908 GEP(hPrivateData
, {0, swr_draw_context_num_constantsFS
});
909 const_sizes_ptr
->setName("num_fs_constants");
911 // load *pAttribs, *pPerspAttribs
912 Value
*pRawAttribs
= LOAD(pPS
, {0, SWR_PS_CONTEXT_pAttribs
}, "pRawAttribs");
913 Value
*pPerspAttribs
=
914 LOAD(pPS
, {0, SWR_PS_CONTEXT_pPerspAttribs
}, "pPerspAttribs");
916 swr_fs
->constantMask
= 0;
917 swr_fs
->flatConstantMask
= 0;
918 swr_fs
->pointSpriteMask
= 0;
920 for (int attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
921 const unsigned mask
= swr_fs
->info
.base
.input_usage_mask
[attrib
];
922 const unsigned interpMode
= swr_fs
->info
.base
.input_interpolate
[attrib
];
923 const unsigned interpLoc
= swr_fs
->info
.base
.input_interpolate_loc
[attrib
];
929 Value
*vi
= nullptr, *vj
= nullptr;
931 case TGSI_INTERPOLATE_LOC_CENTER
:
932 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_center
}, "i");
933 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_center
}, "j");
935 case TGSI_INTERPOLATE_LOC_CENTROID
:
936 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_centroid
}, "i");
937 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_centroid
}, "j");
939 case TGSI_INTERPOLATE_LOC_SAMPLE
:
940 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_sample
}, "i");
941 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_sample
}, "j");
946 Value
*vw
= nullptr, *pAttribs
;
947 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
948 interpMode
== TGSI_INTERPOLATE_COLOR
) {
949 pAttribs
= pPerspAttribs
;
951 case TGSI_INTERPOLATE_LOC_CENTER
:
952 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}));
954 case TGSI_INTERPOLATE_LOC_CENTROID
:
955 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_centroid
}));
957 case TGSI_INTERPOLATE_LOC_SAMPLE
:
958 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_sample
}));
962 pAttribs
= pRawAttribs
;
968 ubyte semantic_name
= swr_fs
->info
.base
.input_semantic_name
[attrib
];
969 ubyte semantic_idx
= swr_fs
->info
.base
.input_semantic_index
[attrib
];
971 if (semantic_name
== TGSI_SEMANTIC_FACE
) {
973 UI_TO_FP(LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), mFP32Ty
);
974 ff
= FSUB(FMUL(ff
, C(2.0f
)), C(1.0f
));
975 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vFrontFace");
977 inputs
[attrib
][0] = wrap(ff
);
978 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
979 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
980 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
982 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
) { // gl_FragCoord
983 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
984 TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
) {
985 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_center
}, "vX"));
986 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_center
}, "vY"));
988 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
}, "vX"));
989 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
}, "vY"));
991 inputs
[attrib
][2] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vZ
}, "vZ"));
993 wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}, "vOneOverW"));
995 } else if (semantic_name
== TGSI_SEMANTIC_PRIMID
) {
996 Value
*primID
= LOAD(pPS
, {0, SWR_PS_CONTEXT_primID
}, "primID");
997 inputs
[attrib
][0] = wrap(VECTOR_SPLAT(JM()->mVWidth
, primID
));
998 inputs
[attrib
][1] = wrap(VIMMED1(0));
999 inputs
[attrib
][2] = wrap(VIMMED1(0));
1000 inputs
[attrib
][3] = wrap(VIMMED1(0));
1004 unsigned linkedAttrib
=
1005 locate_linkage(semantic_name
, semantic_idx
, pPrevShader
);
1007 if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1008 key
.sprite_coord_enable
& (1 << semantic_idx
)) {
1009 /* we add an extra attrib to the backendState in swr_update_derived. */
1010 linkedAttrib
= pPrevShader
->num_outputs
- 1;
1011 swr_fs
->pointSpriteMask
|= (1 << linkedAttrib
);
1012 } else if (linkedAttrib
== 0xFFFFFFFF) {
1013 inputs
[attrib
][0] = wrap(VIMMED1(0.0f
));
1014 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1015 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1016 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
1017 /* If we're reading in color and 2-sided lighting is enabled, we have
1020 if (semantic_name
!= TGSI_SEMANTIC_COLOR
|| !key
.light_twoside
)
1023 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1024 swr_fs
->constantMask
|= 1 << linkedAttrib
;
1025 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1026 swr_fs
->flatConstantMask
|= 1 << linkedAttrib
;
1030 unsigned bcolorAttrib
= 0xFFFFFFFF;
1031 Value
*offset
= NULL
;
1032 if (semantic_name
== TGSI_SEMANTIC_COLOR
&& key
.light_twoside
) {
1033 bcolorAttrib
= locate_linkage(
1034 TGSI_SEMANTIC_BCOLOR
, semantic_idx
, pPrevShader
);
1035 /* Neither front nor back colors were available. Nothing to load. */
1036 if (bcolorAttrib
== 0xFFFFFFFF && linkedAttrib
== 0xFFFFFFFF)
1038 /* If there is no front color, just always use the back color. */
1039 if (linkedAttrib
== 0xFFFFFFFF)
1040 linkedAttrib
= bcolorAttrib
;
1042 if (bcolorAttrib
!= 0xFFFFFFFF) {
1043 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1044 swr_fs
->constantMask
|= 1 << bcolorAttrib
;
1045 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1046 swr_fs
->flatConstantMask
|= 1 << bcolorAttrib
;
1049 unsigned diff
= 12 * (bcolorAttrib
- linkedAttrib
);
1053 XOR(C(1), LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), "backFace");
1055 offset
= MUL(back
, C(diff
));
1056 offset
->setName("offset");
1061 for (int channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1062 if (mask
& (1 << channel
)) {
1063 Value
*indexA
= C(linkedAttrib
* 12 + channel
);
1064 Value
*indexB
= C(linkedAttrib
* 12 + channel
+ 4);
1065 Value
*indexC
= C(linkedAttrib
* 12 + channel
+ 8);
1068 indexA
= ADD(indexA
, offset
);
1069 indexB
= ADD(indexB
, offset
);
1070 indexC
= ADD(indexC
, offset
);
1073 Value
*va
= VBROADCAST(LOAD(GEP(pAttribs
, indexA
)));
1074 Value
*vb
= VBROADCAST(LOAD(GEP(pAttribs
, indexB
)));
1075 Value
*vc
= VBROADCAST(LOAD(GEP(pAttribs
, indexC
)));
1077 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1078 inputs
[attrib
][channel
] = wrap(va
);
1080 Value
*vk
= FSUB(FSUB(VIMMED1(1.0f
), vi
), vj
);
1084 Value
*interp
= FMUL(va
, vi
);
1085 Value
*interp1
= FMUL(vb
, vj
);
1086 interp
= FADD(interp
, interp1
);
1087 interp
= FADD(interp
, vc
);
1088 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
1089 interpMode
== TGSI_INTERPOLATE_COLOR
)
1090 interp
= FMUL(interp
, vw
);
1091 inputs
[attrib
][channel
] = wrap(interp
);
1097 sampler
= swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_FRAGMENT
);
1099 struct lp_bld_tgsi_system_values system_values
;
1100 memset(&system_values
, 0, sizeof(system_values
));
1102 struct lp_build_mask_context mask
;
1104 if (swr_fs
->info
.base
.uses_kill
) {
1105 Value
*mask_val
= LOAD(pPS
, {0, SWR_PS_CONTEXT_activeMask
}, "activeMask");
1106 lp_build_mask_begin(
1107 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
1110 lp_build_tgsi_soa(gallivm
,
1111 swr_fs
->pipe
.tokens
,
1112 lp_type_float_vec(32, 32 * 8),
1113 swr_fs
->info
.base
.uses_kill
? &mask
: NULL
, // mask
1115 wrap(const_sizes_ptr
),
1120 NULL
, // thread data
1123 NULL
); // geometry shader face
1125 sampler
->destroy(sampler
);
1127 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1129 for (uint32_t attrib
= 0; attrib
< swr_fs
->info
.base
.num_outputs
;
1131 switch (swr_fs
->info
.base
.output_semantic_name
[attrib
]) {
1132 case TGSI_SEMANTIC_POSITION
: {
1135 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][2], "");
1136 STORE(unwrap(outZ
), pPS
, {0, SWR_PS_CONTEXT_vZ
});
1139 case TGSI_SEMANTIC_COLOR
: {
1140 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1141 if (!outputs
[attrib
][channel
])
1145 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][channel
], "");
1146 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
1147 swr_fs
->info
.base
.output_semantic_index
[attrib
] == 0) {
1148 for (uint32_t rt
= 0; rt
< key
.nr_cbufs
; rt
++) {
1151 {0, SWR_PS_CONTEXT_shaded
, rt
, channel
});
1157 SWR_PS_CONTEXT_shaded
,
1158 swr_fs
->info
.base
.output_semantic_index
[attrib
],
1166 "unknown output from FS %s[%d]\n",
1167 tgsi_semantic_names
[swr_fs
->info
.base
1168 .output_semantic_name
[attrib
]],
1169 swr_fs
->info
.base
.output_semantic_index
[attrib
]);
1175 LLVMValueRef mask_result
= 0;
1176 if (swr_fs
->info
.base
.uses_kill
) {
1177 mask_result
= lp_build_mask_end(&mask
);
1180 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1182 if (swr_fs
->info
.base
.uses_kill
) {
1183 STORE(unwrap(mask_result
), pPS
, {0, SWR_PS_CONTEXT_activeMask
});
1188 gallivm_verify_function(gallivm
, wrap(pFunction
));
1190 gallivm_compile_module(gallivm
);
1192 PFN_PIXEL_KERNEL kernel
=
1193 (PFN_PIXEL_KERNEL
)gallivm_jit_function(gallivm
, wrap(pFunction
));
1194 debug_printf("frag shader %p\n", kernel
);
1195 assert(kernel
&& "Error: FragShader = NULL");
1197 JM()->mIsModuleFinalized
= true;
1203 swr_compile_fs(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
1205 if (!ctx
->fs
->pipe
.tokens
)
1209 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
1211 PFN_PIXEL_KERNEL func
= builder
.CompileFS(ctx
, key
);
1213 ctx
->fs
->map
.insert(std::make_pair(key
, make_unique
<VariantFS
>(builder
.gallivm
, func
)));