1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ***************************************************************************/
24 #include <llvm/Config/llvm-config.h>
26 #if LLVM_VERSION_MAJOR < 7
27 // llvm redefines DEBUG
28 #pragma push_macro("DEBUG")
32 #include "JitManager.h"
33 #include "llvm-c/Core.h"
34 #include "llvm/Support/CBindingWrapping.h"
35 #include "llvm/IR/LegacyPassManager.h"
37 #if LLVM_VERSION_MAJOR < 7
38 #pragma pop_macro("DEBUG")
42 #include "gen_state_llvm.h"
44 #include "functionpasses/passes.h"
46 #include "tgsi/tgsi_strings.h"
47 #include "util/u_format.h"
48 #include "util/u_prim.h"
49 #include "gallivm/lp_bld_init.h"
50 #include "gallivm/lp_bld_flow.h"
51 #include "gallivm/lp_bld_struct.h"
52 #include "gallivm/lp_bld_tgsi.h"
54 #include "swr_context.h"
55 #include "gen_surf_state_llvm.h"
56 #include "gen_swr_context_llvm.h"
57 #include "swr_resource.h"
58 #include "swr_state.h"
59 #include "swr_screen.h"
61 using namespace SwrJit
;
65 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
);
67 bool operator==(const swr_jit_fs_key
&lhs
, const swr_jit_fs_key
&rhs
)
69 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
72 bool operator==(const swr_jit_vs_key
&lhs
, const swr_jit_vs_key
&rhs
)
74 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
77 bool operator==(const swr_jit_fetch_key
&lhs
, const swr_jit_fetch_key
&rhs
)
79 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
82 bool operator==(const swr_jit_gs_key
&lhs
, const swr_jit_gs_key
&rhs
)
84 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
88 swr_generate_sampler_key(const struct lp_tgsi_info
&info
,
89 struct swr_context
*ctx
,
90 enum pipe_shader_type shader_type
,
91 struct swr_jit_sampler_key
&key
)
93 key
.nr_samplers
= info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
95 for (unsigned i
= 0; i
< key
.nr_samplers
; i
++) {
96 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
97 lp_sampler_static_sampler_state(
98 &key
.sampler
[i
].sampler_state
,
99 ctx
->samplers
[shader_type
][i
]);
104 * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes
105 * are dx10-style? Can't really have mixed opcodes, at least not
106 * if we want to skip the holes here (without rescanning tgsi).
108 if (info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
109 key
.nr_sampler_views
=
110 info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
111 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
112 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER_VIEW
] & (1u << (i
& 31))) {
113 const struct pipe_sampler_view
*view
=
114 ctx
->sampler_views
[shader_type
][i
];
115 lp_sampler_static_texture_state(
116 &key
.sampler
[i
].texture_state
, view
);
118 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
119 const struct util_format_description
*desc
=
120 util_format_description(view
->format
);
121 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
122 !util_format_has_depth(desc
))
123 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
128 key
.nr_sampler_views
= key
.nr_samplers
;
129 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
130 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
131 const struct pipe_sampler_view
*view
=
132 ctx
->sampler_views
[shader_type
][i
];
133 lp_sampler_static_texture_state(
134 &key
.sampler
[i
].texture_state
, view
);
136 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
137 const struct util_format_description
*desc
=
138 util_format_description(view
->format
);
139 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
140 !util_format_has_depth(desc
))
141 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
149 swr_generate_fs_key(struct swr_jit_fs_key
&key
,
150 struct swr_context
*ctx
,
151 swr_fragment_shader
*swr_fs
)
153 memset(&key
, 0, sizeof(key
));
155 key
.nr_cbufs
= ctx
->framebuffer
.nr_cbufs
;
156 key
.light_twoside
= ctx
->rasterizer
->light_twoside
;
157 key
.sprite_coord_enable
= ctx
->rasterizer
->sprite_coord_enable
;
159 struct tgsi_shader_info
*pPrevShader
;
161 pPrevShader
= &ctx
->gs
->info
.base
;
163 pPrevShader
= &ctx
->vs
->info
.base
;
165 memcpy(&key
.vs_output_semantic_name
,
166 &pPrevShader
->output_semantic_name
,
167 sizeof(key
.vs_output_semantic_name
));
168 memcpy(&key
.vs_output_semantic_idx
,
169 &pPrevShader
->output_semantic_index
,
170 sizeof(key
.vs_output_semantic_idx
));
172 swr_generate_sampler_key(swr_fs
->info
, ctx
, PIPE_SHADER_FRAGMENT
, key
);
174 key
.poly_stipple_enable
= ctx
->rasterizer
->poly_stipple_enable
&&
175 ctx
->poly_stipple
.prim_is_poly
;
179 swr_generate_vs_key(struct swr_jit_vs_key
&key
,
180 struct swr_context
*ctx
,
181 swr_vertex_shader
*swr_vs
)
183 memset(&key
, 0, sizeof(key
));
185 key
.clip_plane_mask
=
186 swr_vs
->info
.base
.clipdist_writemask
?
187 swr_vs
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
188 ctx
->rasterizer
->clip_plane_enable
;
190 swr_generate_sampler_key(swr_vs
->info
, ctx
, PIPE_SHADER_VERTEX
, key
);
194 swr_generate_fetch_key(struct swr_jit_fetch_key
&key
,
195 struct swr_vertex_element_state
*velems
)
197 memset(&key
, 0, sizeof(key
));
199 key
.fsState
= velems
->fsState
;
203 swr_generate_gs_key(struct swr_jit_gs_key
&key
,
204 struct swr_context
*ctx
,
205 swr_geometry_shader
*swr_gs
)
207 memset(&key
, 0, sizeof(key
));
209 struct tgsi_shader_info
*pPrevShader
= &ctx
->vs
->info
.base
;
211 memcpy(&key
.vs_output_semantic_name
,
212 &pPrevShader
->output_semantic_name
,
213 sizeof(key
.vs_output_semantic_name
));
214 memcpy(&key
.vs_output_semantic_idx
,
215 &pPrevShader
->output_semantic_index
,
216 sizeof(key
.vs_output_semantic_idx
));
218 swr_generate_sampler_key(swr_gs
->info
, ctx
, PIPE_SHADER_GEOMETRY
, key
);
221 struct BuilderSWR
: public Builder
{
222 BuilderSWR(JitManager
*pJitMgr
, const char *pName
)
225 pJitMgr
->SetupNewModule();
226 gallivm
= gallivm_create(pName
, wrap(&JM()->mContext
));
227 pJitMgr
->mpCurrentModule
= unwrap(gallivm
->module
);
231 gallivm_free_ir(gallivm
);
234 void WriteVS(Value
*pVal
, Value
*pVsContext
, Value
*pVtxOutput
,
235 unsigned slot
, unsigned channel
);
237 struct gallivm_state
*gallivm
;
238 PFN_VERTEX_FUNC
CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
);
239 PFN_PIXEL_KERNEL
CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
);
240 PFN_GS_FUNC
CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
);
243 swr_gs_llvm_fetch_input(const struct lp_build_gs_iface
*gs_iface
,
244 struct lp_build_context
* bld
,
245 boolean is_vindex_indirect
,
246 LLVMValueRef vertex_index
,
247 boolean is_aindex_indirect
,
248 LLVMValueRef attrib_index
,
249 LLVMValueRef swizzle_index
);
251 swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface
*gs_base
,
252 struct lp_build_context
* bld
,
253 LLVMValueRef (*outputs
)[4],
254 LLVMValueRef emitted_vertices_vec
,
255 LLVMValueRef stream_id
);
258 swr_gs_llvm_end_primitive(const struct lp_build_gs_iface
*gs_base
,
259 struct lp_build_context
* bld
,
260 LLVMValueRef total_emitted_vertices_vec_ptr
,
261 LLVMValueRef verts_per_prim_vec
,
262 LLVMValueRef emitted_prims_vec
,
263 LLVMValueRef mask_vec
);
266 swr_gs_llvm_epilogue(const struct lp_build_gs_iface
*gs_base
,
267 LLVMValueRef total_emitted_vertices_vec
,
268 LLVMValueRef emitted_prims_vec
);
272 struct swr_gs_llvm_iface
{
273 struct lp_build_gs_iface base
;
274 struct tgsi_shader_info
*info
;
276 BuilderSWR
*pBuilder
;
279 SWR_GS_STATE
*pGsState
;
280 uint32_t num_outputs
;
281 uint32_t num_verts_per_prim
;
283 Value
*pVtxAttribMap
;
286 // trampoline functions so we can use the builder llvm construction methods
288 swr_gs_llvm_fetch_input(const struct lp_build_gs_iface
*gs_iface
,
289 struct lp_build_context
* bld
,
290 boolean is_vindex_indirect
,
291 LLVMValueRef vertex_index
,
292 boolean is_aindex_indirect
,
293 LLVMValueRef attrib_index
,
294 LLVMValueRef swizzle_index
)
296 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_iface
;
298 return iface
->pBuilder
->swr_gs_llvm_fetch_input(gs_iface
, bld
,
307 swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface
*gs_base
,
308 struct lp_build_context
* bld
,
309 LLVMValueRef (*outputs
)[4],
310 LLVMValueRef emitted_vertices_vec
,
311 LLVMValueRef stream_id
)
313 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
315 iface
->pBuilder
->swr_gs_llvm_emit_vertex(gs_base
, bld
,
317 emitted_vertices_vec
,
322 swr_gs_llvm_end_primitive(const struct lp_build_gs_iface
*gs_base
,
323 struct lp_build_context
* bld
,
324 LLVMValueRef total_emitted_vertices_vec_ptr
,
325 LLVMValueRef verts_per_prim_vec
,
326 LLVMValueRef emitted_prims_vec
,
327 LLVMValueRef mask_vec
)
329 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
331 iface
->pBuilder
->swr_gs_llvm_end_primitive(gs_base
, bld
,
332 total_emitted_vertices_vec_ptr
,
339 swr_gs_llvm_epilogue(const struct lp_build_gs_iface
*gs_base
,
340 LLVMValueRef total_emitted_vertices_vec
,
341 LLVMValueRef emitted_prims_vec
)
343 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
345 iface
->pBuilder
->swr_gs_llvm_epilogue(gs_base
,
346 total_emitted_vertices_vec
,
351 BuilderSWR::swr_gs_llvm_fetch_input(const struct lp_build_gs_iface
*gs_iface
,
352 struct lp_build_context
* bld
,
353 boolean is_vindex_indirect
,
354 LLVMValueRef vertex_index
,
355 boolean is_aindex_indirect
,
356 LLVMValueRef attrib_index
,
357 LLVMValueRef swizzle_index
)
359 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_iface
;
360 Value
*vert_index
= unwrap(vertex_index
);
361 Value
*attr_index
= unwrap(attrib_index
);
363 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
365 if (is_vindex_indirect
|| is_aindex_indirect
) {
367 Value
*res
= unwrap(bld
->zero
);
368 struct lp_type type
= bld
->type
;
370 for (i
= 0; i
< type
.length
; i
++) {
371 Value
*vert_chan_index
= vert_index
;
372 Value
*attr_chan_index
= attr_index
;
374 if (is_vindex_indirect
) {
375 vert_chan_index
= VEXTRACT(vert_index
, C(i
));
377 if (is_aindex_indirect
) {
378 attr_chan_index
= VEXTRACT(attr_index
, C(i
));
382 LOAD(GEP(iface
->pVtxAttribMap
, {C(0), attr_chan_index
}));
384 Value
*pVertex
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pVerts
});
385 Value
*pInputVertStride
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_inputVertStride
});
387 Value
*pVector
= ADD(MUL(vert_chan_index
, pInputVertStride
), attrib
);
388 Value
*pInput
= LOAD(GEP(pVertex
, {pVector
, unwrap(swizzle_index
)}));
390 Value
*value
= VEXTRACT(pInput
, C(i
));
391 res
= VINSERT(res
, value
, C(i
));
396 Value
*attrib
= LOAD(GEP(iface
->pVtxAttribMap
, {C(0), attr_index
}));
398 Value
*pVertex
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pVerts
});
399 Value
*pInputVertStride
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_inputVertStride
});
401 Value
*pVector
= ADD(MUL(vert_index
, pInputVertStride
), attrib
);
403 Value
*pInput
= LOAD(GEP(pVertex
, {pVector
, unwrap(swizzle_index
)}));
409 // GS output stream layout
410 #define VERTEX_COUNT_SIZE 32
411 #define CONTROL_HEADER_SIZE (8*32)
414 BuilderSWR::swr_gs_llvm_emit_vertex(const struct lp_build_gs_iface
*gs_base
,
415 struct lp_build_context
* bld
,
416 LLVMValueRef (*outputs
)[4],
417 LLVMValueRef emitted_vertices_vec
,
418 LLVMValueRef stream_id
)
420 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
422 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
423 const uint32_t headerSize
= VERTEX_COUNT_SIZE
+ CONTROL_HEADER_SIZE
;
424 const uint32_t attribSize
= 4 * sizeof(float);
425 const uint32_t vertSize
= attribSize
* SWR_VTX_NUM_SLOTS
;
426 Value
*pVertexOffset
= MUL(unwrap(emitted_vertices_vec
), VIMMED1(vertSize
));
428 Value
*vMask
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_mask
});
429 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, mVWidth
));
431 Value
*pStack
= STACKSAVE();
432 Value
*pTmpPtr
= ALLOCA(mFP32Ty
, C(4)); // used for dummy write for lane masking
434 for (uint32_t attrib
= 0; attrib
< iface
->num_outputs
; ++attrib
) {
435 uint32_t attribSlot
= attrib
;
436 uint32_t sgvChannel
= 0;
437 if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
438 attribSlot
= VERTEX_SGV_SLOT
;
439 sgvChannel
= VERTEX_SGV_POINT_SIZE_COMP
;
440 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_LAYER
) {
441 attribSlot
= VERTEX_SGV_SLOT
;
442 sgvChannel
= VERTEX_SGV_RTAI_COMP
;
443 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_VIEWPORT_INDEX
) {
444 attribSlot
= VERTEX_SGV_SLOT
;
445 sgvChannel
= VERTEX_SGV_VAI_COMP
;
446 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
447 attribSlot
= VERTEX_POSITION_SLOT
;
449 attribSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
;
450 if (iface
->info
->writes_position
) {
455 Value
*pOutputOffset
= ADD(pVertexOffset
, VIMMED1(headerSize
+ attribSize
* attribSlot
)); // + sgvChannel ?
457 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
458 Value
*pLaneOffset
= VEXTRACT(pOutputOffset
, C(lane
));
459 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
460 Value
*pStreamOffset
= GEP(pStream
, pLaneOffset
);
461 pStreamOffset
= BITCAST(pStreamOffset
, mFP32PtrTy
);
463 Value
*pLaneMask
= VEXTRACT(vMask1
, C(lane
));
464 pStreamOffset
= SELECT(pLaneMask
, pStreamOffset
, pTmpPtr
);
466 for (uint32_t channel
= 0; channel
< 4; ++channel
) {
469 if (attribSlot
== VERTEX_SGV_SLOT
)
470 vData
= LOAD(unwrap(outputs
[attrib
][0]));
472 vData
= LOAD(unwrap(outputs
[attrib
][channel
]));
474 if (attribSlot
!= VERTEX_SGV_SLOT
||
475 sgvChannel
== channel
) {
476 vData
= VEXTRACT(vData
, C(lane
));
477 STORE(vData
, pStreamOffset
);
479 pStreamOffset
= GEP(pStreamOffset
, C(1));
484 /* When the output type is not points, the geometry shader may not
485 * output data to multiple streams. So early exit here.
487 if(iface
->pGsState
->outputTopology
!= TOP_POINT_LIST
) {
488 STACKRESTORE(pStack
);
492 // Info about stream id for each vertex
493 // is coded in 2 bits (4 vert per byte "box"):
494 // ----------------- ----------------- ----
495 // |d|d|c|c|b|b|a|a| |h|h|g|g|f|f|e|e| |...
496 // ----------------- ----------------- ----
498 // Calculate where need to put stream id for current vert
500 Value
*pShiftControl
= MUL(unwrap(emitted_vertices_vec
), VIMMED1(2));
502 // Calculate in which box put stream id for current vert.
503 Value
*pOffsetControl
= LSHR(unwrap(emitted_vertices_vec
), VIMMED1(2));
506 Value
*pStreamIdOffset
= ADD(pOffsetControl
, VIMMED1(VERTEX_COUNT_SIZE
));
508 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
509 Value
*pShift
= TRUNC(VEXTRACT(pShiftControl
, C(lane
)), mInt8Ty
);
510 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
512 Value
*pStreamOffset
= GEP(pStream
, VEXTRACT(pStreamIdOffset
, C(lane
)));
514 // Just make sure that not overflow max - stream id = (0,1,2,3)
515 Value
*vVal
= TRUNC(AND(VEXTRACT(unwrap(stream_id
), C(0)), C(0x3)), mInt8Ty
);
517 // Shift it to correct position in byte "box"
518 vVal
= SHL(vVal
, pShift
);
520 // Info about other vertices can be already stored
521 // so we need to read and add bits from current vert info.
522 Value
*storedValue
= LOAD(pStreamOffset
);
523 vVal
= OR(storedValue
, vVal
);
524 STORE(vVal
, pStreamOffset
);
527 STACKRESTORE(pStack
);
531 BuilderSWR::swr_gs_llvm_end_primitive(const struct lp_build_gs_iface
*gs_base
,
532 struct lp_build_context
* bld
,
533 LLVMValueRef total_emitted_vertices_vec
,
534 LLVMValueRef verts_per_prim_vec
,
535 LLVMValueRef emitted_prims_vec
,
536 LLVMValueRef mask_vec
)
538 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
540 /* When the output type is points, the geometry shader may output data
541 * to multiple streams, and end_primitive has no effect. Info about
542 * stream id for vertices is stored into the same place in memory where
543 * end primitive info is stored so early exit in this case.
545 if (iface
->pGsState
->outputTopology
== TOP_POINT_LIST
) {
549 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
551 Value
*vMask
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_mask
});
552 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, 8));
554 uint32_t vertsPerPrim
= iface
->num_verts_per_prim
;
557 ADD(MUL(unwrap(emitted_prims_vec
), VIMMED1(vertsPerPrim
)),
558 unwrap(verts_per_prim_vec
));
560 vCount
= unwrap(total_emitted_vertices_vec
);
562 Value
*mask
= unwrap(mask_vec
);
563 Value
*cmpMask
= VMASK(ICMP_NE(unwrap(verts_per_prim_vec
), VIMMED1(0)));
564 mask
= AND(mask
, cmpMask
);
565 vMask1
= TRUNC(mask
, VectorType::get(mInt1Ty
, 8));
567 vCount
= SUB(vCount
, VIMMED1(1));
568 Value
*vOffset
= ADD(UDIV(vCount
, VIMMED1(8)), VIMMED1(VERTEX_COUNT_SIZE
));
569 Value
*vValue
= SHL(VIMMED1(1), UREM(vCount
, VIMMED1(8)));
571 vValue
= TRUNC(vValue
, VectorType::get(mInt8Ty
, 8));
573 Value
*pStack
= STACKSAVE();
574 Value
*pTmpPtr
= ALLOCA(mInt8Ty
, C(4)); // used for dummy read/write for lane masking
576 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
577 Value
*vLaneOffset
= VEXTRACT(vOffset
, C(lane
));
578 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
579 Value
*pStreamOffset
= GEP(pStream
, vLaneOffset
);
581 Value
*pLaneMask
= VEXTRACT(vMask1
, C(lane
));
582 pStreamOffset
= SELECT(pLaneMask
, pStreamOffset
, pTmpPtr
);
584 Value
*vVal
= LOAD(pStreamOffset
);
585 vVal
= OR(vVal
, VEXTRACT(vValue
, C(lane
)));
586 STORE(vVal
, pStreamOffset
);
589 STACKRESTORE(pStack
);
593 BuilderSWR::swr_gs_llvm_epilogue(const struct lp_build_gs_iface
*gs_base
,
594 LLVMValueRef total_emitted_vertices_vec
,
595 LLVMValueRef emitted_prims_vec
)
597 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
599 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
601 // Store emit count to each output stream in the first DWORD
602 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
604 Value
* pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
605 pStream
= BITCAST(pStream
, mInt32PtrTy
);
606 Value
* pLaneCount
= VEXTRACT(unwrap(total_emitted_vertices_vec
), C(lane
));
607 STORE(pLaneCount
, pStream
);
612 BuilderSWR::CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
614 SWR_GS_STATE
*pGS
= &ctx
->gs
->gsState
;
615 struct tgsi_shader_info
*info
= &ctx
->gs
->info
.base
;
617 memset(pGS
, 0, sizeof(*pGS
));
619 pGS
->gsEnable
= true;
621 pGS
->numInputAttribs
= (VERTEX_ATTRIB_START_SLOT
- VERTEX_POSITION_SLOT
) + info
->num_inputs
;
622 pGS
->outputTopology
=
623 swr_convert_prim_topology(info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
624 pGS
->maxNumVerts
= info
->properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
625 pGS
->instanceCount
= info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
];
627 // If point primitive then assume to use multiple streams
628 if(pGS
->outputTopology
== TOP_POINT_LIST
) {
629 pGS
->isSingleStream
= false;
631 pGS
->isSingleStream
= true;
632 pGS
->singleStreamID
= 0;
635 pGS
->vertexAttribOffset
= VERTEX_POSITION_SLOT
;
636 pGS
->inputVertStride
= pGS
->numInputAttribs
+ pGS
->vertexAttribOffset
;
637 pGS
->outputVertexSize
= SWR_VTX_NUM_SLOTS
;
638 pGS
->controlDataSize
= 8; // GS ouputs max of 8 32B units
639 pGS
->controlDataOffset
= VERTEX_COUNT_SIZE
;
640 pGS
->outputVertexOffset
= pGS
->controlDataOffset
+ CONTROL_HEADER_SIZE
;
642 pGS
->allocationSize
=
643 VERTEX_COUNT_SIZE
+ // vertex count
644 CONTROL_HEADER_SIZE
+ // control header
645 (SWR_VTX_NUM_SLOTS
* 16) * // sizeof vertex
646 pGS
->maxNumVerts
; // num verts
648 struct swr_geometry_shader
*gs
= ctx
->gs
;
650 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
651 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
653 memset(outputs
, 0, sizeof(outputs
));
655 AttrBuilder attrBuilder
;
656 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
658 std::vector
<Type
*> gsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
659 PointerType::get(mInt8Ty
, 0),
660 PointerType::get(Gen_SWR_GS_CONTEXT(JM()), 0)};
661 FunctionType
*vsFuncType
=
662 FunctionType::get(Type::getVoidTy(JM()->mContext
), gsArgs
, false);
664 // create new vertex shader function
665 auto pFunction
= Function::Create(vsFuncType
,
666 GlobalValue::ExternalLinkage
,
668 JM()->mpCurrentModule
);
669 #if LLVM_VERSION_MAJOR < 5
670 AttributeSet attrSet
= AttributeSet::get(
671 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
672 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
674 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
677 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
678 IRB()->SetInsertPoint(block
);
679 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
681 auto argitr
= pFunction
->arg_begin();
682 Value
*hPrivateData
= &*argitr
++;
683 hPrivateData
->setName("hPrivateData");
684 Value
*pWorkerData
= &*argitr
++;
685 pWorkerData
->setName("pWorkerData");
686 Value
*pGsCtx
= &*argitr
++;
687 pGsCtx
->setName("gsCtx");
690 GEP(hPrivateData
, {C(0), C(swr_draw_context_constantGS
)});
691 consts_ptr
->setName("gs_constants");
692 Value
*const_sizes_ptr
=
693 GEP(hPrivateData
, {0, swr_draw_context_num_constantsGS
});
694 const_sizes_ptr
->setName("num_gs_constants");
696 struct lp_build_sampler_soa
*sampler
=
697 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_GEOMETRY
);
699 struct lp_bld_tgsi_system_values system_values
;
700 memset(&system_values
, 0, sizeof(system_values
));
701 system_values
.prim_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_PrimitiveID
}));
702 system_values
.instance_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_InstanceID
}));
704 std::vector
<Constant
*> mapConstants
;
705 Value
*vtxAttribMap
= ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
706 for (unsigned slot
= 0; slot
< info
->num_inputs
; slot
++) {
707 ubyte semantic_name
= info
->input_semantic_name
[slot
];
708 ubyte semantic_idx
= info
->input_semantic_index
[slot
];
710 unsigned vs_slot
= locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
);
712 vs_slot
+= VERTEX_ATTRIB_START_SLOT
;
714 if (ctx
->vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
717 if (semantic_name
== TGSI_SEMANTIC_POSITION
)
718 vs_slot
= VERTEX_POSITION_SLOT
;
720 STORE(C(vs_slot
), vtxAttribMap
, {0, slot
});
721 mapConstants
.push_back(C(vs_slot
));
724 struct lp_build_mask_context mask
;
725 Value
*mask_val
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_mask
}, "gsMask");
726 lp_build_mask_begin(&mask
, gallivm
,
727 lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
729 // zero out cut buffer so we can load/modify/store bits
730 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
732 Value
* pStream
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
733 MEMSET(pStream
, C((char)0), VERTEX_COUNT_SIZE
+ CONTROL_HEADER_SIZE
, sizeof(float) * KNOB_SIMD_WIDTH
);
736 struct swr_gs_llvm_iface gs_iface
;
737 gs_iface
.base
.fetch_input
= ::swr_gs_llvm_fetch_input
;
738 gs_iface
.base
.emit_vertex
= ::swr_gs_llvm_emit_vertex
;
739 gs_iface
.base
.end_primitive
= ::swr_gs_llvm_end_primitive
;
740 gs_iface
.base
.gs_epilogue
= ::swr_gs_llvm_epilogue
;
741 gs_iface
.pBuilder
= this;
742 gs_iface
.pGsCtx
= pGsCtx
;
743 gs_iface
.pGsState
= pGS
;
744 gs_iface
.num_outputs
= gs
->info
.base
.num_outputs
;
745 gs_iface
.num_verts_per_prim
=
746 u_vertices_per_prim((pipe_prim_type
)info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
747 gs_iface
.info
= info
;
748 gs_iface
.pVtxAttribMap
= vtxAttribMap
;
750 struct lp_build_tgsi_params params
;
751 memset(¶ms
, 0, sizeof(params
));
752 params
.type
= lp_type_float_vec(32, 32 * 8);
753 params
.mask
= & mask
;
754 params
.consts_ptr
= wrap(consts_ptr
);
755 params
.const_sizes_ptr
= wrap(const_sizes_ptr
);
756 params
.system_values
= &system_values
;
757 params
.inputs
= inputs
;
758 params
.context_ptr
= wrap(hPrivateData
);
759 params
.sampler
= sampler
;
760 params
.info
= &gs
->info
.base
;
761 params
.gs_iface
= &gs_iface
.base
;
763 lp_build_tgsi_soa(gallivm
,
768 lp_build_mask_end(&mask
);
770 sampler
->destroy(sampler
);
772 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
776 gallivm_verify_function(gallivm
, wrap(pFunction
));
777 gallivm_compile_module(gallivm
);
780 (PFN_GS_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
782 debug_printf("geom shader %p\n", pFunc
);
783 assert(pFunc
&& "Error: GeomShader = NULL");
785 JM()->mIsModuleFinalized
= true;
791 swr_compile_gs(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
794 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
796 PFN_GS_FUNC func
= builder
.CompileGS(ctx
, key
);
798 ctx
->gs
->map
.insert(std::make_pair(key
, std::make_unique
<VariantGS
>(builder
.gallivm
, func
)));
803 BuilderSWR::WriteVS(Value
*pVal
, Value
*pVsContext
, Value
*pVtxOutput
, unsigned slot
, unsigned channel
)
805 #if USE_SIMD16_FRONTEND && !USE_SIMD16_VS
806 // interleave the simdvertex components into the dest simd16vertex
807 // slot16offset = slot8offset * 2
808 // comp16offset = comp8offset * 2 + alternateOffset
810 Value
*offset
= LOAD(pVsContext
, { 0, SWR_VS_CONTEXT_AlternateOffset
});
811 Value
*pOut
= GEP(pVtxOutput
, { C(0), C(0), C(slot
* 2), offset
} );
812 STORE(pVal
, pOut
, {channel
* 2});
814 Value
*pOut
= GEP(pVtxOutput
, {0, 0, slot
});
815 STORE(pVal
, pOut
, {0, channel
});
820 BuilderSWR::CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
822 struct swr_vertex_shader
*swr_vs
= ctx
->vs
;
824 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
825 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
827 memset(outputs
, 0, sizeof(outputs
));
829 AttrBuilder attrBuilder
;
830 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
832 std::vector
<Type
*> vsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
833 PointerType::get(mInt8Ty
, 0),
834 PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};
835 FunctionType
*vsFuncType
=
836 FunctionType::get(Type::getVoidTy(JM()->mContext
), vsArgs
, false);
838 // create new vertex shader function
839 auto pFunction
= Function::Create(vsFuncType
,
840 GlobalValue::ExternalLinkage
,
842 JM()->mpCurrentModule
);
843 #if LLVM_VERSION_MAJOR < 5
844 AttributeSet attrSet
= AttributeSet::get(
845 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
846 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
848 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
851 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
852 IRB()->SetInsertPoint(block
);
853 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
855 auto argitr
= pFunction
->arg_begin();
856 Value
*hPrivateData
= &*argitr
++;
857 hPrivateData
->setName("hPrivateData");
858 Value
*pWorkerData
= &*argitr
++;
859 pWorkerData
->setName("pWorkerData");
860 Value
*pVsCtx
= &*argitr
++;
861 pVsCtx
->setName("vsCtx");
863 Value
*consts_ptr
= GEP(hPrivateData
, {C(0), C(swr_draw_context_constantVS
)});
865 consts_ptr
->setName("vs_constants");
866 Value
*const_sizes_ptr
=
867 GEP(hPrivateData
, {0, swr_draw_context_num_constantsVS
});
868 const_sizes_ptr
->setName("num_vs_constants");
870 Value
*vtxInput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVin
});
872 vtxInput
= BITCAST(vtxInput
, PointerType::get(Gen_simd16vertex(JM()), 0));
875 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
876 const unsigned mask
= swr_vs
->info
.base
.input_usage_mask
[attrib
];
877 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
878 if (mask
& (1 << channel
)) {
879 inputs
[attrib
][channel
] =
880 wrap(LOAD(vtxInput
, {0, 0, attrib
, channel
}));
885 struct lp_build_sampler_soa
*sampler
=
886 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_VERTEX
);
888 struct lp_bld_tgsi_system_values system_values
;
889 memset(&system_values
, 0, sizeof(system_values
));
890 system_values
.instance_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_InstanceID
}));
893 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID16
}));
895 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID
}));
899 uint32_t vectorWidth
= mVWidth16
;
901 uint32_t vectorWidth
= mVWidth
;
904 struct lp_build_tgsi_params params
;
905 memset(¶ms
, 0, sizeof(params
));
906 params
.type
= lp_type_float_vec(32, 32 * vectorWidth
);
907 params
.consts_ptr
= wrap(consts_ptr
);
908 params
.const_sizes_ptr
= wrap(const_sizes_ptr
);
909 params
.system_values
= &system_values
;
910 params
.inputs
= inputs
;
911 params
.context_ptr
= wrap(hPrivateData
);
912 params
.sampler
= sampler
;
913 params
.info
= &swr_vs
->info
.base
;
915 lp_build_tgsi_soa(gallivm
,
920 sampler
->destroy(sampler
);
922 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
924 Value
*vtxOutput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVout
});
926 vtxOutput
= BITCAST(vtxOutput
, PointerType::get(Gen_simd16vertex(JM()), 0));
929 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
930 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
931 if (!outputs
[attrib
][channel
])
937 if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
938 if (channel
!= VERTEX_SGV_POINT_SIZE_COMP
)
940 val
= LOAD(unwrap(outputs
[attrib
][0]));
941 outSlot
= VERTEX_SGV_SLOT
;
942 } else if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
943 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
944 outSlot
= VERTEX_POSITION_SLOT
;
946 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
947 outSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
;
948 if (swr_vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
952 WriteVS(val
, pVsCtx
, vtxOutput
, outSlot
, channel
);
956 if (ctx
->rasterizer
->clip_plane_enable
||
957 swr_vs
->info
.base
.culldist_writemask
) {
958 unsigned clip_mask
= ctx
->rasterizer
->clip_plane_enable
;
961 if (swr_vs
->info
.base
.writes_clipvertex
) {
962 cv
= locate_linkage(TGSI_SEMANTIC_CLIPVERTEX
, 0,
965 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
966 if (swr_vs
->info
.base
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
967 swr_vs
->info
.base
.output_semantic_index
[i
] == 0) {
973 LLVMValueRef cx
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][0], "");
974 LLVMValueRef cy
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][1], "");
975 LLVMValueRef cz
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][2], "");
976 LLVMValueRef cw
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][3], "");
978 for (unsigned val
= 0; val
< PIPE_MAX_CLIP_PLANES
; val
++) {
979 // clip distance overrides user clip planes
980 if ((swr_vs
->info
.base
.clipdist_writemask
& clip_mask
& (1 << val
)) ||
981 ((swr_vs
->info
.base
.culldist_writemask
<< swr_vs
->info
.base
.num_written_clipdistance
) & (1 << val
))) {
982 unsigned cv
= locate_linkage(TGSI_SEMANTIC_CLIPDIST
, val
< 4 ? 0 : 1,
985 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
], "");
986 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
988 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
- 4], "");
989 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
994 if (!(clip_mask
& (1 << val
)))
997 Value
*px
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 0}));
998 Value
*py
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 1}));
999 Value
*pz
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 2}));
1000 Value
*pw
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 3}));
1002 Value
*bpx
= VBROADCAST_16(px
);
1003 Value
*bpy
= VBROADCAST_16(py
);
1004 Value
*bpz
= VBROADCAST_16(pz
);
1005 Value
*bpw
= VBROADCAST_16(pw
);
1007 Value
*bpx
= VBROADCAST(px
);
1008 Value
*bpy
= VBROADCAST(py
);
1009 Value
*bpz
= VBROADCAST(pz
);
1010 Value
*bpw
= VBROADCAST(pw
);
1012 Value
*dist
= FADD(FMUL(unwrap(cx
), bpx
),
1013 FADD(FMUL(unwrap(cy
), bpy
),
1014 FADD(FMUL(unwrap(cz
), bpz
),
1015 FMUL(unwrap(cw
), bpw
))));
1018 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
1020 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
1026 gallivm_verify_function(gallivm
, wrap(pFunction
));
1027 gallivm_compile_module(gallivm
);
1029 // lp_debug_dump_value(func);
1031 PFN_VERTEX_FUNC pFunc
=
1032 (PFN_VERTEX_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
1034 debug_printf("vert shader %p\n", pFunc
);
1035 assert(pFunc
&& "Error: VertShader = NULL");
1037 JM()->mIsModuleFinalized
= true;
1043 swr_compile_vs(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
1045 if (!ctx
->vs
->pipe
.tokens
)
1049 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
1051 PFN_VERTEX_FUNC func
= builder
.CompileVS(ctx
, key
);
1053 ctx
->vs
->map
.insert(std::make_pair(key
, std::make_unique
<VariantVS
>(builder
.gallivm
, func
)));
1058 swr_so_adjust_attrib(unsigned in_attrib
,
1059 swr_vertex_shader
*swr_vs
)
1061 ubyte semantic_name
;
1064 attrib
= in_attrib
+ VERTEX_ATTRIB_START_SLOT
;
1067 semantic_name
= swr_vs
->info
.base
.output_semantic_name
[in_attrib
];
1068 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
1069 attrib
= VERTEX_POSITION_SLOT
;
1070 } else if (semantic_name
== TGSI_SEMANTIC_PSIZE
) {
1071 attrib
= VERTEX_SGV_SLOT
;
1072 } else if (semantic_name
== TGSI_SEMANTIC_LAYER
) {
1073 attrib
= VERTEX_SGV_SLOT
;
1075 if (swr_vs
->info
.base
.writes_position
) {
1085 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
)
1087 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1088 if ((info
->output_semantic_name
[i
] == name
)
1089 && (info
->output_semantic_index
[i
] == index
)) {
1098 BuilderSWR::CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
1100 struct swr_fragment_shader
*swr_fs
= ctx
->fs
;
1102 struct tgsi_shader_info
*pPrevShader
;
1104 pPrevShader
= &ctx
->gs
->info
.base
;
1106 pPrevShader
= &ctx
->vs
->info
.base
;
1108 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1109 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1111 memset(inputs
, 0, sizeof(inputs
));
1112 memset(outputs
, 0, sizeof(outputs
));
1114 struct lp_build_sampler_soa
*sampler
= NULL
;
1116 AttrBuilder attrBuilder
;
1117 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
1119 std::vector
<Type
*> fsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
1120 PointerType::get(mInt8Ty
, 0),
1121 PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};
1122 FunctionType
*funcType
=
1123 FunctionType::get(Type::getVoidTy(JM()->mContext
), fsArgs
, false);
1125 auto pFunction
= Function::Create(funcType
,
1126 GlobalValue::ExternalLinkage
,
1128 JM()->mpCurrentModule
);
1129 #if LLVM_VERSION_MAJOR < 5
1130 AttributeSet attrSet
= AttributeSet::get(
1131 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
1132 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
1134 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
1137 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
1138 IRB()->SetInsertPoint(block
);
1139 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
1141 auto args
= pFunction
->arg_begin();
1142 Value
*hPrivateData
= &*args
++;
1143 hPrivateData
->setName("hPrivateData");
1144 Value
*pWorkerData
= &*args
++;
1145 pWorkerData
->setName("pWorkerData");
1146 Value
*pPS
= &*args
++;
1147 pPS
->setName("psCtx");
1149 Value
*consts_ptr
= GEP(hPrivateData
, {0, swr_draw_context_constantFS
});
1150 consts_ptr
->setName("fs_constants");
1151 Value
*const_sizes_ptr
=
1152 GEP(hPrivateData
, {0, swr_draw_context_num_constantsFS
});
1153 const_sizes_ptr
->setName("num_fs_constants");
1155 // load *pAttribs, *pPerspAttribs
1156 Value
*pRawAttribs
= LOAD(pPS
, {0, SWR_PS_CONTEXT_pAttribs
}, "pRawAttribs");
1157 Value
*pPerspAttribs
=
1158 LOAD(pPS
, {0, SWR_PS_CONTEXT_pPerspAttribs
}, "pPerspAttribs");
1160 swr_fs
->constantMask
= 0;
1161 swr_fs
->flatConstantMask
= 0;
1162 swr_fs
->pointSpriteMask
= 0;
1164 for (int attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
1165 const unsigned mask
= swr_fs
->info
.base
.input_usage_mask
[attrib
];
1166 const unsigned interpMode
= swr_fs
->info
.base
.input_interpolate
[attrib
];
1167 const unsigned interpLoc
= swr_fs
->info
.base
.input_interpolate_loc
[attrib
];
1173 Value
*vi
= nullptr, *vj
= nullptr;
1174 switch (interpLoc
) {
1175 case TGSI_INTERPOLATE_LOC_CENTER
:
1176 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_center
}, "i");
1177 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_center
}, "j");
1179 case TGSI_INTERPOLATE_LOC_CENTROID
:
1180 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_centroid
}, "i");
1181 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_centroid
}, "j");
1183 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1184 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_sample
}, "i");
1185 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_sample
}, "j");
1190 Value
*vw
= nullptr, *pAttribs
;
1191 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
1192 interpMode
== TGSI_INTERPOLATE_COLOR
) {
1193 pAttribs
= pPerspAttribs
;
1194 switch (interpLoc
) {
1195 case TGSI_INTERPOLATE_LOC_CENTER
:
1196 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}));
1198 case TGSI_INTERPOLATE_LOC_CENTROID
:
1199 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_centroid
}));
1201 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1202 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_sample
}));
1206 pAttribs
= pRawAttribs
;
1212 ubyte semantic_name
= swr_fs
->info
.base
.input_semantic_name
[attrib
];
1213 ubyte semantic_idx
= swr_fs
->info
.base
.input_semantic_index
[attrib
];
1215 if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1217 UI_TO_FP(LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), mFP32Ty
);
1218 ff
= FSUB(FMUL(ff
, C(2.0f
)), C(1.0f
));
1219 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vFrontFace");
1221 inputs
[attrib
][0] = wrap(ff
);
1222 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1223 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1224 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
1226 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
) { // gl_FragCoord
1227 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
1228 TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
) {
1229 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_center
}, "vX"));
1230 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_center
}, "vY"));
1232 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
}, "vX"));
1233 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
}, "vY"));
1235 inputs
[attrib
][2] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vZ
}, "vZ"));
1237 wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}, "vOneOverW"));
1239 } else if (semantic_name
== TGSI_SEMANTIC_LAYER
) { // gl_Layer
1240 Value
*ff
= LOAD(pPS
, {0, SWR_PS_CONTEXT_renderTargetArrayIndex
});
1241 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vRenderTargetArrayIndex");
1242 inputs
[attrib
][0] = wrap(ff
);
1243 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1244 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1245 inputs
[attrib
][3] = wrap(VIMMED1(0.0f
));
1247 } else if (semantic_name
== TGSI_SEMANTIC_VIEWPORT_INDEX
) { // gl_ViewportIndex
1248 Value
*ff
= LOAD(pPS
, {0, SWR_PS_CONTEXT_viewportIndex
});
1249 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vViewportIndex");
1250 inputs
[attrib
][0] = wrap(ff
);
1251 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1252 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1253 inputs
[attrib
][3] = wrap(VIMMED1(0.0f
));
1256 unsigned linkedAttrib
=
1257 locate_linkage(semantic_name
, semantic_idx
, pPrevShader
) - 1;
1259 uint32_t extraAttribs
= 0;
1260 if (semantic_name
== TGSI_SEMANTIC_PRIMID
&& !ctx
->gs
) {
1261 /* non-gs generated primID - need to grab from swizzleMap override */
1262 linkedAttrib
= pPrevShader
->num_outputs
- 1;
1263 swr_fs
->constantMask
|= 1 << linkedAttrib
;
1265 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1266 key
.sprite_coord_enable
& (1 << semantic_idx
)) {
1267 /* we add an extra attrib to the backendState in swr_update_derived. */
1268 linkedAttrib
= pPrevShader
->num_outputs
+ extraAttribs
- 1;
1269 swr_fs
->pointSpriteMask
|= (1 << linkedAttrib
);
1271 } else if (linkedAttrib
== 0xFFFFFFFF) {
1272 inputs
[attrib
][0] = wrap(VIMMED1(0.0f
));
1273 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1274 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1275 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
1276 /* If we're reading in color and 2-sided lighting is enabled, we have
1279 if (semantic_name
!= TGSI_SEMANTIC_COLOR
|| !key
.light_twoside
)
1282 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1283 swr_fs
->constantMask
|= 1 << linkedAttrib
;
1284 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1285 swr_fs
->flatConstantMask
|= 1 << linkedAttrib
;
1289 unsigned bcolorAttrib
= 0xFFFFFFFF;
1290 Value
*offset
= NULL
;
1291 if (semantic_name
== TGSI_SEMANTIC_COLOR
&& key
.light_twoside
) {
1292 bcolorAttrib
= locate_linkage(
1293 TGSI_SEMANTIC_BCOLOR
, semantic_idx
, pPrevShader
) - 1;
1294 /* Neither front nor back colors were available. Nothing to load. */
1295 if (bcolorAttrib
== 0xFFFFFFFF && linkedAttrib
== 0xFFFFFFFF)
1297 /* If there is no front color, just always use the back color. */
1298 if (linkedAttrib
== 0xFFFFFFFF)
1299 linkedAttrib
= bcolorAttrib
;
1301 if (bcolorAttrib
!= 0xFFFFFFFF) {
1302 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1303 swr_fs
->constantMask
|= 1 << bcolorAttrib
;
1304 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1305 swr_fs
->flatConstantMask
|= 1 << bcolorAttrib
;
1308 unsigned diff
= 12 * (bcolorAttrib
- linkedAttrib
);
1312 XOR(C(1), LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), "backFace");
1314 offset
= MUL(back
, C(diff
));
1315 offset
->setName("offset");
1320 for (int channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1321 if (mask
& (1 << channel
)) {
1322 Value
*indexA
= C(linkedAttrib
* 12 + channel
);
1323 Value
*indexB
= C(linkedAttrib
* 12 + channel
+ 4);
1324 Value
*indexC
= C(linkedAttrib
* 12 + channel
+ 8);
1327 indexA
= ADD(indexA
, offset
);
1328 indexB
= ADD(indexB
, offset
);
1329 indexC
= ADD(indexC
, offset
);
1332 Value
*va
= VBROADCAST(LOAD(GEP(pAttribs
, indexA
)));
1333 Value
*vb
= VBROADCAST(LOAD(GEP(pAttribs
, indexB
)));
1334 Value
*vc
= VBROADCAST(LOAD(GEP(pAttribs
, indexC
)));
1336 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1337 inputs
[attrib
][channel
] = wrap(va
);
1339 Value
*vk
= FSUB(FSUB(VIMMED1(1.0f
), vi
), vj
);
1343 Value
*interp
= FMUL(va
, vi
);
1344 Value
*interp1
= FMUL(vb
, vj
);
1345 interp
= FADD(interp
, interp1
);
1346 interp
= FADD(interp
, vc
);
1347 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
1348 interpMode
== TGSI_INTERPOLATE_COLOR
)
1349 interp
= FMUL(interp
, vw
);
1350 inputs
[attrib
][channel
] = wrap(interp
);
1356 sampler
= swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_FRAGMENT
);
1358 struct lp_bld_tgsi_system_values system_values
;
1359 memset(&system_values
, 0, sizeof(system_values
));
1361 struct lp_build_mask_context mask
;
1362 bool uses_mask
= false;
1364 if (swr_fs
->info
.base
.uses_kill
||
1365 key
.poly_stipple_enable
) {
1366 Value
*vActiveMask
= NULL
;
1367 if (swr_fs
->info
.base
.uses_kill
) {
1368 vActiveMask
= LOAD(pPS
, {0, SWR_PS_CONTEXT_activeMask
}, "activeMask");
1370 if (key
.poly_stipple_enable
) {
1371 // first get fragment xy coords and clip to stipple bounds
1372 Value
*vXf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
});
1373 Value
*vYf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
});
1374 Value
*vXu
= FP_TO_UI(vXf
, mSimdInt32Ty
);
1375 Value
*vYu
= FP_TO_UI(vYf
, mSimdInt32Ty
);
1377 // stipple pattern is 32x32, which means that one line of stipple
1378 // is stored in one word:
1379 // vXstipple is bit offset inside 32-bit stipple word
1380 // vYstipple is word index is stipple array
1381 Value
*vXstipple
= AND(vXu
, VIMMED1(0x1f)); // & (32-1)
1382 Value
*vYstipple
= AND(vYu
, VIMMED1(0x1f)); // & (32-1)
1384 // grab stipple pattern base address
1385 Value
*stipplePtr
= GEP(hPrivateData
, {0, swr_draw_context_polyStipple
, 0});
1386 stipplePtr
= BITCAST(stipplePtr
, mInt8PtrTy
);
1388 // peform a gather to grab stipple words for each lane
1389 Value
*vStipple
= GATHERDD(VUNDEF_I(), stipplePtr
, vYstipple
,
1390 VIMMED1(0xffffffff), 4);
1392 // create a mask with one bit corresponding to the x stipple
1393 // and AND it with the pattern, to see if we have a bit
1394 Value
*vBitMask
= LSHR(VIMMED1(0x80000000), vXstipple
);
1395 Value
*vStippleMask
= AND(vStipple
, vBitMask
);
1396 vStippleMask
= ICMP_NE(vStippleMask
, VIMMED1(0));
1397 vStippleMask
= VMASK(vStippleMask
);
1399 if (swr_fs
->info
.base
.uses_kill
) {
1400 vActiveMask
= AND(vActiveMask
, vStippleMask
);
1402 vActiveMask
= vStippleMask
;
1405 lp_build_mask_begin(
1406 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(vActiveMask
));
1410 struct lp_build_tgsi_params params
;
1411 memset(¶ms
, 0, sizeof(params
));
1412 params
.type
= lp_type_float_vec(32, 32 * 8);
1413 params
.mask
= uses_mask
? &mask
: NULL
;
1414 params
.consts_ptr
= wrap(consts_ptr
);
1415 params
.const_sizes_ptr
= wrap(const_sizes_ptr
);
1416 params
.system_values
= &system_values
;
1417 params
.inputs
= inputs
;
1418 params
.context_ptr
= wrap(hPrivateData
);
1419 params
.sampler
= sampler
;
1420 params
.info
= &swr_fs
->info
.base
;
1422 lp_build_tgsi_soa(gallivm
,
1423 swr_fs
->pipe
.tokens
,
1427 sampler
->destroy(sampler
);
1429 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1431 for (uint32_t attrib
= 0; attrib
< swr_fs
->info
.base
.num_outputs
;
1433 switch (swr_fs
->info
.base
.output_semantic_name
[attrib
]) {
1434 case TGSI_SEMANTIC_POSITION
: {
1437 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][2], "");
1438 STORE(unwrap(outZ
), pPS
, {0, SWR_PS_CONTEXT_vZ
});
1441 case TGSI_SEMANTIC_COLOR
: {
1442 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1443 if (!outputs
[attrib
][channel
])
1447 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][channel
], "");
1448 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
1449 swr_fs
->info
.base
.output_semantic_index
[attrib
] == 0) {
1450 for (uint32_t rt
= 0; rt
< key
.nr_cbufs
; rt
++) {
1453 {0, SWR_PS_CONTEXT_shaded
, rt
, channel
});
1459 SWR_PS_CONTEXT_shaded
,
1460 swr_fs
->info
.base
.output_semantic_index
[attrib
],
1468 "unknown output from FS %s[%d]\n",
1469 tgsi_semantic_names
[swr_fs
->info
.base
1470 .output_semantic_name
[attrib
]],
1471 swr_fs
->info
.base
.output_semantic_index
[attrib
]);
1477 LLVMValueRef mask_result
= 0;
1479 mask_result
= lp_build_mask_end(&mask
);
1482 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1485 STORE(unwrap(mask_result
), pPS
, {0, SWR_PS_CONTEXT_activeMask
});
1490 gallivm_verify_function(gallivm
, wrap(pFunction
));
1492 gallivm_compile_module(gallivm
);
1494 // after the gallivm passes, we have to lower the core's intrinsics
1495 llvm::legacy::FunctionPassManager
lowerPass(JM()->mpCurrentModule
);
1496 lowerPass
.add(createLowerX86Pass(this));
1497 lowerPass
.run(*pFunction
);
1499 PFN_PIXEL_KERNEL kernel
=
1500 (PFN_PIXEL_KERNEL
)gallivm_jit_function(gallivm
, wrap(pFunction
));
1501 debug_printf("frag shader %p\n", kernel
);
1502 assert(kernel
&& "Error: FragShader = NULL");
1504 JM()->mIsModuleFinalized
= true;
1510 swr_compile_fs(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
1512 if (!ctx
->fs
->pipe
.tokens
)
1516 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
1518 PFN_PIXEL_KERNEL func
= builder
.CompileFS(ctx
, key
);
1520 ctx
->fs
->map
.insert(std::make_pair(key
, std::make_unique
<VariantFS
>(builder
.gallivm
, func
)));