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
)}));
350 Value
*pVertex
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pVerts
});
351 Value
*pInputVertStride
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_inputVertStride
});
353 Value
*pVector
= ADD(MUL(unwrap(vertex_index
), pInputVertStride
), attrib
);
355 Value
*pInput
= LOAD(GEP(pVertex
, {pVector
, unwrap(swizzle_index
)}));
360 // GS output stream layout
361 #define VERTEX_COUNT_SIZE 32
362 #define CONTROL_HEADER_SIZE (8*32)
365 BuilderSWR::swr_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
366 struct lp_build_tgsi_context
* bld_base
,
367 LLVMValueRef (*outputs
)[4],
368 LLVMValueRef emitted_vertices_vec
)
370 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
372 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
374 const uint32_t headerSize
= VERTEX_COUNT_SIZE
+ CONTROL_HEADER_SIZE
;
375 const uint32_t attribSize
= 4 * sizeof(float);
376 const uint32_t vertSize
= attribSize
* SWR_VTX_NUM_SLOTS
;
377 Value
*pVertexOffset
= MUL(unwrap(emitted_vertices_vec
), VIMMED1(vertSize
));
379 Value
*vMask
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_mask
});
380 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, mVWidth
));
382 Value
*pStack
= STACKSAVE();
383 Value
*pTmpPtr
= ALLOCA(mFP32Ty
, C(4)); // used for dummy write for lane masking
385 for (uint32_t attrib
= 0; attrib
< iface
->num_outputs
; ++attrib
) {
386 uint32_t attribSlot
= attrib
;
387 uint32_t sgvChannel
= 0;
388 if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
389 attribSlot
= VERTEX_SGV_SLOT
;
390 sgvChannel
= VERTEX_SGV_POINT_SIZE_COMP
;
391 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_LAYER
) {
392 attribSlot
= VERTEX_SGV_SLOT
;
393 sgvChannel
= VERTEX_SGV_RTAI_COMP
;
394 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
395 attribSlot
= VERTEX_POSITION_SLOT
;
397 attribSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
;
398 if (iface
->info
->writes_position
) {
403 Value
*pOutputOffset
= ADD(pVertexOffset
, VIMMED1(headerSize
+ attribSize
* attribSlot
)); // + sgvChannel ?
405 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
406 Value
*pLaneOffset
= VEXTRACT(pOutputOffset
, C(lane
));
407 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
408 Value
*pStreamOffset
= GEP(pStream
, pLaneOffset
);
409 pStreamOffset
= BITCAST(pStreamOffset
, mFP32PtrTy
);
411 Value
*pLaneMask
= VEXTRACT(vMask1
, C(lane
));
412 pStreamOffset
= SELECT(pLaneMask
, pStreamOffset
, pTmpPtr
);
414 for (uint32_t channel
= 0; channel
< 4; ++channel
) {
417 if (attribSlot
== VERTEX_SGV_SLOT
)
418 vData
= LOAD(unwrap(outputs
[attrib
][0]));
420 vData
= LOAD(unwrap(outputs
[attrib
][channel
]));
422 if (attribSlot
!= VERTEX_SGV_SLOT
||
423 sgvChannel
== channel
) {
424 vData
= VEXTRACT(vData
, C(lane
));
425 STORE(vData
, pStreamOffset
);
427 pStreamOffset
= GEP(pStreamOffset
, C(1));
432 STACKRESTORE(pStack
);
436 BuilderSWR::swr_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
437 struct lp_build_tgsi_context
* bld_base
,
438 LLVMValueRef verts_per_prim_vec
,
439 LLVMValueRef emitted_prims_vec
)
441 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
443 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
445 Value
*vMask
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_mask
});
446 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, 8));
448 uint32_t vertsPerPrim
= iface
->num_verts_per_prim
;
451 ADD(MUL(unwrap(emitted_prims_vec
), VIMMED1(vertsPerPrim
)),
452 unwrap(verts_per_prim_vec
));
454 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
455 vCount
= LOAD(unwrap(bld
->total_emitted_vertices_vec_ptr
));
457 struct lp_exec_mask
*exec_mask
= &bld
->exec_mask
;
458 Value
*mask
= unwrap(lp_build_mask_value(bld
->mask
));
459 if (exec_mask
->has_mask
)
460 mask
= AND(mask
, unwrap(exec_mask
->exec_mask
));
462 Value
*cmpMask
= VMASK(ICMP_NE(unwrap(verts_per_prim_vec
), VIMMED1(0)));
463 mask
= AND(mask
, cmpMask
);
464 vMask1
= TRUNC(mask
, VectorType::get(mInt1Ty
, 8));
466 vCount
= SUB(vCount
, VIMMED1(1));
467 Value
*vOffset
= ADD(UDIV(vCount
, VIMMED1(8)), VIMMED1(VERTEX_COUNT_SIZE
));
468 Value
*vValue
= SHL(VIMMED1(1), UREM(vCount
, VIMMED1(8)));
470 vValue
= TRUNC(vValue
, VectorType::get(mInt8Ty
, 8));
472 Value
*pStack
= STACKSAVE();
473 Value
*pTmpPtr
= ALLOCA(mInt8Ty
, C(4)); // used for dummy read/write for lane masking
475 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
476 Value
*vLaneOffset
= VEXTRACT(vOffset
, C(lane
));
477 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
478 Value
*pStreamOffset
= GEP(pStream
, vLaneOffset
);
480 Value
*pLaneMask
= VEXTRACT(vMask1
, C(lane
));
481 pStreamOffset
= SELECT(pLaneMask
, pStreamOffset
, pTmpPtr
);
483 Value
*vVal
= LOAD(pStreamOffset
);
484 vVal
= OR(vVal
, VEXTRACT(vValue
, C(lane
)));
485 STORE(vVal
, pStreamOffset
);
488 STACKRESTORE(pStack
);
492 BuilderSWR::swr_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
493 struct lp_build_tgsi_context
* bld_base
,
494 LLVMValueRef total_emitted_vertices_vec
,
495 LLVMValueRef emitted_prims_vec
)
497 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
499 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
501 // Store emit count to each output stream in the first DWORD
502 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
504 Value
* pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
505 pStream
= BITCAST(pStream
, mInt32PtrTy
);
506 Value
* pLaneCount
= VEXTRACT(unwrap(total_emitted_vertices_vec
), C(lane
));
507 STORE(pLaneCount
, pStream
);
512 BuilderSWR::CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
514 SWR_GS_STATE
*pGS
= &ctx
->gs
->gsState
;
515 struct tgsi_shader_info
*info
= &ctx
->gs
->info
.base
;
517 memset(pGS
, 0, sizeof(*pGS
));
519 pGS
->gsEnable
= true;
521 pGS
->numInputAttribs
= info
->num_inputs
;
522 pGS
->outputTopology
=
523 swr_convert_prim_topology(info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
524 pGS
->maxNumVerts
= info
->properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
525 pGS
->instanceCount
= info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
];
527 // XXX: single stream for now...
528 pGS
->isSingleStream
= true;
529 pGS
->singleStreamID
= 0;
531 pGS
->vertexAttribOffset
= VERTEX_ATTRIB_START_SLOT
; // TODO: optimize
532 pGS
->srcVertexAttribOffset
= VERTEX_ATTRIB_START_SLOT
; // TODO: optimize
533 pGS
->inputVertStride
= pGS
->numInputAttribs
+ pGS
->vertexAttribOffset
;
534 pGS
->outputVertexSize
= SWR_VTX_NUM_SLOTS
;
535 pGS
->controlDataSize
= 8; // GS ouputs max of 8 32B units
536 pGS
->controlDataOffset
= VERTEX_COUNT_SIZE
;
537 pGS
->outputVertexOffset
= pGS
->controlDataOffset
+ CONTROL_HEADER_SIZE
;
539 pGS
->allocationSize
=
540 VERTEX_COUNT_SIZE
+ // vertex count
541 CONTROL_HEADER_SIZE
+ // control header
542 (SWR_VTX_NUM_SLOTS
* 16) * // sizeof vertex
543 pGS
->maxNumVerts
; // num verts
545 struct swr_geometry_shader
*gs
= ctx
->gs
;
547 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
548 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
550 memset(outputs
, 0, sizeof(outputs
));
552 AttrBuilder attrBuilder
;
553 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
555 std::vector
<Type
*> gsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
556 PointerType::get(Gen_SWR_GS_CONTEXT(JM()), 0)};
557 FunctionType
*vsFuncType
=
558 FunctionType::get(Type::getVoidTy(JM()->mContext
), gsArgs
, false);
560 // create new vertex shader function
561 auto pFunction
= Function::Create(vsFuncType
,
562 GlobalValue::ExternalLinkage
,
564 JM()->mpCurrentModule
);
565 #if HAVE_LLVM < 0x0500
566 AttributeSet attrSet
= AttributeSet::get(
567 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
568 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
570 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
573 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
574 IRB()->SetInsertPoint(block
);
575 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
577 auto argitr
= pFunction
->arg_begin();
578 Value
*hPrivateData
= &*argitr
++;
579 hPrivateData
->setName("hPrivateData");
580 Value
*pGsCtx
= &*argitr
++;
581 pGsCtx
->setName("gsCtx");
584 GEP(hPrivateData
, {C(0), C(swr_draw_context_constantGS
)});
585 consts_ptr
->setName("gs_constants");
586 Value
*const_sizes_ptr
=
587 GEP(hPrivateData
, {0, swr_draw_context_num_constantsGS
});
588 const_sizes_ptr
->setName("num_gs_constants");
590 struct lp_build_sampler_soa
*sampler
=
591 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_GEOMETRY
);
593 struct lp_bld_tgsi_system_values system_values
;
594 memset(&system_values
, 0, sizeof(system_values
));
595 system_values
.prim_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_PrimitiveID
}));
596 system_values
.instance_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_InstanceID
}));
598 std::vector
<Constant
*> mapConstants
;
599 Value
*vtxAttribMap
= ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
600 for (unsigned slot
= 0; slot
< info
->num_inputs
; slot
++) {
601 ubyte semantic_name
= info
->input_semantic_name
[slot
];
602 ubyte semantic_idx
= info
->input_semantic_index
[slot
];
604 unsigned vs_slot
= locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
);
606 vs_slot
+= VERTEX_ATTRIB_START_SLOT
;
608 if (ctx
->vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
611 if (semantic_name
== TGSI_SEMANTIC_POSITION
)
612 vs_slot
= VERTEX_POSITION_SLOT
;
614 STORE(C(vs_slot
), vtxAttribMap
, {0, slot
});
615 mapConstants
.push_back(C(vs_slot
));
618 struct lp_build_mask_context mask
;
619 Value
*mask_val
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_mask
}, "gsMask");
620 lp_build_mask_begin(&mask
, gallivm
,
621 lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
623 // zero out cut buffer so we can load/modify/store bits
624 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
626 Value
* pStream
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
627 MEMSET(pStream
, C((char)0), VERTEX_COUNT_SIZE
+ CONTROL_HEADER_SIZE
, sizeof(float) * KNOB_SIMD_WIDTH
);
630 struct swr_gs_llvm_iface gs_iface
;
631 gs_iface
.base
.fetch_input
= ::swr_gs_llvm_fetch_input
;
632 gs_iface
.base
.emit_vertex
= ::swr_gs_llvm_emit_vertex
;
633 gs_iface
.base
.end_primitive
= ::swr_gs_llvm_end_primitive
;
634 gs_iface
.base
.gs_epilogue
= ::swr_gs_llvm_epilogue
;
635 gs_iface
.pBuilder
= this;
636 gs_iface
.pGsCtx
= pGsCtx
;
637 gs_iface
.pGsState
= pGS
;
638 gs_iface
.num_outputs
= gs
->info
.base
.num_outputs
;
639 gs_iface
.num_verts_per_prim
=
640 u_vertices_per_prim((pipe_prim_type
)info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
641 gs_iface
.info
= info
;
642 gs_iface
.pVtxAttribMap
= vtxAttribMap
;
644 lp_build_tgsi_soa(gallivm
,
646 lp_type_float_vec(32, 32 * 8),
649 wrap(const_sizes_ptr
),
653 wrap(hPrivateData
), // (sampler context)
659 lp_build_mask_end(&mask
);
661 sampler
->destroy(sampler
);
663 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
667 gallivm_verify_function(gallivm
, wrap(pFunction
));
668 gallivm_compile_module(gallivm
);
671 (PFN_GS_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
673 debug_printf("geom shader %p\n", pFunc
);
674 assert(pFunc
&& "Error: GeomShader = NULL");
676 JM()->mIsModuleFinalized
= true;
682 swr_compile_gs(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
685 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
687 PFN_GS_FUNC func
= builder
.CompileGS(ctx
, key
);
689 ctx
->gs
->map
.insert(std::make_pair(key
, make_unique
<VariantGS
>(builder
.gallivm
, func
)));
694 BuilderSWR::WriteVS(Value
*pVal
, Value
*pVsContext
, Value
*pVtxOutput
, unsigned slot
, unsigned channel
)
696 #if USE_SIMD16_FRONTEND && !USE_SIMD16_SHADERS
697 // interleave the simdvertex components into the dest simd16vertex
698 // slot16offset = slot8offset * 2
699 // comp16offset = comp8offset * 2 + alternateOffset
701 Value
*offset
= LOAD(pVsContext
, { 0, SWR_VS_CONTEXT_AlternateOffset
});
702 Value
*pOut
= GEP(pVtxOutput
, { C(0), C(0), C(slot
* 2), offset
} );
703 STORE(pVal
, pOut
, {channel
* 2});
705 Value
*pOut
= GEP(pVtxOutput
, {0, 0, slot
});
706 STORE(pVal
, pOut
, {0, channel
});
711 BuilderSWR::CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
713 struct swr_vertex_shader
*swr_vs
= ctx
->vs
;
715 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
716 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
718 memset(outputs
, 0, sizeof(outputs
));
720 AttrBuilder attrBuilder
;
721 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
723 std::vector
<Type
*> vsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
724 PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};
725 FunctionType
*vsFuncType
=
726 FunctionType::get(Type::getVoidTy(JM()->mContext
), vsArgs
, false);
728 // create new vertex shader function
729 auto pFunction
= Function::Create(vsFuncType
,
730 GlobalValue::ExternalLinkage
,
732 JM()->mpCurrentModule
);
733 #if HAVE_LLVM < 0x0500
734 AttributeSet attrSet
= AttributeSet::get(
735 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
736 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
738 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
741 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
742 IRB()->SetInsertPoint(block
);
743 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
745 auto argitr
= pFunction
->arg_begin();
746 Value
*hPrivateData
= &*argitr
++;
747 hPrivateData
->setName("hPrivateData");
748 Value
*pVsCtx
= &*argitr
++;
749 pVsCtx
->setName("vsCtx");
751 Value
*consts_ptr
= GEP(hPrivateData
, {C(0), C(swr_draw_context_constantVS
)});
753 consts_ptr
->setName("vs_constants");
754 Value
*const_sizes_ptr
=
755 GEP(hPrivateData
, {0, swr_draw_context_num_constantsVS
});
756 const_sizes_ptr
->setName("num_vs_constants");
758 Value
*vtxInput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVin
});
759 #if USE_SIMD16_SHADERS
760 vtxInput
= BITCAST(vtxInput
, PointerType::get(Gen_simd16vertex(JM()), 0));
763 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
764 const unsigned mask
= swr_vs
->info
.base
.input_usage_mask
[attrib
];
765 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
766 if (mask
& (1 << channel
)) {
767 inputs
[attrib
][channel
] =
768 wrap(LOAD(vtxInput
, {0, 0, attrib
, channel
}));
773 struct lp_build_sampler_soa
*sampler
=
774 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_VERTEX
);
776 struct lp_bld_tgsi_system_values system_values
;
777 memset(&system_values
, 0, sizeof(system_values
));
778 system_values
.instance_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_InstanceID
}));
779 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID
}));
781 lp_build_tgsi_soa(gallivm
,
783 lp_type_float_vec(32, 32 * mVWidth
),
786 wrap(const_sizes_ptr
),
790 wrap(hPrivateData
), // (sampler context)
794 NULL
); // geometry shader face
796 sampler
->destroy(sampler
);
798 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
800 Value
*vtxOutput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVout
});
801 #if USE_SIMD16_SHADERS
802 vtxOutput
= BITCAST(vtxOutput
, PointerType::get(Gen_simd16vertex(JM()), 0));
805 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
806 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
807 if (!outputs
[attrib
][channel
])
813 if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
814 if (channel
!= VERTEX_SGV_POINT_SIZE_COMP
)
816 val
= LOAD(unwrap(outputs
[attrib
][0]));
817 outSlot
= VERTEX_SGV_SLOT
;
818 } else if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
819 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
820 outSlot
= VERTEX_POSITION_SLOT
;
822 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
823 outSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
;
824 if (swr_vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
828 WriteVS(val
, pVsCtx
, vtxOutput
, outSlot
, channel
);
832 if (ctx
->rasterizer
->clip_plane_enable
||
833 swr_vs
->info
.base
.culldist_writemask
) {
834 unsigned clip_mask
= ctx
->rasterizer
->clip_plane_enable
;
837 if (swr_vs
->info
.base
.writes_clipvertex
) {
838 cv
= locate_linkage(TGSI_SEMANTIC_CLIPVERTEX
, 0,
841 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
842 if (swr_vs
->info
.base
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
843 swr_vs
->info
.base
.output_semantic_index
[i
] == 0) {
849 LLVMValueRef cx
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][0], "");
850 LLVMValueRef cy
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][1], "");
851 LLVMValueRef cz
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][2], "");
852 LLVMValueRef cw
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][3], "");
854 for (unsigned val
= 0; val
< PIPE_MAX_CLIP_PLANES
; val
++) {
855 // clip distance overrides user clip planes
856 if ((swr_vs
->info
.base
.clipdist_writemask
& clip_mask
& (1 << val
)) ||
857 ((swr_vs
->info
.base
.culldist_writemask
<< swr_vs
->info
.base
.num_written_clipdistance
) & (1 << val
))) {
858 unsigned cv
= locate_linkage(TGSI_SEMANTIC_CLIPDIST
, val
< 4 ? 0 : 1,
861 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
], "");
862 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
864 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
- 4], "");
865 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
870 if (!(clip_mask
& (1 << val
)))
873 Value
*px
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 0}));
874 Value
*py
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 1}));
875 Value
*pz
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 2}));
876 Value
*pw
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 3}));
877 Value
*dist
= FADD(FMUL(unwrap(cx
), VBROADCAST(px
)),
878 FADD(FMUL(unwrap(cy
), VBROADCAST(py
)),
879 FADD(FMUL(unwrap(cz
), VBROADCAST(pz
)),
880 FMUL(unwrap(cw
), VBROADCAST(pw
)))));
883 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
885 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
891 gallivm_verify_function(gallivm
, wrap(pFunction
));
892 gallivm_compile_module(gallivm
);
894 // lp_debug_dump_value(func);
896 PFN_VERTEX_FUNC pFunc
=
897 (PFN_VERTEX_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
899 debug_printf("vert shader %p\n", pFunc
);
900 assert(pFunc
&& "Error: VertShader = NULL");
902 JM()->mIsModuleFinalized
= true;
908 swr_compile_vs(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
910 if (!ctx
->vs
->pipe
.tokens
)
914 #if USE_SIMD16_SHADERS
915 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr16
),
917 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
920 PFN_VERTEX_FUNC func
= builder
.CompileVS(ctx
, key
);
922 ctx
->vs
->map
.insert(std::make_pair(key
, make_unique
<VariantVS
>(builder
.gallivm
, func
)));
927 swr_so_adjust_attrib(unsigned in_attrib
,
928 swr_vertex_shader
*swr_vs
)
933 attrib
= in_attrib
+ VERTEX_ATTRIB_START_SLOT
;
936 semantic_name
= swr_vs
->info
.base
.output_semantic_name
[in_attrib
];
937 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
938 attrib
= VERTEX_POSITION_SLOT
;
939 } else if (semantic_name
== TGSI_SEMANTIC_PSIZE
) {
940 attrib
= VERTEX_SGV_SLOT
;
941 } else if (semantic_name
== TGSI_SEMANTIC_LAYER
) {
942 attrib
= VERTEX_SGV_SLOT
;
944 if (swr_vs
->info
.base
.writes_position
) {
954 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
)
956 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
957 if ((info
->output_semantic_name
[i
] == name
)
958 && (info
->output_semantic_index
[i
] == index
)) {
967 BuilderSWR::CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
969 struct swr_fragment_shader
*swr_fs
= ctx
->fs
;
971 struct tgsi_shader_info
*pPrevShader
;
973 pPrevShader
= &ctx
->gs
->info
.base
;
975 pPrevShader
= &ctx
->vs
->info
.base
;
977 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
978 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
980 memset(inputs
, 0, sizeof(inputs
));
981 memset(outputs
, 0, sizeof(outputs
));
983 struct lp_build_sampler_soa
*sampler
= NULL
;
985 AttrBuilder attrBuilder
;
986 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
988 std::vector
<Type
*> fsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
989 PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};
990 FunctionType
*funcType
=
991 FunctionType::get(Type::getVoidTy(JM()->mContext
), fsArgs
, false);
993 auto pFunction
= Function::Create(funcType
,
994 GlobalValue::ExternalLinkage
,
996 JM()->mpCurrentModule
);
997 #if HAVE_LLVM < 0x0500
998 AttributeSet attrSet
= AttributeSet::get(
999 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
1000 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
1002 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
1005 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
1006 IRB()->SetInsertPoint(block
);
1007 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
1009 auto args
= pFunction
->arg_begin();
1010 Value
*hPrivateData
= &*args
++;
1011 hPrivateData
->setName("hPrivateData");
1012 Value
*pPS
= &*args
++;
1013 pPS
->setName("psCtx");
1015 Value
*consts_ptr
= GEP(hPrivateData
, {0, swr_draw_context_constantFS
});
1016 consts_ptr
->setName("fs_constants");
1017 Value
*const_sizes_ptr
=
1018 GEP(hPrivateData
, {0, swr_draw_context_num_constantsFS
});
1019 const_sizes_ptr
->setName("num_fs_constants");
1021 // load *pAttribs, *pPerspAttribs
1022 Value
*pRawAttribs
= LOAD(pPS
, {0, SWR_PS_CONTEXT_pAttribs
}, "pRawAttribs");
1023 Value
*pPerspAttribs
=
1024 LOAD(pPS
, {0, SWR_PS_CONTEXT_pPerspAttribs
}, "pPerspAttribs");
1026 swr_fs
->constantMask
= 0;
1027 swr_fs
->flatConstantMask
= 0;
1028 swr_fs
->pointSpriteMask
= 0;
1030 for (int attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
1031 const unsigned mask
= swr_fs
->info
.base
.input_usage_mask
[attrib
];
1032 const unsigned interpMode
= swr_fs
->info
.base
.input_interpolate
[attrib
];
1033 const unsigned interpLoc
= swr_fs
->info
.base
.input_interpolate_loc
[attrib
];
1039 Value
*vi
= nullptr, *vj
= nullptr;
1040 switch (interpLoc
) {
1041 case TGSI_INTERPOLATE_LOC_CENTER
:
1042 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_center
}, "i");
1043 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_center
}, "j");
1045 case TGSI_INTERPOLATE_LOC_CENTROID
:
1046 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_centroid
}, "i");
1047 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_centroid
}, "j");
1049 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1050 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_sample
}, "i");
1051 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_sample
}, "j");
1056 Value
*vw
= nullptr, *pAttribs
;
1057 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
1058 interpMode
== TGSI_INTERPOLATE_COLOR
) {
1059 pAttribs
= pPerspAttribs
;
1060 switch (interpLoc
) {
1061 case TGSI_INTERPOLATE_LOC_CENTER
:
1062 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}));
1064 case TGSI_INTERPOLATE_LOC_CENTROID
:
1065 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_centroid
}));
1067 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1068 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_sample
}));
1072 pAttribs
= pRawAttribs
;
1078 ubyte semantic_name
= swr_fs
->info
.base
.input_semantic_name
[attrib
];
1079 ubyte semantic_idx
= swr_fs
->info
.base
.input_semantic_index
[attrib
];
1081 if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1083 UI_TO_FP(LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), mFP32Ty
);
1084 ff
= FSUB(FMUL(ff
, C(2.0f
)), C(1.0f
));
1085 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vFrontFace");
1087 inputs
[attrib
][0] = wrap(ff
);
1088 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1089 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1090 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
1092 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
) { // gl_FragCoord
1093 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
1094 TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
) {
1095 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_center
}, "vX"));
1096 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_center
}, "vY"));
1098 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
}, "vX"));
1099 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
}, "vY"));
1101 inputs
[attrib
][2] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vZ
}, "vZ"));
1103 wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}, "vOneOverW"));
1107 unsigned linkedAttrib
=
1108 locate_linkage(semantic_name
, semantic_idx
, pPrevShader
) - 1;
1110 uint32_t extraAttribs
= 0;
1111 if (semantic_name
== TGSI_SEMANTIC_PRIMID
&& !ctx
->gs
) {
1112 /* non-gs generated primID - need to grab from swizzleMap override */
1113 linkedAttrib
= pPrevShader
->num_outputs
- 1;
1114 swr_fs
->constantMask
|= 1 << linkedAttrib
;
1116 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1117 key
.sprite_coord_enable
& (1 << semantic_idx
)) {
1118 /* we add an extra attrib to the backendState in swr_update_derived. */
1119 linkedAttrib
= pPrevShader
->num_outputs
+ extraAttribs
- 1;
1120 swr_fs
->pointSpriteMask
|= (1 << linkedAttrib
);
1122 } else if (linkedAttrib
== 0xFFFFFFFF) {
1123 inputs
[attrib
][0] = wrap(VIMMED1(0.0f
));
1124 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1125 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1126 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
1127 /* If we're reading in color and 2-sided lighting is enabled, we have
1130 if (semantic_name
!= TGSI_SEMANTIC_COLOR
|| !key
.light_twoside
)
1133 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1134 swr_fs
->constantMask
|= 1 << linkedAttrib
;
1135 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1136 swr_fs
->flatConstantMask
|= 1 << linkedAttrib
;
1140 unsigned bcolorAttrib
= 0xFFFFFFFF;
1141 Value
*offset
= NULL
;
1142 if (semantic_name
== TGSI_SEMANTIC_COLOR
&& key
.light_twoside
) {
1143 bcolorAttrib
= locate_linkage(
1144 TGSI_SEMANTIC_BCOLOR
, semantic_idx
, pPrevShader
) - 1;
1145 /* Neither front nor back colors were available. Nothing to load. */
1146 if (bcolorAttrib
== 0xFFFFFFFF && linkedAttrib
== 0xFFFFFFFF)
1148 /* If there is no front color, just always use the back color. */
1149 if (linkedAttrib
== 0xFFFFFFFF)
1150 linkedAttrib
= bcolorAttrib
;
1152 if (bcolorAttrib
!= 0xFFFFFFFF) {
1153 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1154 swr_fs
->constantMask
|= 1 << bcolorAttrib
;
1155 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1156 swr_fs
->flatConstantMask
|= 1 << bcolorAttrib
;
1159 unsigned diff
= 12 * (bcolorAttrib
- linkedAttrib
);
1163 XOR(C(1), LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), "backFace");
1165 offset
= MUL(back
, C(diff
));
1166 offset
->setName("offset");
1171 for (int channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1172 if (mask
& (1 << channel
)) {
1173 Value
*indexA
= C(linkedAttrib
* 12 + channel
);
1174 Value
*indexB
= C(linkedAttrib
* 12 + channel
+ 4);
1175 Value
*indexC
= C(linkedAttrib
* 12 + channel
+ 8);
1178 indexA
= ADD(indexA
, offset
);
1179 indexB
= ADD(indexB
, offset
);
1180 indexC
= ADD(indexC
, offset
);
1183 Value
*va
= VBROADCAST(LOAD(GEP(pAttribs
, indexA
)));
1184 Value
*vb
= VBROADCAST(LOAD(GEP(pAttribs
, indexB
)));
1185 Value
*vc
= VBROADCAST(LOAD(GEP(pAttribs
, indexC
)));
1187 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1188 inputs
[attrib
][channel
] = wrap(va
);
1190 Value
*vk
= FSUB(FSUB(VIMMED1(1.0f
), vi
), vj
);
1194 Value
*interp
= FMUL(va
, vi
);
1195 Value
*interp1
= FMUL(vb
, vj
);
1196 interp
= FADD(interp
, interp1
);
1197 interp
= FADD(interp
, vc
);
1198 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
1199 interpMode
== TGSI_INTERPOLATE_COLOR
)
1200 interp
= FMUL(interp
, vw
);
1201 inputs
[attrib
][channel
] = wrap(interp
);
1207 sampler
= swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_FRAGMENT
);
1209 struct lp_bld_tgsi_system_values system_values
;
1210 memset(&system_values
, 0, sizeof(system_values
));
1212 struct lp_build_mask_context mask
;
1213 bool uses_mask
= false;
1215 if (swr_fs
->info
.base
.uses_kill
||
1216 key
.poly_stipple_enable
) {
1217 Value
*vActiveMask
= NULL
;
1218 if (swr_fs
->info
.base
.uses_kill
) {
1219 vActiveMask
= LOAD(pPS
, {0, SWR_PS_CONTEXT_activeMask
}, "activeMask");
1221 if (key
.poly_stipple_enable
) {
1222 // first get fragment xy coords and clip to stipple bounds
1223 Value
*vXf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
});
1224 Value
*vYf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
});
1225 Value
*vXu
= FP_TO_UI(vXf
, mSimdInt32Ty
);
1226 Value
*vYu
= FP_TO_UI(vYf
, mSimdInt32Ty
);
1228 // stipple pattern is 32x32, which means that one line of stipple
1229 // is stored in one word:
1230 // vXstipple is bit offset inside 32-bit stipple word
1231 // vYstipple is word index is stipple array
1232 Value
*vXstipple
= AND(vXu
, VIMMED1(0x1f)); // & (32-1)
1233 Value
*vYstipple
= AND(vYu
, VIMMED1(0x1f)); // & (32-1)
1235 // grab stipple pattern base address
1236 Value
*stipplePtr
= GEP(hPrivateData
, {0, swr_draw_context_polyStipple
, 0});
1237 stipplePtr
= BITCAST(stipplePtr
, mInt8PtrTy
);
1239 // peform a gather to grab stipple words for each lane
1240 Value
*vStipple
= GATHERDD(VUNDEF_I(), stipplePtr
, vYstipple
,
1241 VIMMED1(0xffffffff), 4);
1243 // create a mask with one bit corresponding to the x stipple
1244 // and AND it with the pattern, to see if we have a bit
1245 Value
*vBitMask
= LSHR(VIMMED1(0x80000000), vXstipple
);
1246 Value
*vStippleMask
= AND(vStipple
, vBitMask
);
1247 vStippleMask
= ICMP_NE(vStippleMask
, VIMMED1(0));
1248 vStippleMask
= VMASK(vStippleMask
);
1250 if (swr_fs
->info
.base
.uses_kill
) {
1251 vActiveMask
= AND(vActiveMask
, vStippleMask
);
1253 vActiveMask
= vStippleMask
;
1256 lp_build_mask_begin(
1257 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(vActiveMask
));
1261 lp_build_tgsi_soa(gallivm
,
1262 swr_fs
->pipe
.tokens
,
1263 lp_type_float_vec(32, 32 * 8),
1264 uses_mask
? &mask
: NULL
, // mask
1266 wrap(const_sizes_ptr
),
1271 NULL
, // thread data
1274 NULL
); // geometry shader face
1276 sampler
->destroy(sampler
);
1278 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1280 for (uint32_t attrib
= 0; attrib
< swr_fs
->info
.base
.num_outputs
;
1282 switch (swr_fs
->info
.base
.output_semantic_name
[attrib
]) {
1283 case TGSI_SEMANTIC_POSITION
: {
1286 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][2], "");
1287 STORE(unwrap(outZ
), pPS
, {0, SWR_PS_CONTEXT_vZ
});
1290 case TGSI_SEMANTIC_COLOR
: {
1291 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1292 if (!outputs
[attrib
][channel
])
1296 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][channel
], "");
1297 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
1298 swr_fs
->info
.base
.output_semantic_index
[attrib
] == 0) {
1299 for (uint32_t rt
= 0; rt
< key
.nr_cbufs
; rt
++) {
1302 {0, SWR_PS_CONTEXT_shaded
, rt
, channel
});
1308 SWR_PS_CONTEXT_shaded
,
1309 swr_fs
->info
.base
.output_semantic_index
[attrib
],
1317 "unknown output from FS %s[%d]\n",
1318 tgsi_semantic_names
[swr_fs
->info
.base
1319 .output_semantic_name
[attrib
]],
1320 swr_fs
->info
.base
.output_semantic_index
[attrib
]);
1326 LLVMValueRef mask_result
= 0;
1328 mask_result
= lp_build_mask_end(&mask
);
1331 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1334 STORE(unwrap(mask_result
), pPS
, {0, SWR_PS_CONTEXT_activeMask
});
1339 gallivm_verify_function(gallivm
, wrap(pFunction
));
1341 gallivm_compile_module(gallivm
);
1343 PFN_PIXEL_KERNEL kernel
=
1344 (PFN_PIXEL_KERNEL
)gallivm_jit_function(gallivm
, wrap(pFunction
));
1345 debug_printf("frag shader %p\n", kernel
);
1346 assert(kernel
&& "Error: FragShader = NULL");
1348 JM()->mIsModuleFinalized
= true;
1354 swr_compile_fs(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
1356 if (!ctx
->fs
->pipe
.tokens
)
1360 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
1362 PFN_PIXEL_KERNEL func
= builder
.CompileFS(ctx
, key
);
1364 ctx
->fs
->map
.insert(std::make_pair(key
, make_unique
<VariantFS
>(builder
.gallivm
, func
)));