Revert "Support indexing by loop variables in SkVMGenerator"

This reverts commit ebf569004f.

Reason for revert: std::clamp is c++17

Original change's description:
> Support indexing by loop variables in SkVMGenerator
>
> Bug: skia:11096
> Change-Id: I25a91bacf1c3455ac67422fb0e59b9b152c2054a
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/354667
> Commit-Queue: Brian Osman <brianosman@google.com>
> Reviewed-by: Mike Klein <mtklein@google.com>

TBR=mtklein@google.com,brianosman@google.com,johnstiles@google.com

Change-Id: I0590cf7fe626fb59be3381b5e8eb66a9a2a9e8cb
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: skia:11096
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/356056
Reviewed-by: Mike Klein <mtklein@google.com>
Commit-Queue: Mike Klein <mtklein@google.com>
This commit is contained in:
Mike Klein 2021-01-19 21:27:41 +00:00 committed by Skia Commit-Bot
parent 9679612d6b
commit b7e836cee9
9 changed files with 25 additions and 152 deletions

View File

@ -488,7 +488,6 @@ sksl_settings_tests = [
]
sksl_rte_tests = [
"$_tests/sksl/runtime/ArrayIndexing.rte",
"$_tests/sksl/runtime/ConversionConstructors.rte",
"$_tests/sksl/runtime/LoopInt.rte",
"$_tests/sksl/runtime/LoopFloat.rte",
@ -496,7 +495,6 @@ sksl_rte_tests = [
"$_tests/sksl/runtime/SampleWithExplicitCoord.rte",
"$_tests/sksl/runtime/SampleWithUniformMatrix.rte",
"$_tests/sksl/runtime/SampleWithVariableMatrix.rte",
"$_tests/sksl/runtime/VectorIndexing.rte",
]
sksl_rte_error_tests = [

View File

@ -568,6 +568,18 @@ namespace skvm {
return id;
}
bool Builder::allImm() const { return true; }
template <typename T, typename... Rest>
bool Builder::allImm(Val id, T* imm, Rest... rest) const {
if (fProgram[id].op == Op::splat) {
static_assert(sizeof(T) == 4);
memcpy(imm, &fProgram[id].immA, 4);
return this->allImm(rest...);
}
return false;
}
Ptr Builder::arg(int stride) {
int ix = (int)fStrides.size();
fStrides.push_back(stride);

View File

@ -897,24 +897,16 @@ namespace skvm {
uint64_t hash() const;
Val push(Instruction);
bool allImm() const { return true; }
template <typename T, typename... Rest>
bool allImm(Val id, T* imm, Rest... rest) const {
if (fProgram[id].op == Op::splat) {
static_assert(sizeof(T) == 4);
memcpy(imm, &fProgram[id].immA, 4);
return this->allImm(rest...);
}
return false;
}
private:
Val push(Op op, Val x=NA, Val y=NA, Val z=NA, Val w=NA, int immA=0, int immB=0) {
return this->push(Instruction{op, x,y,z,w, immA,immB});
}
bool allImm() const;
template <typename T, typename... Rest>
bool allImm(Val, T* imm, Rest...) const;
template <typename T>
bool isImm(Val id, T want) const {
T imm = 0;

View File

@ -509,14 +509,11 @@ SkVMGenerator::Slot SkVMGenerator::getSlot(const Expression& e) {
const IndexExpression& i = e.as<IndexExpression>();
Slot baseSlot = this->getSlot(*i.base());
Value index = this->writeExpression(*i.index());
int indexValue = -1;
SkAssertResult(fBuilder->allImm(index[0], &indexValue));
const Expression& index = *i.index();
SkASSERT(index.isCompileTimeConstant());
// When indexing by a literal, the front-end guarantees that we don't go out of bounds.
// But when indexing by a loop variable, it's possible to generate out-of-bounds access.
// The GLSL spec leaves that behavior undefined - we'll just clamp everything here.
indexValue = std::clamp(indexValue, 0, i.base()->type().columns() - 1);
SKSL_INT indexValue = index.getConstantInt();
SkASSERT(indexValue >= 0 && indexValue < i.base()->type().columns());
size_t stride = slot_count(i.type());
return baseSlot + indexValue * stride;

View File

@ -669,7 +669,8 @@ DEF_TEST(SkSLInterpreterCompound, r) {
REPORTER_ASSERT(r, out == 8);
}
{
// TODO: Doesn't work until SkVM generator supports indexing-by-loop variable
if (false) {
float in[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
float out = 0;
skvm::Program p = build(sums);
@ -689,7 +690,8 @@ DEF_TEST(SkSLInterpreterCompound, r) {
REPORTER_ASSERT(r, out == gRects[2]);
}
{
// TODO: Doesn't work until SkVM generator supports indexing-by-loop variable
if (false) {
ManyRects in;
memset(&in, 0, sizeof(in));
in.fNumRects = 2;

View File

@ -1,41 +0,0 @@
uniform float u1[4];
float index_by_literal() {
return u1[0];
}
uniform float u2[4];
float index_by_loop() {
float sum = 0;
for (int i = 3; i >= 0; --i) {
sum += u2[i];
}
return sum;
}
uniform float u3[4];
float index_by_complex_loop() {
float prod = 1;
for (int i = 0; i < 4; ++i) {
prod *= u3[i < 2 ? 0 : i];
}
return prod;
}
uniform float u4[16];
float index_out_of_bounds_checked() {
float sum = 0;
for (float f = -2.3; f < 17.0; f += 3.7) {
if (f > 0 && f < 16) {
sum -= u4[int(f)];
}
}
return sum;
}
float4 main() {
return float4(
index_by_literal(),
index_by_loop(),
index_by_complex_loop(),
index_out_of_bounds_checked());
}

View File

@ -1,37 +0,0 @@
uniform float4 u1;
float index_by_literal() {
return u1[0];
}
uniform float4 u2;
float index_by_loop() {
float sum = 0;
for (int i = 0; i < 4; ++i) {
sum += u2[i];
}
return sum;
}
uniform float4 u3;
float index_by_complex_loop() {
float prod = 1;
for (int i = 0; i < 4; ++i) {
prod *= u3[i < 2 ? 0 : i];
}
return prod;
}
uniform float4 u4;
float index_clamped_out_of_bounds() {
for (int i = 7; i < 8; i++) {
return u4[i];
}
}
float4 main() {
return float4(
index_by_literal(),
index_by_loop(),
index_by_complex_loop(),
index_clamped_out_of_bounds());
}

View File

@ -1,29 +0,0 @@
13 registers, 27 instructions:
0 r0 = uniform32 ptr0 4
1 r1 = uniform32 ptr0 14
2 r2 = uniform32 ptr0 18
3 r3 = uniform32 ptr0 1C
4 r4 = uniform32 ptr0 20
5 r5 = uniform32 ptr0 24
6 r6 = uniform32 ptr0 2C
7 r7 = uniform32 ptr0 30
8 r8 = uniform32 ptr0 38
9 r9 = uniform32 ptr0 48
10 r10 = uniform32 ptr0 54
11 r11 = uniform32 ptr0 64
12 r12 = splat 0 (0)
13 r3 = add_f32 r4 r3
14 r2 = add_f32 r3 r2
15 r1 = add_f32 r2 r1
16 r5 = mul_f32 r5 r5
17 r6 = mul_f32 r5 r6
18 r7 = mul_f32 r6 r7
19 r8 = sub_f32 r12 r8
20 r9 = sub_f32 r8 r9
21 r10 = sub_f32 r9 r10
22 r11 = sub_f32 r10 r11
loop:
23 store32 ptr1 r0
24 store32 ptr2 r1
25 store32 ptr3 r7
26 store32 ptr4 r11

View File

@ -1,21 +0,0 @@
9 registers, 19 instructions:
0 r0 = uniform32 ptr0 4
1 r1 = uniform32 ptr0 14
2 r2 = uniform32 ptr0 18
3 r3 = uniform32 ptr0 1C
4 r4 = uniform32 ptr0 20
5 r5 = uniform32 ptr0 24
6 r6 = uniform32 ptr0 2C
7 r7 = uniform32 ptr0 30
8 r8 = uniform32 ptr0 40
9 r2 = add_f32 r1 r2
10 r3 = add_f32 r2 r3
11 r4 = add_f32 r3 r4
12 r5 = mul_f32 r5 r5
13 r6 = mul_f32 r5 r6
14 r7 = mul_f32 r6 r7
loop:
15 store32 ptr1 r0
16 store32 ptr2 r4
17 store32 ptr3 r7
18 store32 ptr4 r8