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 #include "llvm/IR/LegacyPassManager.h"
31 #pragma pop_macro("DEBUG")
34 #include "gen_state_llvm.h"
36 #include "functionpasses/passes.h"
38 #include "tgsi/tgsi_strings.h"
39 #include "util/u_format.h"
40 #include "util/u_prim.h"
41 #include "gallivm/lp_bld_init.h"
42 #include "gallivm/lp_bld_flow.h"
43 #include "gallivm/lp_bld_struct.h"
44 #include "gallivm/lp_bld_tgsi.h"
46 #include "swr_context.h"
47 #include "gen_swr_context_llvm.h"
48 #include "swr_resource.h"
49 #include "swr_state.h"
50 #include "swr_screen.h"
52 using namespace SwrJit
;
56 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
);
58 bool operator==(const swr_jit_fs_key
&lhs
, const swr_jit_fs_key
&rhs
)
60 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
63 bool operator==(const swr_jit_vs_key
&lhs
, const swr_jit_vs_key
&rhs
)
65 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
68 bool operator==(const swr_jit_fetch_key
&lhs
, const swr_jit_fetch_key
&rhs
)
70 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
73 bool operator==(const swr_jit_gs_key
&lhs
, const swr_jit_gs_key
&rhs
)
75 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
79 swr_generate_sampler_key(const struct lp_tgsi_info
&info
,
80 struct swr_context
*ctx
,
81 enum pipe_shader_type shader_type
,
82 struct swr_jit_sampler_key
&key
)
84 key
.nr_samplers
= info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
86 for (unsigned i
= 0; i
< key
.nr_samplers
; i
++) {
87 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
88 lp_sampler_static_sampler_state(
89 &key
.sampler
[i
].sampler_state
,
90 ctx
->samplers
[shader_type
][i
]);
95 * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes
96 * are dx10-style? Can't really have mixed opcodes, at least not
97 * if we want to skip the holes here (without rescanning tgsi).
99 if (info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
100 key
.nr_sampler_views
=
101 info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
102 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
103 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER_VIEW
] & (1u << (i
& 31))) {
104 const struct pipe_sampler_view
*view
=
105 ctx
->sampler_views
[shader_type
][i
];
106 lp_sampler_static_texture_state(
107 &key
.sampler
[i
].texture_state
, view
);
109 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
110 const struct util_format_description
*desc
=
111 util_format_description(view
->format
);
112 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
113 !util_format_has_depth(desc
))
114 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
119 key
.nr_sampler_views
= key
.nr_samplers
;
120 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
121 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
122 const struct pipe_sampler_view
*view
=
123 ctx
->sampler_views
[shader_type
][i
];
124 lp_sampler_static_texture_state(
125 &key
.sampler
[i
].texture_state
, view
);
127 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
128 const struct util_format_description
*desc
=
129 util_format_description(view
->format
);
130 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
131 !util_format_has_depth(desc
))
132 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
140 swr_generate_fs_key(struct swr_jit_fs_key
&key
,
141 struct swr_context
*ctx
,
142 swr_fragment_shader
*swr_fs
)
144 memset(&key
, 0, sizeof(key
));
146 key
.nr_cbufs
= ctx
->framebuffer
.nr_cbufs
;
147 key
.light_twoside
= ctx
->rasterizer
->light_twoside
;
148 key
.sprite_coord_enable
= ctx
->rasterizer
->sprite_coord_enable
;
150 struct tgsi_shader_info
*pPrevShader
;
152 pPrevShader
= &ctx
->gs
->info
.base
;
154 pPrevShader
= &ctx
->vs
->info
.base
;
156 memcpy(&key
.vs_output_semantic_name
,
157 &pPrevShader
->output_semantic_name
,
158 sizeof(key
.vs_output_semantic_name
));
159 memcpy(&key
.vs_output_semantic_idx
,
160 &pPrevShader
->output_semantic_index
,
161 sizeof(key
.vs_output_semantic_idx
));
163 swr_generate_sampler_key(swr_fs
->info
, ctx
, PIPE_SHADER_FRAGMENT
, key
);
165 key
.poly_stipple_enable
= ctx
->rasterizer
->poly_stipple_enable
&&
166 ctx
->poly_stipple
.prim_is_poly
;
170 swr_generate_vs_key(struct swr_jit_vs_key
&key
,
171 struct swr_context
*ctx
,
172 swr_vertex_shader
*swr_vs
)
174 memset(&key
, 0, sizeof(key
));
176 key
.clip_plane_mask
=
177 swr_vs
->info
.base
.clipdist_writemask
?
178 swr_vs
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
179 ctx
->rasterizer
->clip_plane_enable
;
181 swr_generate_sampler_key(swr_vs
->info
, ctx
, PIPE_SHADER_VERTEX
, key
);
185 swr_generate_fetch_key(struct swr_jit_fetch_key
&key
,
186 struct swr_vertex_element_state
*velems
)
188 memset(&key
, 0, sizeof(key
));
190 key
.fsState
= velems
->fsState
;
194 swr_generate_gs_key(struct swr_jit_gs_key
&key
,
195 struct swr_context
*ctx
,
196 swr_geometry_shader
*swr_gs
)
198 memset(&key
, 0, sizeof(key
));
200 struct tgsi_shader_info
*pPrevShader
= &ctx
->vs
->info
.base
;
202 memcpy(&key
.vs_output_semantic_name
,
203 &pPrevShader
->output_semantic_name
,
204 sizeof(key
.vs_output_semantic_name
));
205 memcpy(&key
.vs_output_semantic_idx
,
206 &pPrevShader
->output_semantic_index
,
207 sizeof(key
.vs_output_semantic_idx
));
209 swr_generate_sampler_key(swr_gs
->info
, ctx
, PIPE_SHADER_GEOMETRY
, key
);
212 struct BuilderSWR
: public Builder
{
213 BuilderSWR(JitManager
*pJitMgr
, const char *pName
)
216 pJitMgr
->SetupNewModule();
217 gallivm
= gallivm_create(pName
, wrap(&JM()->mContext
));
218 pJitMgr
->mpCurrentModule
= unwrap(gallivm
->module
);
222 gallivm_free_ir(gallivm
);
225 void WriteVS(Value
*pVal
, Value
*pVsContext
, Value
*pVtxOutput
,
226 unsigned slot
, unsigned channel
);
228 struct gallivm_state
*gallivm
;
229 PFN_VERTEX_FUNC
CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
);
230 PFN_PIXEL_KERNEL
CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
);
231 PFN_GS_FUNC
CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
);
234 swr_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
235 struct lp_build_tgsi_context
* bld_base
,
236 boolean is_vindex_indirect
,
237 LLVMValueRef vertex_index
,
238 boolean is_aindex_indirect
,
239 LLVMValueRef attrib_index
,
240 LLVMValueRef swizzle_index
);
242 swr_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
243 struct lp_build_tgsi_context
* bld_base
,
244 LLVMValueRef (*outputs
)[4],
245 LLVMValueRef emitted_vertices_vec
);
248 swr_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
249 struct lp_build_tgsi_context
* bld_base
,
250 LLVMValueRef verts_per_prim_vec
,
251 LLVMValueRef emitted_prims_vec
);
254 swr_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
255 struct lp_build_tgsi_context
* bld_base
,
256 LLVMValueRef total_emitted_vertices_vec
,
257 LLVMValueRef emitted_prims_vec
);
261 struct swr_gs_llvm_iface
{
262 struct lp_build_tgsi_gs_iface base
;
263 struct tgsi_shader_info
*info
;
265 BuilderSWR
*pBuilder
;
268 SWR_GS_STATE
*pGsState
;
269 uint32_t num_outputs
;
270 uint32_t num_verts_per_prim
;
272 Value
*pVtxAttribMap
;
275 // trampoline functions so we can use the builder llvm construction methods
277 swr_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
278 struct lp_build_tgsi_context
* bld_base
,
279 boolean is_vindex_indirect
,
280 LLVMValueRef vertex_index
,
281 boolean is_aindex_indirect
,
282 LLVMValueRef attrib_index
,
283 LLVMValueRef swizzle_index
)
285 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_iface
;
287 return iface
->pBuilder
->swr_gs_llvm_fetch_input(gs_iface
, bld_base
,
296 swr_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
297 struct lp_build_tgsi_context
* bld_base
,
298 LLVMValueRef (*outputs
)[4],
299 LLVMValueRef emitted_vertices_vec
)
301 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
303 iface
->pBuilder
->swr_gs_llvm_emit_vertex(gs_base
, bld_base
,
305 emitted_vertices_vec
);
309 swr_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
310 struct lp_build_tgsi_context
* bld_base
,
311 LLVMValueRef verts_per_prim_vec
,
312 LLVMValueRef emitted_prims_vec
)
314 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
316 iface
->pBuilder
->swr_gs_llvm_end_primitive(gs_base
, bld_base
,
322 swr_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
323 struct lp_build_tgsi_context
* bld_base
,
324 LLVMValueRef total_emitted_vertices_vec
,
325 LLVMValueRef emitted_prims_vec
)
327 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
329 iface
->pBuilder
->swr_gs_llvm_epilogue(gs_base
, bld_base
,
330 total_emitted_vertices_vec
,
335 BuilderSWR::swr_gs_llvm_fetch_input(const struct lp_build_tgsi_gs_iface
*gs_iface
,
336 struct lp_build_tgsi_context
* bld_base
,
337 boolean is_vindex_indirect
,
338 LLVMValueRef vertex_index
,
339 boolean is_aindex_indirect
,
340 LLVMValueRef attrib_index
,
341 LLVMValueRef swizzle_index
)
343 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_iface
;
344 Value
*vert_index
= unwrap(vertex_index
);
345 Value
*attr_index
= unwrap(attrib_index
);
347 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
349 if (is_vindex_indirect
|| is_aindex_indirect
) {
351 Value
*res
= unwrap(bld_base
->base
.zero
);
352 struct lp_type type
= bld_base
->base
.type
;
354 for (i
= 0; i
< type
.length
; i
++) {
355 Value
*vert_chan_index
= vert_index
;
356 Value
*attr_chan_index
= attr_index
;
358 if (is_vindex_indirect
) {
359 vert_chan_index
= VEXTRACT(vert_index
, C(i
));
361 if (is_aindex_indirect
) {
362 attr_chan_index
= VEXTRACT(attr_index
, C(i
));
366 LOAD(GEP(iface
->pVtxAttribMap
, {C(0), attr_chan_index
}));
368 Value
*pVertex
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pVerts
});
369 Value
*pInputVertStride
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_inputVertStride
});
371 Value
*pVector
= ADD(MUL(vert_chan_index
, pInputVertStride
), attrib
);
372 Value
*pInput
= LOAD(GEP(pVertex
, {pVector
, unwrap(swizzle_index
)}));
374 Value
*value
= VEXTRACT(pInput
, C(i
));
375 res
= VINSERT(res
, value
, C(i
));
380 Value
*attrib
= LOAD(GEP(iface
->pVtxAttribMap
, {C(0), attr_index
}));
382 Value
*pVertex
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pVerts
});
383 Value
*pInputVertStride
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_inputVertStride
});
385 Value
*pVector
= ADD(MUL(vert_index
, pInputVertStride
), attrib
);
387 Value
*pInput
= LOAD(GEP(pVertex
, {pVector
, unwrap(swizzle_index
)}));
393 // GS output stream layout
394 #define VERTEX_COUNT_SIZE 32
395 #define CONTROL_HEADER_SIZE (8*32)
398 BuilderSWR::swr_gs_llvm_emit_vertex(const struct lp_build_tgsi_gs_iface
*gs_base
,
399 struct lp_build_tgsi_context
* bld_base
,
400 LLVMValueRef (*outputs
)[4],
401 LLVMValueRef emitted_vertices_vec
)
403 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
405 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
407 const uint32_t headerSize
= VERTEX_COUNT_SIZE
+ CONTROL_HEADER_SIZE
;
408 const uint32_t attribSize
= 4 * sizeof(float);
409 const uint32_t vertSize
= attribSize
* SWR_VTX_NUM_SLOTS
;
410 Value
*pVertexOffset
= MUL(unwrap(emitted_vertices_vec
), VIMMED1(vertSize
));
412 Value
*vMask
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_mask
});
413 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, mVWidth
));
415 Value
*pStack
= STACKSAVE();
416 Value
*pTmpPtr
= ALLOCA(mFP32Ty
, C(4)); // used for dummy write for lane masking
418 for (uint32_t attrib
= 0; attrib
< iface
->num_outputs
; ++attrib
) {
419 uint32_t attribSlot
= attrib
;
420 uint32_t sgvChannel
= 0;
421 if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
422 attribSlot
= VERTEX_SGV_SLOT
;
423 sgvChannel
= VERTEX_SGV_POINT_SIZE_COMP
;
424 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_LAYER
) {
425 attribSlot
= VERTEX_SGV_SLOT
;
426 sgvChannel
= VERTEX_SGV_RTAI_COMP
;
427 } else if (iface
->info
->output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
428 attribSlot
= VERTEX_POSITION_SLOT
;
430 attribSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
;
431 if (iface
->info
->writes_position
) {
436 Value
*pOutputOffset
= ADD(pVertexOffset
, VIMMED1(headerSize
+ attribSize
* attribSlot
)); // + sgvChannel ?
438 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
439 Value
*pLaneOffset
= VEXTRACT(pOutputOffset
, C(lane
));
440 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
441 Value
*pStreamOffset
= GEP(pStream
, pLaneOffset
);
442 pStreamOffset
= BITCAST(pStreamOffset
, mFP32PtrTy
);
444 Value
*pLaneMask
= VEXTRACT(vMask1
, C(lane
));
445 pStreamOffset
= SELECT(pLaneMask
, pStreamOffset
, pTmpPtr
);
447 for (uint32_t channel
= 0; channel
< 4; ++channel
) {
450 if (attribSlot
== VERTEX_SGV_SLOT
)
451 vData
= LOAD(unwrap(outputs
[attrib
][0]));
453 vData
= LOAD(unwrap(outputs
[attrib
][channel
]));
455 if (attribSlot
!= VERTEX_SGV_SLOT
||
456 sgvChannel
== channel
) {
457 vData
= VEXTRACT(vData
, C(lane
));
458 STORE(vData
, pStreamOffset
);
460 pStreamOffset
= GEP(pStreamOffset
, C(1));
465 STACKRESTORE(pStack
);
469 BuilderSWR::swr_gs_llvm_end_primitive(const struct lp_build_tgsi_gs_iface
*gs_base
,
470 struct lp_build_tgsi_context
* bld_base
,
471 LLVMValueRef verts_per_prim_vec
,
472 LLVMValueRef emitted_prims_vec
)
474 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
476 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
478 Value
*vMask
= LOAD(iface
->pGsCtx
, { 0, SWR_GS_CONTEXT_mask
});
479 Value
*vMask1
= TRUNC(vMask
, VectorType::get(mInt1Ty
, 8));
481 uint32_t vertsPerPrim
= iface
->num_verts_per_prim
;
484 ADD(MUL(unwrap(emitted_prims_vec
), VIMMED1(vertsPerPrim
)),
485 unwrap(verts_per_prim_vec
));
487 struct lp_build_tgsi_soa_context
*bld
= lp_soa_context(bld_base
);
488 vCount
= LOAD(unwrap(bld
->total_emitted_vertices_vec_ptr
));
490 struct lp_exec_mask
*exec_mask
= &bld
->exec_mask
;
491 Value
*mask
= unwrap(lp_build_mask_value(bld
->mask
));
492 if (exec_mask
->has_mask
)
493 mask
= AND(mask
, unwrap(exec_mask
->exec_mask
));
495 Value
*cmpMask
= VMASK(ICMP_NE(unwrap(verts_per_prim_vec
), VIMMED1(0)));
496 mask
= AND(mask
, cmpMask
);
497 vMask1
= TRUNC(mask
, VectorType::get(mInt1Ty
, 8));
499 vCount
= SUB(vCount
, VIMMED1(1));
500 Value
*vOffset
= ADD(UDIV(vCount
, VIMMED1(8)), VIMMED1(VERTEX_COUNT_SIZE
));
501 Value
*vValue
= SHL(VIMMED1(1), UREM(vCount
, VIMMED1(8)));
503 vValue
= TRUNC(vValue
, VectorType::get(mInt8Ty
, 8));
505 Value
*pStack
= STACKSAVE();
506 Value
*pTmpPtr
= ALLOCA(mInt8Ty
, C(4)); // used for dummy read/write for lane masking
508 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
) {
509 Value
*vLaneOffset
= VEXTRACT(vOffset
, C(lane
));
510 Value
*pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
511 Value
*pStreamOffset
= GEP(pStream
, vLaneOffset
);
513 Value
*pLaneMask
= VEXTRACT(vMask1
, C(lane
));
514 pStreamOffset
= SELECT(pLaneMask
, pStreamOffset
, pTmpPtr
);
516 Value
*vVal
= LOAD(pStreamOffset
);
517 vVal
= OR(vVal
, VEXTRACT(vValue
, C(lane
)));
518 STORE(vVal
, pStreamOffset
);
521 STACKRESTORE(pStack
);
525 BuilderSWR::swr_gs_llvm_epilogue(const struct lp_build_tgsi_gs_iface
*gs_base
,
526 struct lp_build_tgsi_context
* bld_base
,
527 LLVMValueRef total_emitted_vertices_vec
,
528 LLVMValueRef emitted_prims_vec
)
530 swr_gs_llvm_iface
*iface
= (swr_gs_llvm_iface
*)gs_base
;
532 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
534 // Store emit count to each output stream in the first DWORD
535 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
537 Value
* pStream
= LOAD(iface
->pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
538 pStream
= BITCAST(pStream
, mInt32PtrTy
);
539 Value
* pLaneCount
= VEXTRACT(unwrap(total_emitted_vertices_vec
), C(lane
));
540 STORE(pLaneCount
, pStream
);
545 BuilderSWR::CompileGS(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
547 SWR_GS_STATE
*pGS
= &ctx
->gs
->gsState
;
548 struct tgsi_shader_info
*info
= &ctx
->gs
->info
.base
;
550 memset(pGS
, 0, sizeof(*pGS
));
552 pGS
->gsEnable
= true;
554 pGS
->numInputAttribs
= info
->num_inputs
;
555 pGS
->outputTopology
=
556 swr_convert_prim_topology(info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
557 pGS
->maxNumVerts
= info
->properties
[TGSI_PROPERTY_GS_MAX_OUTPUT_VERTICES
];
558 pGS
->instanceCount
= info
->properties
[TGSI_PROPERTY_GS_INVOCATIONS
];
560 // XXX: single stream for now...
561 pGS
->isSingleStream
= true;
562 pGS
->singleStreamID
= 0;
564 pGS
->vertexAttribOffset
= VERTEX_ATTRIB_START_SLOT
; // TODO: optimize
565 pGS
->srcVertexAttribOffset
= VERTEX_ATTRIB_START_SLOT
; // TODO: optimize
566 pGS
->inputVertStride
= pGS
->numInputAttribs
+ pGS
->vertexAttribOffset
;
567 pGS
->outputVertexSize
= SWR_VTX_NUM_SLOTS
;
568 pGS
->controlDataSize
= 8; // GS ouputs max of 8 32B units
569 pGS
->controlDataOffset
= VERTEX_COUNT_SIZE
;
570 pGS
->outputVertexOffset
= pGS
->controlDataOffset
+ CONTROL_HEADER_SIZE
;
572 pGS
->allocationSize
=
573 VERTEX_COUNT_SIZE
+ // vertex count
574 CONTROL_HEADER_SIZE
+ // control header
575 (SWR_VTX_NUM_SLOTS
* 16) * // sizeof vertex
576 pGS
->maxNumVerts
; // num verts
578 struct swr_geometry_shader
*gs
= ctx
->gs
;
580 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
581 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
583 memset(outputs
, 0, sizeof(outputs
));
585 AttrBuilder attrBuilder
;
586 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
588 std::vector
<Type
*> gsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
589 PointerType::get(Gen_SWR_GS_CONTEXT(JM()), 0)};
590 FunctionType
*vsFuncType
=
591 FunctionType::get(Type::getVoidTy(JM()->mContext
), gsArgs
, false);
593 // create new vertex shader function
594 auto pFunction
= Function::Create(vsFuncType
,
595 GlobalValue::ExternalLinkage
,
597 JM()->mpCurrentModule
);
598 #if HAVE_LLVM < 0x0500
599 AttributeSet attrSet
= AttributeSet::get(
600 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
601 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
603 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
606 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
607 IRB()->SetInsertPoint(block
);
608 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
610 auto argitr
= pFunction
->arg_begin();
611 Value
*hPrivateData
= &*argitr
++;
612 hPrivateData
->setName("hPrivateData");
613 Value
*pGsCtx
= &*argitr
++;
614 pGsCtx
->setName("gsCtx");
617 GEP(hPrivateData
, {C(0), C(swr_draw_context_constantGS
)});
618 consts_ptr
->setName("gs_constants");
619 Value
*const_sizes_ptr
=
620 GEP(hPrivateData
, {0, swr_draw_context_num_constantsGS
});
621 const_sizes_ptr
->setName("num_gs_constants");
623 struct lp_build_sampler_soa
*sampler
=
624 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_GEOMETRY
);
626 struct lp_bld_tgsi_system_values system_values
;
627 memset(&system_values
, 0, sizeof(system_values
));
628 system_values
.prim_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_PrimitiveID
}));
629 system_values
.instance_id
= wrap(LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_InstanceID
}));
631 std::vector
<Constant
*> mapConstants
;
632 Value
*vtxAttribMap
= ALLOCA(ArrayType::get(mInt32Ty
, PIPE_MAX_SHADER_INPUTS
));
633 for (unsigned slot
= 0; slot
< info
->num_inputs
; slot
++) {
634 ubyte semantic_name
= info
->input_semantic_name
[slot
];
635 ubyte semantic_idx
= info
->input_semantic_index
[slot
];
637 unsigned vs_slot
= locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
);
639 vs_slot
+= VERTEX_ATTRIB_START_SLOT
;
641 if (ctx
->vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
644 if (semantic_name
== TGSI_SEMANTIC_POSITION
)
645 vs_slot
= VERTEX_POSITION_SLOT
;
647 STORE(C(vs_slot
), vtxAttribMap
, {0, slot
});
648 mapConstants
.push_back(C(vs_slot
));
651 struct lp_build_mask_context mask
;
652 Value
*mask_val
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_mask
}, "gsMask");
653 lp_build_mask_begin(&mask
, gallivm
,
654 lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
656 // zero out cut buffer so we can load/modify/store bits
657 for (uint32_t lane
= 0; lane
< mVWidth
; ++lane
)
659 Value
* pStream
= LOAD(pGsCtx
, {0, SWR_GS_CONTEXT_pStreams
, lane
});
660 MEMSET(pStream
, C((char)0), VERTEX_COUNT_SIZE
+ CONTROL_HEADER_SIZE
, sizeof(float) * KNOB_SIMD_WIDTH
);
663 struct swr_gs_llvm_iface gs_iface
;
664 gs_iface
.base
.fetch_input
= ::swr_gs_llvm_fetch_input
;
665 gs_iface
.base
.emit_vertex
= ::swr_gs_llvm_emit_vertex
;
666 gs_iface
.base
.end_primitive
= ::swr_gs_llvm_end_primitive
;
667 gs_iface
.base
.gs_epilogue
= ::swr_gs_llvm_epilogue
;
668 gs_iface
.pBuilder
= this;
669 gs_iface
.pGsCtx
= pGsCtx
;
670 gs_iface
.pGsState
= pGS
;
671 gs_iface
.num_outputs
= gs
->info
.base
.num_outputs
;
672 gs_iface
.num_verts_per_prim
=
673 u_vertices_per_prim((pipe_prim_type
)info
->properties
[TGSI_PROPERTY_GS_OUTPUT_PRIM
]);
674 gs_iface
.info
= info
;
675 gs_iface
.pVtxAttribMap
= vtxAttribMap
;
677 lp_build_tgsi_soa(gallivm
,
679 lp_type_float_vec(32, 32 * 8),
682 wrap(const_sizes_ptr
),
686 wrap(hPrivateData
), // (sampler context)
692 lp_build_mask_end(&mask
);
694 sampler
->destroy(sampler
);
696 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
700 gallivm_verify_function(gallivm
, wrap(pFunction
));
701 gallivm_compile_module(gallivm
);
704 (PFN_GS_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
706 debug_printf("geom shader %p\n", pFunc
);
707 assert(pFunc
&& "Error: GeomShader = NULL");
709 JM()->mIsModuleFinalized
= true;
715 swr_compile_gs(struct swr_context
*ctx
, swr_jit_gs_key
&key
)
718 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
720 PFN_GS_FUNC func
= builder
.CompileGS(ctx
, key
);
722 ctx
->gs
->map
.insert(std::make_pair(key
, make_unique
<VariantGS
>(builder
.gallivm
, func
)));
727 BuilderSWR::WriteVS(Value
*pVal
, Value
*pVsContext
, Value
*pVtxOutput
, unsigned slot
, unsigned channel
)
729 #if USE_SIMD16_FRONTEND && !USE_SIMD16_VS
730 // interleave the simdvertex components into the dest simd16vertex
731 // slot16offset = slot8offset * 2
732 // comp16offset = comp8offset * 2 + alternateOffset
734 Value
*offset
= LOAD(pVsContext
, { 0, SWR_VS_CONTEXT_AlternateOffset
});
735 Value
*pOut
= GEP(pVtxOutput
, { C(0), C(0), C(slot
* 2), offset
} );
736 STORE(pVal
, pOut
, {channel
* 2});
738 Value
*pOut
= GEP(pVtxOutput
, {0, 0, slot
});
739 STORE(pVal
, pOut
, {0, channel
});
744 BuilderSWR::CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
746 struct swr_vertex_shader
*swr_vs
= ctx
->vs
;
748 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
749 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
751 memset(outputs
, 0, sizeof(outputs
));
753 AttrBuilder attrBuilder
;
754 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
756 std::vector
<Type
*> vsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
757 PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};
758 FunctionType
*vsFuncType
=
759 FunctionType::get(Type::getVoidTy(JM()->mContext
), vsArgs
, false);
761 // create new vertex shader function
762 auto pFunction
= Function::Create(vsFuncType
,
763 GlobalValue::ExternalLinkage
,
765 JM()->mpCurrentModule
);
766 #if HAVE_LLVM < 0x0500
767 AttributeSet attrSet
= AttributeSet::get(
768 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
769 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
771 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
774 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
775 IRB()->SetInsertPoint(block
);
776 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
778 auto argitr
= pFunction
->arg_begin();
779 Value
*hPrivateData
= &*argitr
++;
780 hPrivateData
->setName("hPrivateData");
781 Value
*pVsCtx
= &*argitr
++;
782 pVsCtx
->setName("vsCtx");
784 Value
*consts_ptr
= GEP(hPrivateData
, {C(0), C(swr_draw_context_constantVS
)});
786 consts_ptr
->setName("vs_constants");
787 Value
*const_sizes_ptr
=
788 GEP(hPrivateData
, {0, swr_draw_context_num_constantsVS
});
789 const_sizes_ptr
->setName("num_vs_constants");
791 Value
*vtxInput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVin
});
793 vtxInput
= BITCAST(vtxInput
, PointerType::get(Gen_simd16vertex(JM()), 0));
796 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
797 const unsigned mask
= swr_vs
->info
.base
.input_usage_mask
[attrib
];
798 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
799 if (mask
& (1 << channel
)) {
800 inputs
[attrib
][channel
] =
801 wrap(LOAD(vtxInput
, {0, 0, attrib
, channel
}));
806 struct lp_build_sampler_soa
*sampler
=
807 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_VERTEX
);
809 struct lp_bld_tgsi_system_values system_values
;
810 memset(&system_values
, 0, sizeof(system_values
));
811 system_values
.instance_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_InstanceID
}));
814 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID16
}));
816 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID
}));
820 uint32_t vectorWidth
= mVWidth16
;
822 uint32_t vectorWidth
= mVWidth
;
825 lp_build_tgsi_soa(gallivm
,
827 lp_type_float_vec(32, 32 * vectorWidth
),
830 wrap(const_sizes_ptr
),
834 wrap(hPrivateData
), // (sampler context)
838 NULL
); // geometry shader face
840 sampler
->destroy(sampler
);
842 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
844 Value
*vtxOutput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVout
});
846 vtxOutput
= BITCAST(vtxOutput
, PointerType::get(Gen_simd16vertex(JM()), 0));
849 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
850 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
851 if (!outputs
[attrib
][channel
])
857 if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
) {
858 if (channel
!= VERTEX_SGV_POINT_SIZE_COMP
)
860 val
= LOAD(unwrap(outputs
[attrib
][0]));
861 outSlot
= VERTEX_SGV_SLOT
;
862 } else if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_POSITION
) {
863 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
864 outSlot
= VERTEX_POSITION_SLOT
;
866 val
= LOAD(unwrap(outputs
[attrib
][channel
]));
867 outSlot
= VERTEX_ATTRIB_START_SLOT
+ attrib
;
868 if (swr_vs
->info
.base
.output_semantic_name
[0] == TGSI_SEMANTIC_POSITION
)
872 WriteVS(val
, pVsCtx
, vtxOutput
, outSlot
, channel
);
876 if (ctx
->rasterizer
->clip_plane_enable
||
877 swr_vs
->info
.base
.culldist_writemask
) {
878 unsigned clip_mask
= ctx
->rasterizer
->clip_plane_enable
;
881 if (swr_vs
->info
.base
.writes_clipvertex
) {
882 cv
= locate_linkage(TGSI_SEMANTIC_CLIPVERTEX
, 0,
885 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
886 if (swr_vs
->info
.base
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
887 swr_vs
->info
.base
.output_semantic_index
[i
] == 0) {
893 LLVMValueRef cx
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][0], "");
894 LLVMValueRef cy
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][1], "");
895 LLVMValueRef cz
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][2], "");
896 LLVMValueRef cw
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][3], "");
898 for (unsigned val
= 0; val
< PIPE_MAX_CLIP_PLANES
; val
++) {
899 // clip distance overrides user clip planes
900 if ((swr_vs
->info
.base
.clipdist_writemask
& clip_mask
& (1 << val
)) ||
901 ((swr_vs
->info
.base
.culldist_writemask
<< swr_vs
->info
.base
.num_written_clipdistance
) & (1 << val
))) {
902 unsigned cv
= locate_linkage(TGSI_SEMANTIC_CLIPDIST
, val
< 4 ? 0 : 1,
905 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
], "");
906 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
908 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
- 4], "");
909 WriteVS(unwrap(dist
), pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
914 if (!(clip_mask
& (1 << val
)))
917 Value
*px
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 0}));
918 Value
*py
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 1}));
919 Value
*pz
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 2}));
920 Value
*pw
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 3}));
922 Value
*bpx
= VBROADCAST_16(px
);
923 Value
*bpy
= VBROADCAST_16(py
);
924 Value
*bpz
= VBROADCAST_16(pz
);
925 Value
*bpw
= VBROADCAST_16(pw
);
927 Value
*bpx
= VBROADCAST(px
);
928 Value
*bpy
= VBROADCAST(py
);
929 Value
*bpz
= VBROADCAST(pz
);
930 Value
*bpw
= VBROADCAST(pw
);
932 Value
*dist
= FADD(FMUL(unwrap(cx
), bpx
),
933 FADD(FMUL(unwrap(cy
), bpy
),
934 FADD(FMUL(unwrap(cz
), bpz
),
935 FMUL(unwrap(cw
), bpw
))));
938 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
);
940 WriteVS(dist
, pVsCtx
, vtxOutput
, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4);
946 gallivm_verify_function(gallivm
, wrap(pFunction
));
947 gallivm_compile_module(gallivm
);
949 // lp_debug_dump_value(func);
951 PFN_VERTEX_FUNC pFunc
=
952 (PFN_VERTEX_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
954 debug_printf("vert shader %p\n", pFunc
);
955 assert(pFunc
&& "Error: VertShader = NULL");
957 JM()->mIsModuleFinalized
= true;
963 swr_compile_vs(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
965 if (!ctx
->vs
->pipe
.tokens
)
969 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
971 PFN_VERTEX_FUNC func
= builder
.CompileVS(ctx
, key
);
973 ctx
->vs
->map
.insert(std::make_pair(key
, make_unique
<VariantVS
>(builder
.gallivm
, func
)));
978 swr_so_adjust_attrib(unsigned in_attrib
,
979 swr_vertex_shader
*swr_vs
)
984 attrib
= in_attrib
+ VERTEX_ATTRIB_START_SLOT
;
987 semantic_name
= swr_vs
->info
.base
.output_semantic_name
[in_attrib
];
988 if (semantic_name
== TGSI_SEMANTIC_POSITION
) {
989 attrib
= VERTEX_POSITION_SLOT
;
990 } else if (semantic_name
== TGSI_SEMANTIC_PSIZE
) {
991 attrib
= VERTEX_SGV_SLOT
;
992 } else if (semantic_name
== TGSI_SEMANTIC_LAYER
) {
993 attrib
= VERTEX_SGV_SLOT
;
995 if (swr_vs
->info
.base
.writes_position
) {
1005 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
)
1007 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
1008 if ((info
->output_semantic_name
[i
] == name
)
1009 && (info
->output_semantic_index
[i
] == index
)) {
1018 BuilderSWR::CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
1020 struct swr_fragment_shader
*swr_fs
= ctx
->fs
;
1022 struct tgsi_shader_info
*pPrevShader
;
1024 pPrevShader
= &ctx
->gs
->info
.base
;
1026 pPrevShader
= &ctx
->vs
->info
.base
;
1028 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
1029 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
1031 memset(inputs
, 0, sizeof(inputs
));
1032 memset(outputs
, 0, sizeof(outputs
));
1034 struct lp_build_sampler_soa
*sampler
= NULL
;
1036 AttrBuilder attrBuilder
;
1037 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
1039 std::vector
<Type
*> fsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
1040 PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};
1041 FunctionType
*funcType
=
1042 FunctionType::get(Type::getVoidTy(JM()->mContext
), fsArgs
, false);
1044 auto pFunction
= Function::Create(funcType
,
1045 GlobalValue::ExternalLinkage
,
1047 JM()->mpCurrentModule
);
1048 #if HAVE_LLVM < 0x0500
1049 AttributeSet attrSet
= AttributeSet::get(
1050 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
1051 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
1053 pFunction
->addAttributes(AttributeList::FunctionIndex
, attrBuilder
);
1056 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
1057 IRB()->SetInsertPoint(block
);
1058 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
1060 auto args
= pFunction
->arg_begin();
1061 Value
*hPrivateData
= &*args
++;
1062 hPrivateData
->setName("hPrivateData");
1063 Value
*pPS
= &*args
++;
1064 pPS
->setName("psCtx");
1066 Value
*consts_ptr
= GEP(hPrivateData
, {0, swr_draw_context_constantFS
});
1067 consts_ptr
->setName("fs_constants");
1068 Value
*const_sizes_ptr
=
1069 GEP(hPrivateData
, {0, swr_draw_context_num_constantsFS
});
1070 const_sizes_ptr
->setName("num_fs_constants");
1072 // load *pAttribs, *pPerspAttribs
1073 Value
*pRawAttribs
= LOAD(pPS
, {0, SWR_PS_CONTEXT_pAttribs
}, "pRawAttribs");
1074 Value
*pPerspAttribs
=
1075 LOAD(pPS
, {0, SWR_PS_CONTEXT_pPerspAttribs
}, "pPerspAttribs");
1077 swr_fs
->constantMask
= 0;
1078 swr_fs
->flatConstantMask
= 0;
1079 swr_fs
->pointSpriteMask
= 0;
1081 for (int attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
1082 const unsigned mask
= swr_fs
->info
.base
.input_usage_mask
[attrib
];
1083 const unsigned interpMode
= swr_fs
->info
.base
.input_interpolate
[attrib
];
1084 const unsigned interpLoc
= swr_fs
->info
.base
.input_interpolate_loc
[attrib
];
1090 Value
*vi
= nullptr, *vj
= nullptr;
1091 switch (interpLoc
) {
1092 case TGSI_INTERPOLATE_LOC_CENTER
:
1093 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_center
}, "i");
1094 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_center
}, "j");
1096 case TGSI_INTERPOLATE_LOC_CENTROID
:
1097 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_centroid
}, "i");
1098 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_centroid
}, "j");
1100 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1101 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_sample
}, "i");
1102 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_sample
}, "j");
1107 Value
*vw
= nullptr, *pAttribs
;
1108 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
1109 interpMode
== TGSI_INTERPOLATE_COLOR
) {
1110 pAttribs
= pPerspAttribs
;
1111 switch (interpLoc
) {
1112 case TGSI_INTERPOLATE_LOC_CENTER
:
1113 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}));
1115 case TGSI_INTERPOLATE_LOC_CENTROID
:
1116 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_centroid
}));
1118 case TGSI_INTERPOLATE_LOC_SAMPLE
:
1119 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_sample
}));
1123 pAttribs
= pRawAttribs
;
1129 ubyte semantic_name
= swr_fs
->info
.base
.input_semantic_name
[attrib
];
1130 ubyte semantic_idx
= swr_fs
->info
.base
.input_semantic_index
[attrib
];
1132 if (semantic_name
== TGSI_SEMANTIC_FACE
) {
1134 UI_TO_FP(LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), mFP32Ty
);
1135 ff
= FSUB(FMUL(ff
, C(2.0f
)), C(1.0f
));
1136 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vFrontFace");
1138 inputs
[attrib
][0] = wrap(ff
);
1139 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1140 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1141 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
1143 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
) { // gl_FragCoord
1144 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
1145 TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
) {
1146 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_center
}, "vX"));
1147 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_center
}, "vY"));
1149 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
}, "vX"));
1150 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
}, "vY"));
1152 inputs
[attrib
][2] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vZ
}, "vZ"));
1154 wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}, "vOneOverW"));
1158 unsigned linkedAttrib
=
1159 locate_linkage(semantic_name
, semantic_idx
, pPrevShader
) - 1;
1161 uint32_t extraAttribs
= 0;
1162 if (semantic_name
== TGSI_SEMANTIC_PRIMID
&& !ctx
->gs
) {
1163 /* non-gs generated primID - need to grab from swizzleMap override */
1164 linkedAttrib
= pPrevShader
->num_outputs
- 1;
1165 swr_fs
->constantMask
|= 1 << linkedAttrib
;
1167 } else if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
1168 key
.sprite_coord_enable
& (1 << semantic_idx
)) {
1169 /* we add an extra attrib to the backendState in swr_update_derived. */
1170 linkedAttrib
= pPrevShader
->num_outputs
+ extraAttribs
- 1;
1171 swr_fs
->pointSpriteMask
|= (1 << linkedAttrib
);
1173 } else if (linkedAttrib
== 0xFFFFFFFF) {
1174 inputs
[attrib
][0] = wrap(VIMMED1(0.0f
));
1175 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
1176 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
1177 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
1178 /* If we're reading in color and 2-sided lighting is enabled, we have
1181 if (semantic_name
!= TGSI_SEMANTIC_COLOR
|| !key
.light_twoside
)
1184 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1185 swr_fs
->constantMask
|= 1 << linkedAttrib
;
1186 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1187 swr_fs
->flatConstantMask
|= 1 << linkedAttrib
;
1191 unsigned bcolorAttrib
= 0xFFFFFFFF;
1192 Value
*offset
= NULL
;
1193 if (semantic_name
== TGSI_SEMANTIC_COLOR
&& key
.light_twoside
) {
1194 bcolorAttrib
= locate_linkage(
1195 TGSI_SEMANTIC_BCOLOR
, semantic_idx
, pPrevShader
) - 1;
1196 /* Neither front nor back colors were available. Nothing to load. */
1197 if (bcolorAttrib
== 0xFFFFFFFF && linkedAttrib
== 0xFFFFFFFF)
1199 /* If there is no front color, just always use the back color. */
1200 if (linkedAttrib
== 0xFFFFFFFF)
1201 linkedAttrib
= bcolorAttrib
;
1203 if (bcolorAttrib
!= 0xFFFFFFFF) {
1204 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1205 swr_fs
->constantMask
|= 1 << bcolorAttrib
;
1206 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
1207 swr_fs
->flatConstantMask
|= 1 << bcolorAttrib
;
1210 unsigned diff
= 12 * (bcolorAttrib
- linkedAttrib
);
1214 XOR(C(1), LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), "backFace");
1216 offset
= MUL(back
, C(diff
));
1217 offset
->setName("offset");
1222 for (int channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1223 if (mask
& (1 << channel
)) {
1224 Value
*indexA
= C(linkedAttrib
* 12 + channel
);
1225 Value
*indexB
= C(linkedAttrib
* 12 + channel
+ 4);
1226 Value
*indexC
= C(linkedAttrib
* 12 + channel
+ 8);
1229 indexA
= ADD(indexA
, offset
);
1230 indexB
= ADD(indexB
, offset
);
1231 indexC
= ADD(indexC
, offset
);
1234 Value
*va
= VBROADCAST(LOAD(GEP(pAttribs
, indexA
)));
1235 Value
*vb
= VBROADCAST(LOAD(GEP(pAttribs
, indexB
)));
1236 Value
*vc
= VBROADCAST(LOAD(GEP(pAttribs
, indexC
)));
1238 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
1239 inputs
[attrib
][channel
] = wrap(va
);
1241 Value
*vk
= FSUB(FSUB(VIMMED1(1.0f
), vi
), vj
);
1245 Value
*interp
= FMUL(va
, vi
);
1246 Value
*interp1
= FMUL(vb
, vj
);
1247 interp
= FADD(interp
, interp1
);
1248 interp
= FADD(interp
, vc
);
1249 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
1250 interpMode
== TGSI_INTERPOLATE_COLOR
)
1251 interp
= FMUL(interp
, vw
);
1252 inputs
[attrib
][channel
] = wrap(interp
);
1258 sampler
= swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_FRAGMENT
);
1260 struct lp_bld_tgsi_system_values system_values
;
1261 memset(&system_values
, 0, sizeof(system_values
));
1263 struct lp_build_mask_context mask
;
1264 bool uses_mask
= false;
1266 if (swr_fs
->info
.base
.uses_kill
||
1267 key
.poly_stipple_enable
) {
1268 Value
*vActiveMask
= NULL
;
1269 if (swr_fs
->info
.base
.uses_kill
) {
1270 vActiveMask
= LOAD(pPS
, {0, SWR_PS_CONTEXT_activeMask
}, "activeMask");
1272 if (key
.poly_stipple_enable
) {
1273 // first get fragment xy coords and clip to stipple bounds
1274 Value
*vXf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
});
1275 Value
*vYf
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
});
1276 Value
*vXu
= FP_TO_UI(vXf
, mSimdInt32Ty
);
1277 Value
*vYu
= FP_TO_UI(vYf
, mSimdInt32Ty
);
1279 // stipple pattern is 32x32, which means that one line of stipple
1280 // is stored in one word:
1281 // vXstipple is bit offset inside 32-bit stipple word
1282 // vYstipple is word index is stipple array
1283 Value
*vXstipple
= AND(vXu
, VIMMED1(0x1f)); // & (32-1)
1284 Value
*vYstipple
= AND(vYu
, VIMMED1(0x1f)); // & (32-1)
1286 // grab stipple pattern base address
1287 Value
*stipplePtr
= GEP(hPrivateData
, {0, swr_draw_context_polyStipple
, 0});
1288 stipplePtr
= BITCAST(stipplePtr
, mInt8PtrTy
);
1290 // peform a gather to grab stipple words for each lane
1291 Value
*vStipple
= GATHERDD(VUNDEF_I(), stipplePtr
, vYstipple
,
1292 VIMMED1(0xffffffff), 4);
1294 // create a mask with one bit corresponding to the x stipple
1295 // and AND it with the pattern, to see if we have a bit
1296 Value
*vBitMask
= LSHR(VIMMED1(0x80000000), vXstipple
);
1297 Value
*vStippleMask
= AND(vStipple
, vBitMask
);
1298 vStippleMask
= ICMP_NE(vStippleMask
, VIMMED1(0));
1299 vStippleMask
= VMASK(vStippleMask
);
1301 if (swr_fs
->info
.base
.uses_kill
) {
1302 vActiveMask
= AND(vActiveMask
, vStippleMask
);
1304 vActiveMask
= vStippleMask
;
1307 lp_build_mask_begin(
1308 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(vActiveMask
));
1312 lp_build_tgsi_soa(gallivm
,
1313 swr_fs
->pipe
.tokens
,
1314 lp_type_float_vec(32, 32 * 8),
1315 uses_mask
? &mask
: NULL
, // mask
1317 wrap(const_sizes_ptr
),
1322 NULL
, // thread data
1325 NULL
); // geometry shader face
1327 sampler
->destroy(sampler
);
1329 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1331 for (uint32_t attrib
= 0; attrib
< swr_fs
->info
.base
.num_outputs
;
1333 switch (swr_fs
->info
.base
.output_semantic_name
[attrib
]) {
1334 case TGSI_SEMANTIC_POSITION
: {
1337 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][2], "");
1338 STORE(unwrap(outZ
), pPS
, {0, SWR_PS_CONTEXT_vZ
});
1341 case TGSI_SEMANTIC_COLOR
: {
1342 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
1343 if (!outputs
[attrib
][channel
])
1347 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][channel
], "");
1348 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
1349 swr_fs
->info
.base
.output_semantic_index
[attrib
] == 0) {
1350 for (uint32_t rt
= 0; rt
< key
.nr_cbufs
; rt
++) {
1353 {0, SWR_PS_CONTEXT_shaded
, rt
, channel
});
1359 SWR_PS_CONTEXT_shaded
,
1360 swr_fs
->info
.base
.output_semantic_index
[attrib
],
1368 "unknown output from FS %s[%d]\n",
1369 tgsi_semantic_names
[swr_fs
->info
.base
1370 .output_semantic_name
[attrib
]],
1371 swr_fs
->info
.base
.output_semantic_index
[attrib
]);
1377 LLVMValueRef mask_result
= 0;
1379 mask_result
= lp_build_mask_end(&mask
);
1382 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
1385 STORE(unwrap(mask_result
), pPS
, {0, SWR_PS_CONTEXT_activeMask
});
1390 gallivm_verify_function(gallivm
, wrap(pFunction
));
1392 gallivm_compile_module(gallivm
);
1394 // after the gallivm passes, we have to lower the core's intrinsics
1395 llvm::legacy::FunctionPassManager
lowerPass(JM()->mpCurrentModule
);
1396 lowerPass
.add(createLowerX86Pass(mpJitMgr
, this));
1397 lowerPass
.run(*pFunction
);
1399 PFN_PIXEL_KERNEL kernel
=
1400 (PFN_PIXEL_KERNEL
)gallivm_jit_function(gallivm
, wrap(pFunction
));
1401 debug_printf("frag shader %p\n", kernel
);
1402 assert(kernel
&& "Error: FragShader = NULL");
1404 JM()->mIsModuleFinalized
= true;
1410 swr_compile_fs(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
1412 if (!ctx
->fs
->pipe
.tokens
)
1416 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
1418 PFN_PIXEL_KERNEL func
= builder
.CompileFS(ctx
, key
);
1420 ctx
->fs
->map
.insert(std::make_pair(key
, make_unique
<VariantFS
>(builder
.gallivm
, func
)));