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 key
.sprite_coord_enable
= ctx
->rasterizer
->sprite_coord_enable
;
135 memcpy(&key
.vs_output_semantic_name
,
136 &ctx
->vs
->info
.base
.output_semantic_name
,
137 sizeof(key
.vs_output_semantic_name
));
138 memcpy(&key
.vs_output_semantic_idx
,
139 &ctx
->vs
->info
.base
.output_semantic_index
,
140 sizeof(key
.vs_output_semantic_idx
));
142 swr_generate_sampler_key(swr_fs
->info
, ctx
, PIPE_SHADER_FRAGMENT
, key
);
146 swr_generate_vs_key(struct swr_jit_vs_key
&key
,
147 struct swr_context
*ctx
,
148 swr_vertex_shader
*swr_vs
)
150 memset(&key
, 0, sizeof(key
));
152 key
.clip_plane_mask
=
153 swr_vs
->info
.base
.clipdist_writemask
?
154 swr_vs
->info
.base
.clipdist_writemask
& ctx
->rasterizer
->clip_plane_enable
:
155 ctx
->rasterizer
->clip_plane_enable
;
157 swr_generate_sampler_key(swr_vs
->info
, ctx
, PIPE_SHADER_VERTEX
, key
);
160 struct BuilderSWR
: public Builder
{
161 BuilderSWR(JitManager
*pJitMgr
, const char *pName
)
164 pJitMgr
->SetupNewModule();
165 gallivm
= gallivm_create(pName
, wrap(&JM()->mContext
));
166 pJitMgr
->mpCurrentModule
= unwrap(gallivm
->module
);
170 gallivm_free_ir(gallivm
);
173 struct gallivm_state
*gallivm
;
174 PFN_VERTEX_FUNC
CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
);
175 PFN_PIXEL_KERNEL
CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
);
179 BuilderSWR::CompileVS(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
181 struct swr_vertex_shader
*swr_vs
= ctx
->vs
;
183 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
184 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
186 memset(outputs
, 0, sizeof(outputs
));
188 AttrBuilder attrBuilder
;
189 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
190 AttributeSet attrSet
= AttributeSet::get(
191 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
193 std::vector
<Type
*> vsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
194 PointerType::get(Gen_SWR_VS_CONTEXT(JM()), 0)};
195 FunctionType
*vsFuncType
=
196 FunctionType::get(Type::getVoidTy(JM()->mContext
), vsArgs
, false);
198 // create new vertex shader function
199 auto pFunction
= Function::Create(vsFuncType
,
200 GlobalValue::ExternalLinkage
,
202 JM()->mpCurrentModule
);
203 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
205 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
206 IRB()->SetInsertPoint(block
);
207 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
209 auto argitr
= pFunction
->arg_begin();
210 Value
*hPrivateData
= &*argitr
++;
211 hPrivateData
->setName("hPrivateData");
212 Value
*pVsCtx
= &*argitr
++;
213 pVsCtx
->setName("vsCtx");
215 Value
*consts_ptr
= GEP(hPrivateData
, {C(0), C(swr_draw_context_constantVS
)});
217 consts_ptr
->setName("vs_constants");
218 Value
*const_sizes_ptr
=
219 GEP(hPrivateData
, {0, swr_draw_context_num_constantsVS
});
220 const_sizes_ptr
->setName("num_vs_constants");
222 Value
*vtxInput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVin
});
224 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
225 const unsigned mask
= swr_vs
->info
.base
.input_usage_mask
[attrib
];
226 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
227 if (mask
& (1 << channel
)) {
228 inputs
[attrib
][channel
] =
229 wrap(LOAD(vtxInput
, {0, 0, attrib
, channel
}));
234 struct lp_build_sampler_soa
*sampler
=
235 swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_VERTEX
);
237 struct lp_bld_tgsi_system_values system_values
;
238 memset(&system_values
, 0, sizeof(system_values
));
239 system_values
.instance_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_InstanceID
}));
240 system_values
.vertex_id
= wrap(LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_VertexID
}));
242 lp_build_tgsi_soa(gallivm
,
244 lp_type_float_vec(32, 32 * 8),
247 wrap(const_sizes_ptr
),
251 wrap(hPrivateData
), // (sampler context)
255 NULL
); // geometry shader face
257 sampler
->destroy(sampler
);
259 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
261 Value
*vtxOutput
= LOAD(pVsCtx
, {0, SWR_VS_CONTEXT_pVout
});
263 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
264 for (uint32_t attrib
= 0; attrib
< PIPE_MAX_SHADER_OUTPUTS
; attrib
++) {
265 if (!outputs
[attrib
][channel
])
268 Value
*val
= LOAD(unwrap(outputs
[attrib
][channel
]));
270 uint32_t outSlot
= attrib
;
271 if (swr_vs
->info
.base
.output_semantic_name
[attrib
] == TGSI_SEMANTIC_PSIZE
)
272 outSlot
= VERTEX_POINT_SIZE_SLOT
;
273 STORE(val
, vtxOutput
, {0, 0, outSlot
, channel
});
277 if (ctx
->rasterizer
->clip_plane_enable
||
278 swr_vs
->info
.base
.culldist_writemask
) {
279 unsigned clip_mask
= ctx
->rasterizer
->clip_plane_enable
;
282 if (swr_vs
->info
.base
.writes_clipvertex
) {
283 cv
= 1 + locate_linkage(TGSI_SEMANTIC_CLIPVERTEX
, 0,
286 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
287 if (swr_vs
->info
.base
.output_semantic_name
[i
] == TGSI_SEMANTIC_POSITION
&&
288 swr_vs
->info
.base
.output_semantic_index
[i
] == 0) {
294 LLVMValueRef cx
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][0], "");
295 LLVMValueRef cy
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][1], "");
296 LLVMValueRef cz
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][2], "");
297 LLVMValueRef cw
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][3], "");
299 for (unsigned val
= 0; val
< PIPE_MAX_CLIP_PLANES
; val
++) {
300 // clip distance overrides user clip planes
301 if ((swr_vs
->info
.base
.clipdist_writemask
& clip_mask
& (1 << val
)) ||
302 ((swr_vs
->info
.base
.culldist_writemask
<< swr_vs
->info
.base
.num_written_clipdistance
) & (1 << val
))) {
303 unsigned cv
= 1 + locate_linkage(TGSI_SEMANTIC_CLIPDIST
, val
< 4 ? 0 : 1,
306 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
], "");
307 STORE(unwrap(dist
), vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
});
309 LLVMValueRef dist
= LLVMBuildLoad(gallivm
->builder
, outputs
[cv
][val
- 4], "");
310 STORE(unwrap(dist
), vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4});
315 if (!(clip_mask
& (1 << val
)))
318 Value
*px
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 0}));
319 Value
*py
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 1}));
320 Value
*pz
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 2}));
321 Value
*pw
= LOAD(GEP(hPrivateData
, {0, swr_draw_context_userClipPlanes
, val
, 3}));
322 Value
*dist
= FADD(FMUL(unwrap(cx
), VBROADCAST(px
)),
323 FADD(FMUL(unwrap(cy
), VBROADCAST(py
)),
324 FADD(FMUL(unwrap(cz
), VBROADCAST(pz
)),
325 FMUL(unwrap(cw
), VBROADCAST(pw
)))));
328 STORE(dist
, vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_LO_SLOT
, val
});
330 STORE(dist
, vtxOutput
, {0, 0, VERTEX_CLIPCULL_DIST_HI_SLOT
, val
- 4});
336 gallivm_verify_function(gallivm
, wrap(pFunction
));
337 gallivm_compile_module(gallivm
);
339 // lp_debug_dump_value(func);
341 PFN_VERTEX_FUNC pFunc
=
342 (PFN_VERTEX_FUNC
)gallivm_jit_function(gallivm
, wrap(pFunction
));
344 debug_printf("vert shader %p\n", pFunc
);
345 assert(pFunc
&& "Error: VertShader = NULL");
347 #if (LLVM_VERSION_MAJOR == 3) && (LLVM_VERSION_MINOR >= 5)
348 JM()->mIsModuleFinalized
= true;
355 swr_compile_vs(struct swr_context
*ctx
, swr_jit_vs_key
&key
)
358 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
360 PFN_VERTEX_FUNC func
= builder
.CompileVS(ctx
, key
);
362 ctx
->vs
->map
.insert(std::make_pair(key
, make_unique
<VariantVS
>(builder
.gallivm
, func
)));
367 locate_linkage(ubyte name
, ubyte index
, struct tgsi_shader_info
*info
)
369 for (int i
= 0; i
< PIPE_MAX_SHADER_OUTPUTS
; i
++) {
370 if ((info
->output_semantic_name
[i
] == name
)
371 && (info
->output_semantic_index
[i
] == index
)) {
372 return i
- 1; // position is not part of the linkage
380 BuilderSWR::CompileFS(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
382 struct swr_fragment_shader
*swr_fs
= ctx
->fs
;
384 LLVMValueRef inputs
[PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
];
385 LLVMValueRef outputs
[PIPE_MAX_SHADER_OUTPUTS
][TGSI_NUM_CHANNELS
];
387 memset(inputs
, 0, sizeof(inputs
));
388 memset(outputs
, 0, sizeof(outputs
));
390 struct lp_build_sampler_soa
*sampler
= NULL
;
392 AttrBuilder attrBuilder
;
393 attrBuilder
.addStackAlignmentAttr(JM()->mVWidth
* sizeof(float));
394 AttributeSet attrSet
= AttributeSet::get(
395 JM()->mContext
, AttributeSet::FunctionIndex
, attrBuilder
);
397 std::vector
<Type
*> fsArgs
{PointerType::get(Gen_swr_draw_context(JM()), 0),
398 PointerType::get(Gen_SWR_PS_CONTEXT(JM()), 0)};
399 FunctionType
*funcType
=
400 FunctionType::get(Type::getVoidTy(JM()->mContext
), fsArgs
, false);
402 auto pFunction
= Function::Create(funcType
,
403 GlobalValue::ExternalLinkage
,
405 JM()->mpCurrentModule
);
406 pFunction
->addAttributes(AttributeSet::FunctionIndex
, attrSet
);
408 BasicBlock
*block
= BasicBlock::Create(JM()->mContext
, "entry", pFunction
);
409 IRB()->SetInsertPoint(block
);
410 LLVMPositionBuilderAtEnd(gallivm
->builder
, wrap(block
));
412 auto args
= pFunction
->arg_begin();
413 Value
*hPrivateData
= &*args
++;
414 hPrivateData
->setName("hPrivateData");
415 Value
*pPS
= &*args
++;
416 pPS
->setName("psCtx");
418 Value
*consts_ptr
= GEP(hPrivateData
, {0, swr_draw_context_constantFS
});
419 consts_ptr
->setName("fs_constants");
420 Value
*const_sizes_ptr
=
421 GEP(hPrivateData
, {0, swr_draw_context_num_constantsFS
});
422 const_sizes_ptr
->setName("num_fs_constants");
424 // load *pAttribs, *pPerspAttribs
425 Value
*pRawAttribs
= LOAD(pPS
, {0, SWR_PS_CONTEXT_pAttribs
}, "pRawAttribs");
426 Value
*pPerspAttribs
=
427 LOAD(pPS
, {0, SWR_PS_CONTEXT_pPerspAttribs
}, "pPerspAttribs");
429 swr_fs
->constantMask
= 0;
430 swr_fs
->flatConstantMask
= 0;
431 swr_fs
->pointSpriteMask
= 0;
433 for (int attrib
= 0; attrib
< PIPE_MAX_SHADER_INPUTS
; attrib
++) {
434 const unsigned mask
= swr_fs
->info
.base
.input_usage_mask
[attrib
];
435 const unsigned interpMode
= swr_fs
->info
.base
.input_interpolate
[attrib
];
436 const unsigned interpLoc
= swr_fs
->info
.base
.input_interpolate_loc
[attrib
];
442 Value
*vi
= nullptr, *vj
= nullptr;
444 case TGSI_INTERPOLATE_LOC_CENTER
:
445 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_center
}, "i");
446 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_center
}, "j");
448 case TGSI_INTERPOLATE_LOC_CENTROID
:
449 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_centroid
}, "i");
450 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_centroid
}, "j");
452 case TGSI_INTERPOLATE_LOC_SAMPLE
:
453 vi
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vI
, PixelPositions_sample
}, "i");
454 vj
= LOAD(pPS
, {0, SWR_PS_CONTEXT_vJ
, PixelPositions_sample
}, "j");
459 Value
*vw
= nullptr, *pAttribs
;
460 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
461 interpMode
== TGSI_INTERPOLATE_COLOR
) {
462 pAttribs
= pPerspAttribs
;
464 case TGSI_INTERPOLATE_LOC_CENTER
:
465 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}));
467 case TGSI_INTERPOLATE_LOC_CENTROID
:
468 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_centroid
}));
470 case TGSI_INTERPOLATE_LOC_SAMPLE
:
471 vw
= VRCP(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_sample
}));
475 pAttribs
= pRawAttribs
;
481 ubyte semantic_name
= swr_fs
->info
.base
.input_semantic_name
[attrib
];
482 ubyte semantic_idx
= swr_fs
->info
.base
.input_semantic_index
[attrib
];
484 if (semantic_name
== TGSI_SEMANTIC_FACE
) {
486 UI_TO_FP(LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), mFP32Ty
);
487 ff
= FSUB(FMUL(ff
, C(2.0f
)), C(1.0f
));
488 ff
= VECTOR_SPLAT(JM()->mVWidth
, ff
, "vFrontFace");
490 inputs
[attrib
][0] = wrap(ff
);
491 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
492 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
493 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
495 } else if (semantic_name
== TGSI_SEMANTIC_POSITION
) { // gl_FragCoord
496 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER
] ==
497 TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER
) {
498 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_center
}, "vX"));
499 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_center
}, "vY"));
501 inputs
[attrib
][0] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vX
, PixelPositions_UL
}, "vX"));
502 inputs
[attrib
][1] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vY
, PixelPositions_UL
}, "vY"));
504 inputs
[attrib
][2] = wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vZ
}, "vZ"));
506 wrap(LOAD(pPS
, {0, SWR_PS_CONTEXT_vOneOverW
, PixelPositions_center
}, "vOneOverW"));
508 } else if (semantic_name
== TGSI_SEMANTIC_PRIMID
) {
509 Value
*primID
= LOAD(pPS
, {0, SWR_PS_CONTEXT_primID
}, "primID");
510 inputs
[attrib
][0] = wrap(VECTOR_SPLAT(JM()->mVWidth
, primID
));
511 inputs
[attrib
][1] = wrap(VIMMED1(0));
512 inputs
[attrib
][2] = wrap(VIMMED1(0));
513 inputs
[attrib
][3] = wrap(VIMMED1(0));
517 unsigned linkedAttrib
=
518 locate_linkage(semantic_name
, semantic_idx
, &ctx
->vs
->info
.base
);
519 if (semantic_name
== TGSI_SEMANTIC_GENERIC
&&
520 key
.sprite_coord_enable
& (1 << semantic_idx
)) {
521 /* we add an extra attrib to the backendState in swr_update_derived. */
522 linkedAttrib
= ctx
->vs
->info
.base
.num_outputs
- 1;
523 swr_fs
->pointSpriteMask
|= (1 << linkedAttrib
);
524 } else if (linkedAttrib
== 0xFFFFFFFF) {
525 inputs
[attrib
][0] = wrap(VIMMED1(0.0f
));
526 inputs
[attrib
][1] = wrap(VIMMED1(0.0f
));
527 inputs
[attrib
][2] = wrap(VIMMED1(0.0f
));
528 inputs
[attrib
][3] = wrap(VIMMED1(1.0f
));
529 /* If we're reading in color and 2-sided lighting is enabled, we have
532 if (semantic_name
!= TGSI_SEMANTIC_COLOR
|| !key
.light_twoside
)
535 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
536 swr_fs
->constantMask
|= 1 << linkedAttrib
;
537 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
538 swr_fs
->flatConstantMask
|= 1 << linkedAttrib
;
542 unsigned bcolorAttrib
= 0xFFFFFFFF;
543 Value
*offset
= NULL
;
544 if (semantic_name
== TGSI_SEMANTIC_COLOR
&& key
.light_twoside
) {
545 bcolorAttrib
= locate_linkage(
546 TGSI_SEMANTIC_BCOLOR
, semantic_idx
, &ctx
->vs
->info
.base
);
547 /* Neither front nor back colors were available. Nothing to load. */
548 if (bcolorAttrib
== 0xFFFFFFFF && linkedAttrib
== 0xFFFFFFFF)
550 /* If there is no front color, just always use the back color. */
551 if (linkedAttrib
== 0xFFFFFFFF)
552 linkedAttrib
= bcolorAttrib
;
554 if (bcolorAttrib
!= 0xFFFFFFFF) {
555 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
556 swr_fs
->constantMask
|= 1 << bcolorAttrib
;
557 } else if (interpMode
== TGSI_INTERPOLATE_COLOR
) {
558 swr_fs
->flatConstantMask
|= 1 << bcolorAttrib
;
561 unsigned diff
= 12 * (bcolorAttrib
- linkedAttrib
);
565 XOR(C(1), LOAD(pPS
, {0, SWR_PS_CONTEXT_frontFace
}), "backFace");
567 offset
= MUL(back
, C(diff
));
568 offset
->setName("offset");
573 for (int channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
574 if (mask
& (1 << channel
)) {
575 Value
*indexA
= C(linkedAttrib
* 12 + channel
);
576 Value
*indexB
= C(linkedAttrib
* 12 + channel
+ 4);
577 Value
*indexC
= C(linkedAttrib
* 12 + channel
+ 8);
580 indexA
= ADD(indexA
, offset
);
581 indexB
= ADD(indexB
, offset
);
582 indexC
= ADD(indexC
, offset
);
585 Value
*va
= VBROADCAST(LOAD(GEP(pAttribs
, indexA
)));
586 Value
*vb
= VBROADCAST(LOAD(GEP(pAttribs
, indexB
)));
587 Value
*vc
= VBROADCAST(LOAD(GEP(pAttribs
, indexC
)));
589 if (interpMode
== TGSI_INTERPOLATE_CONSTANT
) {
590 inputs
[attrib
][channel
] = wrap(va
);
592 Value
*vk
= FSUB(FSUB(VIMMED1(1.0f
), vi
), vj
);
596 Value
*interp
= FMUL(va
, vi
);
597 Value
*interp1
= FMUL(vb
, vj
);
598 interp
= FADD(interp
, interp1
);
599 interp
= FADD(interp
, vc
);
600 if (interpMode
== TGSI_INTERPOLATE_PERSPECTIVE
||
601 interpMode
== TGSI_INTERPOLATE_COLOR
)
602 interp
= FMUL(interp
, vw
);
603 inputs
[attrib
][channel
] = wrap(interp
);
609 sampler
= swr_sampler_soa_create(key
.sampler
, PIPE_SHADER_FRAGMENT
);
611 struct lp_bld_tgsi_system_values system_values
;
612 memset(&system_values
, 0, sizeof(system_values
));
614 struct lp_build_mask_context mask
;
616 if (swr_fs
->info
.base
.uses_kill
) {
617 Value
*mask_val
= LOAD(pPS
, {0, SWR_PS_CONTEXT_activeMask
}, "activeMask");
619 &mask
, gallivm
, lp_type_float_vec(32, 32 * 8), wrap(mask_val
));
622 lp_build_tgsi_soa(gallivm
,
624 lp_type_float_vec(32, 32 * 8),
625 swr_fs
->info
.base
.uses_kill
? &mask
: NULL
, // mask
627 wrap(const_sizes_ptr
),
635 NULL
); // geometry shader face
637 sampler
->destroy(sampler
);
639 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
641 for (uint32_t attrib
= 0; attrib
< swr_fs
->info
.base
.num_outputs
;
643 switch (swr_fs
->info
.base
.output_semantic_name
[attrib
]) {
644 case TGSI_SEMANTIC_POSITION
: {
647 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][2], "");
648 STORE(unwrap(outZ
), pPS
, {0, SWR_PS_CONTEXT_vZ
});
651 case TGSI_SEMANTIC_COLOR
: {
652 for (uint32_t channel
= 0; channel
< TGSI_NUM_CHANNELS
; channel
++) {
653 if (!outputs
[attrib
][channel
])
657 LLVMBuildLoad(gallivm
->builder
, outputs
[attrib
][channel
], "");
658 if (swr_fs
->info
.base
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
] &&
659 swr_fs
->info
.base
.output_semantic_index
[attrib
] == 0) {
660 for (uint32_t rt
= 0; rt
< key
.nr_cbufs
; rt
++) {
663 {0, SWR_PS_CONTEXT_shaded
, rt
, channel
});
669 SWR_PS_CONTEXT_shaded
,
670 swr_fs
->info
.base
.output_semantic_index
[attrib
],
678 "unknown output from FS %s[%d]\n",
679 tgsi_semantic_names
[swr_fs
->info
.base
680 .output_semantic_name
[attrib
]],
681 swr_fs
->info
.base
.output_semantic_index
[attrib
]);
687 LLVMValueRef mask_result
= 0;
688 if (swr_fs
->info
.base
.uses_kill
) {
689 mask_result
= lp_build_mask_end(&mask
);
692 IRB()->SetInsertPoint(unwrap(LLVMGetInsertBlock(gallivm
->builder
)));
694 if (swr_fs
->info
.base
.uses_kill
) {
695 STORE(unwrap(mask_result
), pPS
, {0, SWR_PS_CONTEXT_activeMask
});
700 gallivm_verify_function(gallivm
, wrap(pFunction
));
702 gallivm_compile_module(gallivm
);
704 PFN_PIXEL_KERNEL kernel
=
705 (PFN_PIXEL_KERNEL
)gallivm_jit_function(gallivm
, wrap(pFunction
));
706 debug_printf("frag shader %p\n", kernel
);
707 assert(kernel
&& "Error: FragShader = NULL");
709 #if (LLVM_VERSION_MAJOR == 3) && (LLVM_VERSION_MINOR >= 5)
710 JM()->mIsModuleFinalized
= true;
717 swr_compile_fs(struct swr_context
*ctx
, swr_jit_fs_key
&key
)
720 reinterpret_cast<JitManager
*>(swr_screen(ctx
->pipe
.screen
)->hJitMgr
),
722 PFN_PIXEL_KERNEL func
= builder
.CompileFS(ctx
, key
);
724 ctx
->fs
->map
.insert(std::make_pair(key
, make_unique
<VariantFS
>(builder
.gallivm
, func
)));