-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathConstants.cpp
84 lines (64 loc) · 2.95 KB
/
Constants.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "test/Modules.h"
#include "spvgentwo/Templates.h"
using namespace spvgentwo;
spvgentwo::Module test::constants(spvgentwo::IAllocator* _pAllocator, spvgentwo::ILogger* _pLogger)
{
Module module(_pAllocator, _pLogger);
module.addCapability(spv::Capability::Shader);
module.addCapability(spv::Capability::GenericPointer);
module.addCapability(spv::Capability::LiteralSampler);
module.addCapability(spv::Capability::Float64);
module.addCapability(spv::Capability::Int16);
module.addCapability(spv::Capability::Int8);
Function& main = module.addEntryPoint<void>(spv::ExecutionModel::Vertex, u8"main");
BasicBlock& bb = *main;
// using addConstant() manually:
{
Constant myConst = module.newConstant();
// manual constant setup
myConst.addData(123u);
myConst.setType<unsigned int>();
myConst.setOperation(spv::Op::OpConstant);
// add constant to cache/map and retrieve generated OpConstantXXX instruction
Instruction* inst = module.addConstant(myConst);
myConst.reset(); // clear data and type for reuse
// make infers type, data and operation based on value passed
myConst.make(1337.f);
inst = module.addConstant(myConst);
// extract constant data 1337.f
const float* val = inst->getConstant()->getDataAs<float>();
}
// use module constant()
{
// create a vec3
Instruction* vecconst = module.constant(make_vector(1.f, 2.f, 3.f));
vecconst = module.constant(make_vector(-1.0, 32.0));
// create a literal sampler constant
Instruction* samplerconst = module.constant(const_sampler_t{ spv::SamplerAddressingMode::ClampToEdge, ConstantSamplerCoordMode::UnNormalized, spv::SamplerFilterMode::Linear});
// create a nullptr constant for unsigned int pointer
Instruction* nullptrconst = module.constant(const_null_t<unsigned int*>{});
}
// specialization constants
{
// integer specialization constant with value 42
Instruction* intconst = module.constant(42u, true);
//same as above, just a bit shorter
Instruction* s2 = module.specConstant(3u);
// add new constant without added a Constant descriptor, just the instruction
// initialize the new instructions as UDiv operation
// finally convert this UDiv operation to OpSpecConstantOp instruction
Instruction* specOp = module.addConstantInstr()->opUDiv(intconst, s2)->toSpecOp();
// same as above but with explicit opSpecConstantOp
specOp = module.addConstantInstr()->opSpecConstantOp(module.type<unsigned int>(), spv::Op::OpIMul, s2, intconst);
// two short 'short' vectors
Instruction* vecconst1 = module.constant(make_vector((short)8, (short)-16), true);
Instruction* vecconst2 = module.constant(make_vector((short)-32, (short)64), true);
specOp = module.addConstantInstr()->opIMul(vecconst1, vecconst2)->toSpecOp();
// test 8-bit integers
Instruction* const1 = module.constant((char)4, true);
Instruction* const2 = module.constant((char)-3, true);
specOp = module.addConstantInstr()->opIMul(const1, const2)->toSpecOp();
}
bb.returnValue();
return module;
}