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 "util/u_format.h"
38 #include "gallivm/lp_bld_init.h"
39 #include "gallivm/lp_bld_flow.h"
40 #include "gallivm/lp_bld_struct.h"
41 #include "gallivm/lp_bld_tgsi.h"
43 #include "swr_context.h"
44 #include "swr_context_llvm.h"
45 #include "swr_resource.h"
46 #include "swr_state.h"
47 #include "swr_screen.h"
49 using namespace SwrJit
;
52 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
);
54 bool operator==(const swr_jit_fs_key
&lhs
, const swr_jit_fs_key
&rhs
)
56 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
59 bool operator==(const swr_jit_vs_key
&lhs
, const swr_jit_vs_key
&rhs
)
61 return !memcmp(&lhs
, &rhs
, sizeof(lhs
));
65 swr_generate_sampler_key(const struct lp_tgsi_info
&info
,
66 struct swr_context
*ctx
,
68 struct swr_jit_sampler_key
&key
)
70 key
.nr_samplers
= info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
72 for (unsigned i
= 0; i
< key
.nr_samplers
; i
++) {
73 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
74 lp_sampler_static_sampler_state(
75 &key
.sampler
[i
].sampler_state
,
76 ctx
->samplers
[shader_type
][i
]);
81 * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes
82 * are dx10-style? Can't really have mixed opcodes, at least not
83 * if we want to skip the holes here (without rescanning tgsi).
85 if (info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
86 key
.nr_sampler_views
=
87 info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
88 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
89 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER_VIEW
] & (1 << i
)) {
90 const struct pipe_sampler_view
*view
=
91 ctx
->sampler_views
[shader_type
][i
];
92 lp_sampler_static_texture_state(
93 &key
.sampler
[i
].texture_state
, view
);
95 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
96 const struct util_format_description
*desc
=
97 util_format_description(view
->format
);
98 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
99 !util_format_has_depth(desc
))
100 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
105 key
.nr_sampler_views
= key
.nr_samplers
;
106 for (unsigned i
= 0; i
< key
.nr_sampler_views
; i
++) {
107 if (info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
108 const struct pipe_sampler_view
*view
=
109 ctx
->sampler_views
[shader_type
][i
];
110 lp_sampler_static_texture_state(
111 &key
.sampler
[i
].texture_state
, view
);
113 struct swr_resource
*swr_res
= swr_resource(view
->texture
);
114 const struct util_format_description
*desc
=
115 util_format_description(view
->format
);
116 if (swr_res
->has_depth
&& swr_res
->has_stencil
&&
117 !util_format_has_depth(desc
))
118 key
.sampler
[i
].texture_state
.format
= PIPE_FORMAT_S8_UINT
;
126 swr_generate_fs_key(struct swr_jit_fs_key
&key
,
127 struct swr_context
*ctx
,
128 swr_fragment_shader
*swr_fs
)
130 memset(&key
, 0, sizeof(key
));
132 key
.nr_cbufs
= ctx
->framebuffer
.nr_cbufs
;
133 key
.light_twoside
= ctx
->rasterizer
->light_twoside
;
134 memcpy(&key
.vs_output_semantic_name
,
135 &ctx
->vs
->info
.base
.output_semantic_name
,
136 sizeof(key
.vs_output_semantic_name
));
137 memcpy(&key
.vs_output_semantic_idx
,
138 &ctx
->vs
->info
.base
.output_semantic_index
,
139 sizeof(key
.vs_output_semantic_idx
));
141 swr_generate_sampler_key(swr_fs
->info
, ctx
, PIPE_SHADER_FRAGMENT
, key
);
145 swr_generate_vs_key(struct swr_jit_vs_key
&key
,
146 struct swr_context
*ctx
,
147 swr_vertex_shader
*swr_vs
)
149 memset(&key
, 0, sizeof(key
));
151 key
.clip_plane_mask
=
152 swr_vs
->info
.base
.clipdist_writemask
?
153 swr_vs
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
154 ctx
->rasterizer
->clip_plane_enable
;
156 swr_generate_sampler_key(swr_vs
->info
, ctx
, PIPE_SHADER_VERTEX
, key
);
159 struct BuilderSWR
: public Builder
{
160 BuilderSWR(JitManager
*pJitMgr
, const char *pName
)
163 pJitMgr
->SetupNewModule();
164 gallivm
= gallivm_create(pName
, wrap(&JM()->mContext
));
165 pJitMgr
->mpCurrentModule
= unwrap(gallivm
->module
);
169 gallivm_free_ir(gallivm
);
172 struct gallivm_state
*gallivm
;
173 PFN_VERTEX_FUNC
CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
);
174 PFN_PIXEL_KERNEL
CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
);
178 BuilderSWR::CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
180 struct swr_vertex_shader
*swr_vs
= ctx
->vs
;
182 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
183 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
185 memset(outputs
, 0, sizeof(outputs
));
187 AttrBuilder attrBuilder
;
188 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
189 AttributeSet attrSet
= AttributeSet::get(
190 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
192 std::vector
<Type
*> vsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
193 PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};
194 FunctionType
*vsFuncType
=
195 FunctionType::get(Type::getVoidTy(JM()->mContext
), vsArgs
, false);
197 // create new vertex shader function
198 auto pFunction
= Function::Create(vsFuncType
,
199 GlobalValue::ExternalLinkage
,
201 JM()->mpCurrentModule
);
202 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
204 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
205 IRB()->SetInsertPoint(block
);
206 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
208 auto argitr
= pFunction
->arg_begin();
209 Value
*hPrivateData
= &*argitr
++;
210 hPrivateData
->setName("hPrivateData");
211 Value
*pVsCtx
= &*argitr
++;
212 pVsCtx
->setName("vsCtx");
214 Value
*consts_ptr
= GEP(hPrivateData
, {C(0), C(swr_draw_context_constantVS
)});
216 consts_ptr
->setName("vs_constants");
217 Value
*const_sizes_ptr
=
218 GEP(hPrivateData
, {0, swr_draw_context_num_constantsVS
});
219 const_sizes_ptr
->setName("num_vs_constants");
221 Value
*vtxInput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVin
});
223 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
224 const unsigned mask
= swr_vs
->info
.base
.input_usage_mask
[attrib
];
225 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
226 if (mask
& (1 << channel
)) {
227 inputs
[attrib
][channel
] =
228 wrap(LOAD(vtxInput
, {0, 0, attrib
, channel
}));
233 struct lp_build_sampler_soa
*sampler
=
234 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_VERTEX
);
236 struct lp_bld_tgsi_system_values system_values
;
237 memset(&system_values
, 0, sizeof(system_values
));
238 system_values
.instance_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_InstanceID
}));
239 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID
}));
241 lp_build_tgsi_soa(gallivm
,
243 lp_type_float_vec(32, 32 * 8),
246 wrap(const_sizes_ptr
),
250 wrap(hPrivateData
), // (sampler context)
254 NULL
); // geometry shader face
256 sampler
->destroy(sampler
);
258 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
260 Value
*vtxOutput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVout
});
262 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
263 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
264 if (!outputs
[attrib
][channel
])
267 Value
*val
= LOAD(unwrap(outputs
[attrib
][channel
]));
269 uint32_t outSlot
= attrib
;
270 if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
)
271 outSlot
= VERTEX_POINT_SIZE_SLOT
;
272 STORE(val
, vtxOutput
, {0, 0, outSlot
, channel
});
276 if (ctx
->rasterizer
->clip_plane_enable
||
277 swr_vs
->info
.base
.culldist_writemask
) {
278 unsigned clip_mask
= ctx
->rasterizer
->clip_plane_enable
;
281 if (swr_vs
->info
.base
.writes_clipvertex
) {
282 cv
= 1 + locate_linkage(TGSI_SEMANTIC_CLIPVERTEX
, 0,
285 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
286 if (swr_vs
->info
.base
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
287 swr_vs
->info
.base
.output_semantic_index
[i
] == 0) {
293 LLVMValueRef cx
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][0], "");
294 LLVMValueRef cy
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][1], "");
295 LLVMValueRef cz
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][2], "");
296 LLVMValueRef cw
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][3], "");
298 for (unsigned val
= 0; val
< PIPE_MAX_CLIP_PLANES
; val
++) {
299 // clip distance overrides user clip planes
300 if ((swr_vs
->info
.base
.clipdist_writemask
& clip_mask
& (1 << val
)) ||
301 ((swr_vs
->info
.base
.culldist_writemask
<< swr_vs
->info
.base
.num_written_clipdistance
) & (1 << val
))) {
302 unsigned cv
= 1 + locate_linkage(TGSI_SEMANTIC_CLIPDIST
, val
< 4 ? 0 : 1,
305 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
], "");
306 STORE(unwrap(dist
), vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
});
308 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
- 4], "");
309 STORE(unwrap(dist
), vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4});
314 if (!(clip_mask
& (1 << val
)))
317 Value
*px
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 0}));
318 Value
*py
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 1}));
319 Value
*pz
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 2}));
320 Value
*pw
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 3}));
321 Value
*dist
= FADD(FMUL(unwrap(cx
), VBROADCAST(px
)),
322 FADD(FMUL(unwrap(cy
), VBROADCAST(py
)),
323 FADD(FMUL(unwrap(cz
), VBROADCAST(pz
)),
324 FMUL(unwrap(cw
), VBROADCAST(pw
)))));
327 STORE(dist
, vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
});
329 STORE(dist
, vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4});
335 gallivm_verify_function(gallivm
, wrap(pFunction
));
336 gallivm_compile_module(gallivm
);
338 // lp_debug_dump_value(func);
340 PFN_VERTEX_FUNC pFunc
=
341 (PFN_VERTEX_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
343 debug_printf("vert shader %p\n", pFunc
);
344 assert(pFunc
&& "Error: VertShader = NULL");
346 #if (LLVM_VERSION_MAJOR == 3) && (LLVM_VERSION_MINOR >= 5)
347 JM()->mIsModuleFinalized
= true;
354 swr_compile_vs(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
357 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
359 PFN_VERTEX_FUNC func
= builder
.CompileVS(ctx
, key
);
361 ctx
->vs
->map
.insert(std::make_pair(key
, make_unique
<VariantVS
>(builder
.gallivm
, func
)));
366 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
)
368 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
369 if ((info
->output_semantic_name
[i
] == name
)
370 && (info
->output_semantic_index
[i
] == index
)) {
371 return i
- 1; // position is not part of the linkage
379 BuilderSWR::CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
381 struct swr_fragment_shader
*swr_fs
= ctx
->fs
;
383 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
384 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
386 memset(inputs
, 0, sizeof(inputs
));
387 memset(outputs
, 0, sizeof(outputs
));
389 struct lp_build_sampler_soa
*sampler
= NULL
;
391 AttrBuilder attrBuilder
;
392 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
393 AttributeSet attrSet
= AttributeSet::get(
394 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
396 std::vector
<Type
*> fsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
397 PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};
398 FunctionType
*funcType
=
399 FunctionType::get(Type::getVoidTy(JM()->mContext
), fsArgs
, false);
401 auto pFunction
= Function::Create(funcType
,
402 GlobalValue::ExternalLinkage
,
404 JM()->mpCurrentModule
);
405 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
407 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
408 IRB()->SetInsertPoint(block
);
409 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
411 auto args
= pFunction
->arg_begin();
412 Value
*hPrivateData
= &*args
++;
413 hPrivateData
->setName("hPrivateData");
414 Value
*pPS
= &*args
++;
415 pPS
->setName("psCtx");
417 Value
*consts_ptr
= GEP(hPrivateData
, {0, swr_draw_context_constantFS
});
418 consts_ptr
->setName("fs_constants");
419 Value
*const_sizes_ptr
=
420 GEP(hPrivateData
, {0, swr_draw_context_num_constantsFS
});
421 const_sizes_ptr
->setName("num_fs_constants");
423 // load *pAttribs, *pPerspAttribs
424 Value
*pRawAttribs
= LOAD(pPS
, {0, SWR_PS_CONTEXT_pAttribs
}, "pRawAttribs");
425 Value
*pPerspAttribs
=
426 LOAD(pPS
, {0, SWR_PS_CONTEXT_pPerspAttribs
}, "pPerspAttribs");
428 swr_fs
->constantMask
= 0;
429 swr_fs
->flatConstantMask
= 0;
430 swr_fs
->pointSpriteMask
= 0;
432 for (int attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
433 const unsigned mask
= swr_fs
->info
.base
.input_usage_mask
[attrib
];
434 const unsigned interpMode
= swr_fs
->info
.base
.input_interpolate
[attrib
];
435 const unsigned interpLoc
= swr_fs
->info
.base
.input_interpolate_loc
[attrib
];
441 Value
*vi
= nullptr, *vj
= nullptr;
443 case TGSI_INTERPOLATE_LOC_CENTER
:
444 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_center
}, "i");
445 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_center
}, "j");
447 case TGSI_INTERPOLATE_LOC_CENTROID
:
448 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_centroid
}, "i");
449 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_centroid
}, "j");
451 case TGSI_INTERPOLATE_LOC_SAMPLE
:
452 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_sample
}, "i");
453 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_sample
}, "j");
458 Value
*vw
= nullptr, *pAttribs
;
459 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
) {
460 pAttribs
= pPerspAttribs
;
462 case TGSI_INTERPOLATE_LOC_CENTER
:
463 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}));
465 case TGSI_INTERPOLATE_LOC_CENTROID
:
466 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_centroid
}));
468 case TGSI_INTERPOLATE_LOC_SAMPLE
:
469 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_sample
}));
473 pAttribs
= pRawAttribs
;
479 ubyte semantic_name
= swr_fs
->info
.base
.input_semantic_name
[attrib
];
480 ubyte semantic_idx
= swr_fs
->info
.base
.input_semantic_index
[attrib
];
482 if (semantic_name
== TGSI_SEMANTIC_FACE
) {
484 UI_TO_FP(LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), mFP32Ty
);
485 ff
= FSUB(FMUL(ff
, C(2.0f
)), C(1.0f
));
486 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vFrontFace");
488 inputs
[attrib
][0] = wrap(ff
);
489 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
490 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
491 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
493 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
) { // gl_FragCoord
494 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
495 TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
) {
496 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_center
}, "vX"));
497 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_center
}, "vY"));
499 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
}, "vX"));
500 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
}, "vY"));
502 inputs
[attrib
][2] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vZ
}, "vZ"));
504 wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}, "vOneOverW"));
506 } else if (semantic_name
== TGSI_SEMANTIC_PRIMID
) {
507 Value
*primID
= LOAD(pPS
, {0, SWR_PS_CONTEXT_primID
}, "primID");
508 inputs
[attrib
][0] = wrap(VECTOR_SPLAT(JM()->mVWidth
, primID
));
509 inputs
[attrib
][1] = wrap(VIMMED1(0));
510 inputs
[attrib
][2] = wrap(VIMMED1(0));
511 inputs
[attrib
][3] = wrap(VIMMED1(0));
515 unsigned linkedAttrib
=
516 locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
);
517 if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
518 ctx
->rasterizer
->sprite_coord_enable
& (1 << semantic_idx
)) {
519 /* we add an extra attrib to the backendState in swr_update_derived. */
520 linkedAttrib
= ctx
->vs
->info
.base
.num_outputs
- 1;
521 swr_fs
->pointSpriteMask
|= (1 << linkedAttrib
);
522 } else if (linkedAttrib
== 0xFFFFFFFF) {
523 inputs
[attrib
][0] = wrap(VIMMED1(0.0f
));
524 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
525 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
526 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
527 /* If we're reading in color and 2-sided lighting is enabled, we have
530 if (semantic_name
!= TGSI_SEMANTIC_COLOR
|| !key
.light_twoside
)
533 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
534 swr_fs
->constantMask
|= 1 << linkedAttrib
;
535 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
536 swr_fs
->flatConstantMask
|= 1 << linkedAttrib
;
540 unsigned bcolorAttrib
= 0xFFFFFFFF;
541 Value
*offset
= NULL
;
542 if (semantic_name
== TGSI_SEMANTIC_COLOR
&& key
.light_twoside
) {
543 bcolorAttrib
= locate_linkage(
544 TGSI_SEMANTIC_BCOLOR
, semantic_idx
, &ctx
->vs
->info
.base
);
545 /* Neither front nor back colors were available. Nothing to load. */
546 if (bcolorAttrib
== 0xFFFFFFFF && linkedAttrib
== 0xFFFFFFFF)
548 /* If there is no front color, just always use the back color. */
549 if (linkedAttrib
== 0xFFFFFFFF)
550 linkedAttrib
= bcolorAttrib
;
552 if (bcolorAttrib
!= 0xFFFFFFFF) {
553 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
554 swr_fs
->constantMask
|= 1 << bcolorAttrib
;
555 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
556 swr_fs
->flatConstantMask
|= 1 << bcolorAttrib
;
559 unsigned diff
= 12 * (bcolorAttrib
- linkedAttrib
);
563 XOR(C(1), LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), "backFace");
565 offset
= MUL(back
, C(diff
));
566 offset
->setName("offset");
571 for (int channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
572 if (mask
& (1 << channel
)) {
573 Value
*indexA
= C(linkedAttrib
* 12 + channel
);
574 Value
*indexB
= C(linkedAttrib
* 12 + channel
+ 4);
575 Value
*indexC
= C(linkedAttrib
* 12 + channel
+ 8);
578 indexA
= ADD(indexA
, offset
);
579 indexB
= ADD(indexB
, offset
);
580 indexC
= ADD(indexC
, offset
);
583 Value
*va
= VBROADCAST(LOAD(GEP(pAttribs
, indexA
)));
584 Value
*vb
= VBROADCAST(LOAD(GEP(pAttribs
, indexB
)));
585 Value
*vc
= VBROADCAST(LOAD(GEP(pAttribs
, indexC
)));
587 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
588 inputs
[attrib
][channel
] = wrap(va
);
590 Value
*vk
= FSUB(FSUB(VIMMED1(1.0f
), vi
), vj
);
594 Value
*interp
= FMUL(va
, vi
);
595 Value
*interp1
= FMUL(vb
, vj
);
596 interp
= FADD(interp
, interp1
);
597 interp
= FADD(interp
, vc
);
598 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
)
599 interp
= FMUL(interp
, vw
);
600 inputs
[attrib
][channel
] = wrap(interp
);
606 sampler
= swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_FRAGMENT
);
608 struct lp_bld_tgsi_system_values system_values
;
609 memset(&system_values
, 0, sizeof(system_values
));
611 struct lp_build_mask_context mask
;
613 if (swr_fs
->info
.base
.uses_kill
) {
614 Value
*mask_val
= LOAD(pPS
, {0, SWR_PS_CONTEXT_activeMask
}, "activeMask");
616 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
619 lp_build_tgsi_soa(gallivm
,
621 lp_type_float_vec(32, 32 * 8),
622 swr_fs
->info
.base
.uses_kill
? &mask
: NULL
, // mask
624 wrap(const_sizes_ptr
),
632 NULL
); // geometry shader face
634 sampler
->destroy(sampler
);
636 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
638 for (uint32_t attrib
= 0; attrib
< swr_fs
->info
.base
.num_outputs
;
640 switch (swr_fs
->info
.base
.output_semantic_name
[attrib
]) {
641 case TGSI_SEMANTIC_POSITION
: {
644 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][2], "");
645 STORE(unwrap(outZ
), pPS
, {0, SWR_PS_CONTEXT_vZ
});
648 case TGSI_SEMANTIC_COLOR
: {
649 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
650 if (!outputs
[attrib
][channel
])
654 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][channel
], "");
655 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
656 swr_fs
->info
.base
.output_semantic_index
[attrib
] == 0) {
657 for (uint32_t rt
= 0; rt
< key
.nr_cbufs
; rt
++) {
660 {0, SWR_PS_CONTEXT_shaded
, rt
, channel
});
666 SWR_PS_CONTEXT_shaded
,
667 swr_fs
->info
.base
.output_semantic_index
[attrib
],
675 "unknown output from FS %s[%d]\n",
676 tgsi_semantic_names
[swr_fs
->info
.base
677 .output_semantic_name
[attrib
]],
678 swr_fs
->info
.base
.output_semantic_index
[attrib
]);
684 LLVMValueRef mask_result
= 0;
685 if (swr_fs
->info
.base
.uses_kill
) {
686 mask_result
= lp_build_mask_end(&mask
);
689 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
691 if (swr_fs
->info
.base
.uses_kill
) {
692 STORE(unwrap(mask_result
), pPS
, {0, SWR_PS_CONTEXT_activeMask
});
697 gallivm_verify_function(gallivm
, wrap(pFunction
));
699 gallivm_compile_module(gallivm
);
701 PFN_PIXEL_KERNEL kernel
=
702 (PFN_PIXEL_KERNEL
)gallivm_jit_function(gallivm
, wrap(pFunction
));
703 debug_printf("frag shader %p\n", kernel
);
704 assert(kernel
&& "Error: FragShader = NULL");
706 #if (LLVM_VERSION_MAJOR == 3) && (LLVM_VERSION_MINOR >= 5)
707 JM()->mIsModuleFinalized
= true;
714 swr_compile_fs(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
717 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
719 PFN_PIXEL_KERNEL func
= builder
.CompileFS(ctx
, key
);
721 ctx
->fs
->map
.insert(std::make_pair(key
, make_unique
<VariantFS
>(builder
.gallivm
, func
)));