1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 ***************************************************************************/
24 // llvm redefines DEBUG
25 #pragma push_macro("DEBUG")
27 #include "JitManager.h"
28 #include "llvm-c/Core.h"
29 #include "llvm/Support/CBindingWrapping.h"
30 #pragma pop_macro("DEBUG")
33 #include "state_llvm.h"
36 #include "tgsi/tgsi_strings.h"
37 #include "gallivm/lp_bld_init.h"
38 #include "gallivm/lp_bld_flow.h"
39 #include "gallivm/lp_bld_struct.h"
40 #include "gallivm/lp_bld_tgsi.h"
42 #include "swr_context.h"
43 #include "swr_context_llvm.h"
44 #include "swr_state.h"
45 #include "swr_screen.h"
47 using namespace SwrJit
;
50 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
);
52 bool operator==(const swr_jit_fs_key
&lhs
, const swr_jit_fs_key
&rhs
)
54 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
57 bool operator==(const swr_jit_vs_key
&lhs
, const swr_jit_vs_key
&rhs
)
59 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
63 swr_generate_sampler_key(const struct lp_tgsi_info
&info
,
64 struct swr_context
*ctx
,
66 struct swr_jit_sampler_key
&key
)
68 key
.nr_samplers
= info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
70 for (unsigned i
= 0; i
< key
.nr_samplers
; i
++) {
71 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
72 lp_sampler_static_sampler_state(
73 &key
.sampler
[i
].sampler_state
,
74 ctx
->samplers
[shader_type
][i
]);
79 * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes
80 * are dx10-style? Can't really have mixed opcodes, at least not
81 * if we want to skip the holes here (without rescanning tgsi).
83 if (info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
84 key
.nr_sampler_views
=
85 info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
86 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
87 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER_VIEW
] & (1 << i
)) {
88 lp_sampler_static_texture_state(
89 &key
.sampler
[i
].texture_state
,
90 ctx
->sampler_views
[shader_type
][i
]);
94 key
.nr_sampler_views
= key
.nr_samplers
;
95 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
96 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
97 lp_sampler_static_texture_state(
98 &key
.sampler
[i
].texture_state
,
99 ctx
->sampler_views
[shader_type
][i
]);
106 swr_generate_fs_key(struct swr_jit_fs_key
&key
,
107 struct swr_context
*ctx
,
108 swr_fragment_shader
*swr_fs
)
110 memset(&key
, 0, sizeof(key
));
112 key
.nr_cbufs
= ctx
->framebuffer
.nr_cbufs
;
113 key
.light_twoside
= ctx
->rasterizer
->light_twoside
;
114 memcpy(&key
.vs_output_semantic_name
,
115 &ctx
->vs
->info
.base
.output_semantic_name
,
116 sizeof(key
.vs_output_semantic_name
));
117 memcpy(&key
.vs_output_semantic_idx
,
118 &ctx
->vs
->info
.base
.output_semantic_index
,
119 sizeof(key
.vs_output_semantic_idx
));
121 swr_generate_sampler_key(swr_fs
->info
, ctx
, PIPE_SHADER_FRAGMENT
, key
);
125 swr_generate_vs_key(struct swr_jit_vs_key
&key
,
126 struct swr_context
*ctx
,
127 swr_vertex_shader
*swr_vs
)
129 memset(&key
, 0, sizeof(key
));
131 key
.clip_plane_mask
=
132 swr_vs
->info
.base
.clipdist_writemask
?
133 swr_vs
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
134 ctx
->rasterizer
->clip_plane_enable
;
136 swr_generate_sampler_key(swr_vs
->info
, ctx
, PIPE_SHADER_VERTEX
, key
);
139 struct BuilderSWR
: public Builder
{
140 BuilderSWR(JitManager
*pJitMgr
, const char *pName
)
143 pJitMgr
->SetupNewModule();
144 gallivm
= gallivm_create(pName
, wrap(&JM()->mContext
));
145 pJitMgr
->mpCurrentModule
= unwrap(gallivm
->module
);
149 gallivm_free_ir(gallivm
);
152 struct gallivm_state
*gallivm
;
153 PFN_VERTEX_FUNC
CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
);
154 PFN_PIXEL_KERNEL
CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
);
158 BuilderSWR::CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
160 struct swr_vertex_shader
*swr_vs
= ctx
->vs
;
162 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
163 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
165 memset(outputs
, 0, sizeof(outputs
));
167 AttrBuilder attrBuilder
;
168 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
169 AttributeSet attrSet
= AttributeSet::get(
170 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
172 std::vector
<Type
*> vsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
173 PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};
174 FunctionType
*vsFuncType
=
175 FunctionType::get(Type::getVoidTy(JM()->mContext
), vsArgs
, false);
177 // create new vertex shader function
178 auto pFunction
= Function::Create(vsFuncType
,
179 GlobalValue::ExternalLinkage
,
181 JM()->mpCurrentModule
);
182 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
184 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
185 IRB()->SetInsertPoint(block
);
186 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
188 auto argitr
= pFunction
->arg_begin();
189 Value
*hPrivateData
= &*argitr
++;
190 hPrivateData
->setName("hPrivateData");
191 Value
*pVsCtx
= &*argitr
++;
192 pVsCtx
->setName("vsCtx");
194 Value
*consts_ptr
= GEP(hPrivateData
, {C(0), C(swr_draw_context_constantVS
)});
196 consts_ptr
->setName("vs_constants");
197 Value
*const_sizes_ptr
=
198 GEP(hPrivateData
, {0, swr_draw_context_num_constantsVS
});
199 const_sizes_ptr
->setName("num_vs_constants");
201 Value
*vtxInput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVin
});
203 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
204 const unsigned mask
= swr_vs
->info
.base
.input_usage_mask
[attrib
];
205 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
206 if (mask
& (1 << channel
)) {
207 inputs
[attrib
][channel
] =
208 wrap(LOAD(vtxInput
, {0, 0, attrib
, channel
}));
213 struct lp_build_sampler_soa
*sampler
=
214 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_VERTEX
);
216 struct lp_bld_tgsi_system_values system_values
;
217 memset(&system_values
, 0, sizeof(system_values
));
218 system_values
.instance_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_InstanceID
}));
219 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID
}));
221 lp_build_tgsi_soa(gallivm
,
223 lp_type_float_vec(32, 32 * 8),
226 wrap(const_sizes_ptr
),
230 wrap(hPrivateData
), // (sampler context)
234 NULL
); // geometry shader face
236 sampler
->destroy(sampler
);
238 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
240 Value
*vtxOutput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVout
});
242 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
243 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
244 if (!outputs
[attrib
][channel
])
247 Value
*val
= LOAD(unwrap(outputs
[attrib
][channel
]));
249 uint32_t outSlot
= attrib
;
250 if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
)
251 outSlot
= VERTEX_POINT_SIZE_SLOT
;
252 STORE(val
, vtxOutput
, {0, 0, outSlot
, channel
});
256 if (ctx
->rasterizer
->clip_plane_enable
||
257 swr_vs
->info
.base
.culldist_writemask
) {
258 unsigned clip_mask
= ctx
->rasterizer
->clip_plane_enable
;
261 if (swr_vs
->info
.base
.writes_clipvertex
) {
262 cv
= 1 + locate_linkage(TGSI_SEMANTIC_CLIPVERTEX
, 0,
265 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
266 if (swr_vs
->info
.base
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
267 swr_vs
->info
.base
.output_semantic_index
[i
] == 0) {
273 LLVMValueRef cx
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][0], "");
274 LLVMValueRef cy
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][1], "");
275 LLVMValueRef cz
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][2], "");
276 LLVMValueRef cw
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][3], "");
278 for (unsigned val
= 0; val
< PIPE_MAX_CLIP_PLANES
; val
++) {
279 // clip distance overrides user clip planes
280 if ((swr_vs
->info
.base
.clipdist_writemask
& clip_mask
& (1 << val
)) ||
281 ((swr_vs
->info
.base
.culldist_writemask
<< swr_vs
->info
.base
.num_written_clipdistance
) & (1 << val
))) {
282 unsigned cv
= 1 + locate_linkage(TGSI_SEMANTIC_CLIPDIST
, val
< 4 ? 0 : 1,
285 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
], "");
286 STORE(unwrap(dist
), vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
});
288 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
- 4], "");
289 STORE(unwrap(dist
), vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4});
294 if (!(clip_mask
& (1 << val
)))
297 Value
*px
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 0}));
298 Value
*py
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 1}));
299 Value
*pz
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 2}));
300 Value
*pw
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 3}));
301 Value
*dist
= FADD(FMUL(unwrap(cx
), VBROADCAST(px
)),
302 FADD(FMUL(unwrap(cy
), VBROADCAST(py
)),
303 FADD(FMUL(unwrap(cz
), VBROADCAST(pz
)),
304 FMUL(unwrap(cw
), VBROADCAST(pw
)))));
307 STORE(dist
, vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
});
309 STORE(dist
, vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4});
315 gallivm_verify_function(gallivm
, wrap(pFunction
));
316 gallivm_compile_module(gallivm
);
318 // lp_debug_dump_value(func);
320 PFN_VERTEX_FUNC pFunc
=
321 (PFN_VERTEX_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
323 debug_printf("vert shader %p\n", pFunc
);
324 assert(pFunc
&& "Error: VertShader = NULL");
326 #if (LLVM_VERSION_MAJOR == 3) && (LLVM_VERSION_MINOR >= 5)
327 JM()->mIsModuleFinalized
= true;
334 swr_compile_vs(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
337 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
339 PFN_VERTEX_FUNC func
= builder
.CompileVS(ctx
, key
);
341 ctx
->vs
->map
.insert(std::make_pair(key
, make_unique
<VariantVS
>(builder
.gallivm
, func
)));
346 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
)
348 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
349 if ((info
->output_semantic_name
[i
] == name
)
350 && (info
->output_semantic_index
[i
] == index
)) {
351 return i
- 1; // position is not part of the linkage
355 if (name
== TGSI_SEMANTIC_COLOR
) { // BCOLOR fallback
356 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
357 if ((info
->output_semantic_name
[i
] == TGSI_SEMANTIC_BCOLOR
)
358 && (info
->output_semantic_index
[i
] == index
)) {
359 return i
- 1; // position is not part of the linkage
368 BuilderSWR::CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
370 struct swr_fragment_shader
*swr_fs
= ctx
->fs
;
372 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
373 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
375 memset(inputs
, 0, sizeof(inputs
));
376 memset(outputs
, 0, sizeof(outputs
));
378 struct lp_build_sampler_soa
*sampler
= NULL
;
380 AttrBuilder attrBuilder
;
381 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
382 AttributeSet attrSet
= AttributeSet::get(
383 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
385 std::vector
<Type
*> fsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
386 PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};
387 FunctionType
*funcType
=
388 FunctionType::get(Type::getVoidTy(JM()->mContext
), fsArgs
, false);
390 auto pFunction
= Function::Create(funcType
,
391 GlobalValue::ExternalLinkage
,
393 JM()->mpCurrentModule
);
394 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
396 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
397 IRB()->SetInsertPoint(block
);
398 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
400 auto args
= pFunction
->arg_begin();
401 Value
*hPrivateData
= &*args
++;
402 hPrivateData
->setName("hPrivateData");
403 Value
*pPS
= &*args
++;
404 pPS
->setName("psCtx");
406 Value
*consts_ptr
= GEP(hPrivateData
, {0, swr_draw_context_constantFS
});
407 consts_ptr
->setName("fs_constants");
408 Value
*const_sizes_ptr
=
409 GEP(hPrivateData
, {0, swr_draw_context_num_constantsFS
});
410 const_sizes_ptr
->setName("num_fs_constants");
412 // load *pAttribs, *pPerspAttribs
413 Value
*pRawAttribs
= LOAD(pPS
, {0, SWR_PS_CONTEXT_pAttribs
}, "pRawAttribs");
414 Value
*pPerspAttribs
=
415 LOAD(pPS
, {0, SWR_PS_CONTEXT_pPerspAttribs
}, "pPerspAttribs");
417 swr_fs
->constantMask
= 0;
418 swr_fs
->flatConstantMask
= 0;
419 swr_fs
->pointSpriteMask
= 0;
421 for (int attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
422 const unsigned mask
= swr_fs
->info
.base
.input_usage_mask
[attrib
];
423 const unsigned interpMode
= swr_fs
->info
.base
.input_interpolate
[attrib
];
424 const unsigned interpLoc
= swr_fs
->info
.base
.input_interpolate_loc
[attrib
];
430 Value
*vi
= nullptr, *vj
= nullptr;
432 case TGSI_INTERPOLATE_LOC_CENTER
:
433 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_center
}, "i");
434 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_center
}, "j");
436 case TGSI_INTERPOLATE_LOC_CENTROID
:
437 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_centroid
}, "i");
438 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_centroid
}, "j");
440 case TGSI_INTERPOLATE_LOC_SAMPLE
:
441 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_sample
}, "i");
442 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_sample
}, "j");
447 Value
*vw
= nullptr, *pAttribs
;
448 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
) {
449 pAttribs
= pPerspAttribs
;
451 case TGSI_INTERPOLATE_LOC_CENTER
:
452 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}));
454 case TGSI_INTERPOLATE_LOC_CENTROID
:
455 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_centroid
}));
457 case TGSI_INTERPOLATE_LOC_SAMPLE
:
458 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_sample
}));
462 pAttribs
= pRawAttribs
;
468 ubyte semantic_name
= swr_fs
->info
.base
.input_semantic_name
[attrib
];
469 ubyte semantic_idx
= swr_fs
->info
.base
.input_semantic_index
[attrib
];
471 if (semantic_name
== TGSI_SEMANTIC_FACE
) {
473 UI_TO_FP(LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), mFP32Ty
);
474 ff
= FSUB(FMUL(ff
, C(2.0f
)), C(1.0f
));
475 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vFrontFace");
477 inputs
[attrib
][0] = wrap(ff
);
478 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
479 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
480 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
482 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
) { // gl_FragCoord
483 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_center
}, "vX"));
484 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_center
}, "vY"));
485 inputs
[attrib
][2] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vZ
}, "vZ"));
487 wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}, "vOneOverW"));
489 } else if (semantic_name
== TGSI_SEMANTIC_PRIMID
) {
490 Value
*primID
= LOAD(pPS
, {0, SWR_PS_CONTEXT_primID
}, "primID");
491 inputs
[attrib
][0] = wrap(VECTOR_SPLAT(JM()->mVWidth
, primID
));
492 inputs
[attrib
][1] = wrap(VIMMED1(0));
493 inputs
[attrib
][2] = wrap(VIMMED1(0));
494 inputs
[attrib
][3] = wrap(VIMMED1(0));
498 unsigned linkedAttrib
=
499 locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
);
500 if (linkedAttrib
== 0xFFFFFFFF) {
501 // not found - check for point sprite
502 if (ctx
->rasterizer
->sprite_coord_enable
) {
503 linkedAttrib
= ctx
->vs
->info
.base
.num_outputs
- 1;
504 swr_fs
->pointSpriteMask
|= (1 << linkedAttrib
);
508 tgsi_semantic_names
[semantic_name
],
510 assert(0 && "attribute linkage not found");
514 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
515 swr_fs
->constantMask
|= 1 << linkedAttrib
;
516 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
517 swr_fs
->flatConstantMask
|= 1 << linkedAttrib
;
520 for (int channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
521 if (mask
& (1 << channel
)) {
522 Value
*indexA
= C(linkedAttrib
* 12 + channel
);
523 Value
*indexB
= C(linkedAttrib
* 12 + channel
+ 4);
524 Value
*indexC
= C(linkedAttrib
* 12 + channel
+ 8);
526 if ((semantic_name
== TGSI_SEMANTIC_COLOR
)
527 && ctx
->rasterizer
->light_twoside
) {
528 unsigned bcolorAttrib
= locate_linkage(
529 TGSI_SEMANTIC_BCOLOR
, semantic_idx
, &ctx
->vs
->info
.base
);
531 unsigned diff
= 12 * (bcolorAttrib
- linkedAttrib
);
534 XOR(C(1), LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), "backFace");
536 Value
*offset
= MUL(back
, C(diff
));
537 offset
->setName("offset");
539 indexA
= ADD(indexA
, offset
);
540 indexB
= ADD(indexB
, offset
);
541 indexC
= ADD(indexC
, offset
);
543 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
544 swr_fs
->constantMask
|= 1 << bcolorAttrib
;
545 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
546 swr_fs
->flatConstantMask
|= 1 << bcolorAttrib
;
550 Value
*va
= VBROADCAST(LOAD(GEP(pAttribs
, indexA
)));
551 Value
*vb
= VBROADCAST(LOAD(GEP(pAttribs
, indexB
)));
552 Value
*vc
= VBROADCAST(LOAD(GEP(pAttribs
, indexC
)));
554 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
555 inputs
[attrib
][channel
] = wrap(va
);
557 Value
*vk
= FSUB(FSUB(VIMMED1(1.0f
), vi
), vj
);
561 Value
*interp
= FMUL(va
, vi
);
562 Value
*interp1
= FMUL(vb
, vj
);
563 interp
= FADD(interp
, interp1
);
564 interp
= FADD(interp
, vc
);
565 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
)
566 interp
= FMUL(interp
, vw
);
567 inputs
[attrib
][channel
] = wrap(interp
);
573 sampler
= swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_FRAGMENT
);
575 struct lp_bld_tgsi_system_values system_values
;
576 memset(&system_values
, 0, sizeof(system_values
));
578 struct lp_build_mask_context mask
;
580 if (swr_fs
->info
.base
.uses_kill
) {
581 Value
*mask_val
= LOAD(pPS
, {0, SWR_PS_CONTEXT_activeMask
}, "activeMask");
583 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
586 lp_build_tgsi_soa(gallivm
,
588 lp_type_float_vec(32, 32 * 8),
589 swr_fs
->info
.base
.uses_kill
? &mask
: NULL
, // mask
591 wrap(const_sizes_ptr
),
599 NULL
); // geometry shader face
601 sampler
->destroy(sampler
);
603 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
605 for (uint32_t attrib
= 0; attrib
< swr_fs
->info
.base
.num_outputs
;
607 switch (swr_fs
->info
.base
.output_semantic_name
[attrib
]) {
608 case TGSI_SEMANTIC_POSITION
: {
611 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][2], "");
612 STORE(unwrap(outZ
), pPS
, {0, SWR_PS_CONTEXT_vZ
});
615 case TGSI_SEMANTIC_COLOR
: {
616 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
617 if (!outputs
[attrib
][channel
])
621 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][channel
], "");
622 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
]) {
623 for (uint32_t rt
= 0; rt
< key
.nr_cbufs
; rt
++) {
626 {0, SWR_PS_CONTEXT_shaded
, rt
, channel
});
632 SWR_PS_CONTEXT_shaded
,
633 swr_fs
->info
.base
.output_semantic_index
[attrib
],
641 "unknown output from FS %s[%d]\n",
642 tgsi_semantic_names
[swr_fs
->info
.base
643 .output_semantic_name
[attrib
]],
644 swr_fs
->info
.base
.output_semantic_index
[attrib
]);
650 LLVMValueRef mask_result
= 0;
651 if (swr_fs
->info
.base
.uses_kill
) {
652 mask_result
= lp_build_mask_end(&mask
);
655 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
657 if (swr_fs
->info
.base
.uses_kill
) {
658 STORE(unwrap(mask_result
), pPS
, {0, SWR_PS_CONTEXT_activeMask
});
663 gallivm_verify_function(gallivm
, wrap(pFunction
));
665 gallivm_compile_module(gallivm
);
667 PFN_PIXEL_KERNEL kernel
=
668 (PFN_PIXEL_KERNEL
)gallivm_jit_function(gallivm
, wrap(pFunction
));
669 debug_printf("frag shader %p\n", kernel
);
670 assert(kernel
&& "Error: FragShader = NULL");
672 #if (LLVM_VERSION_MAJOR == 3) && (LLVM_VERSION_MINOR >= 5)
673 JM()->mIsModuleFinalized
= true;
680 swr_compile_fs(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
683 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
685 PFN_PIXEL_KERNEL func
= builder
.CompileFS(ctx
, key
);
687 ctx
->fs
->map
.insert(std::make_pair(key
, make_unique
<VariantFS
>(builder
.gallivm
, func
)));