2016-11-22 16:39:36 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2022-03-18 20:40:58 +00:00
|
|
|
#include "include/private/SkSLModifiers.h"
|
2021-08-13 21:29:51 +00:00
|
|
|
#include "include/sksl/SkSLErrorReporter.h"
|
2022-03-18 20:40:58 +00:00
|
|
|
#include "include/sksl/SkSLPosition.h"
|
|
|
|
#include "src/sksl/SkSLBuiltinTypes.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/sksl/SkSLContext.h"
|
2021-09-16 17:18:10 +00:00
|
|
|
#include "src/sksl/SkSLMangler.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/sksl/SkSLMemoryLayout.h"
|
2022-03-18 20:40:58 +00:00
|
|
|
#include "src/sksl/SkSLUtil.h"
|
|
|
|
#include "src/sksl/ir/SkSLType.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
2016-11-22 16:39:36 +00:00
|
|
|
|
2022-03-18 20:40:58 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-11-22 16:39:36 +00:00
|
|
|
DEF_TEST(SkSLMemoryLayout140Test, r) {
|
2021-01-11 21:13:08 +00:00
|
|
|
SkSL::TestingOnly_AbortErrorReporter errors;
|
2021-11-19 15:59:59 +00:00
|
|
|
SkSL::ShaderCaps caps;
|
2021-09-16 17:18:10 +00:00
|
|
|
SkSL::Mangler mangler;
|
|
|
|
SkSL::Context context(errors, caps, mangler);
|
2016-11-22 16:39:36 +00:00
|
|
|
SkSL::MemoryLayout layout(SkSL::MemoryLayout::k140_Standard);
|
|
|
|
|
|
|
|
// basic types
|
2021-01-11 19:22:36 +00:00
|
|
|
REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fFloat));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.size(*context.fTypes.fFloat2));
|
|
|
|
REPORTER_ASSERT(r, 12 == layout.size(*context.fTypes.fFloat3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fFloat4));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fInt));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.size(*context.fTypes.fInt2));
|
|
|
|
REPORTER_ASSERT(r, 12 == layout.size(*context.fTypes.fInt3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fInt4));
|
|
|
|
REPORTER_ASSERT(r, 1 == layout.size(*context.fTypes.fBool));
|
|
|
|
REPORTER_ASSERT(r, 2 == layout.size(*context.fTypes.fBool2));
|
|
|
|
REPORTER_ASSERT(r, 3 == layout.size(*context.fTypes.fBool3));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fBool4));
|
|
|
|
REPORTER_ASSERT(r, 32 == layout.size(*context.fTypes.fFloat2x2));
|
|
|
|
REPORTER_ASSERT(r, 32 == layout.size(*context.fTypes.fFloat2x4));
|
|
|
|
REPORTER_ASSERT(r, 48 == layout.size(*context.fTypes.fFloat3x3));
|
|
|
|
REPORTER_ASSERT(r, 64 == layout.size(*context.fTypes.fFloat4x2));
|
|
|
|
REPORTER_ASSERT(r, 64 == layout.size(*context.fTypes.fFloat4x4));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fFloat));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fFloat2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fInt));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fInt2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fInt3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fInt4));
|
|
|
|
REPORTER_ASSERT(r, 1 == layout.alignment(*context.fTypes.fBool));
|
|
|
|
REPORTER_ASSERT(r, 2 == layout.alignment(*context.fTypes.fBool2));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fBool3));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fBool4));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat2x2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat2x4));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat3x3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4x2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4x4));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
|
|
|
// struct 1
|
|
|
|
std::vector<SkSL::Type::Field> fields1;
|
2022-04-05 12:52:53 +00:00
|
|
|
fields1.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("a"),
|
|
|
|
context.fTypes.fFloat3.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s1 = SkSL::Type::MakeStructType(SkSL::Position(),
|
|
|
|
std::string("s1"), fields1);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*s1));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*s1));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
fields1.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("b"),
|
|
|
|
context.fTypes.fFloat.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s2 = SkSL::Type::MakeStructType(SkSL::Position(), std::string("s2"),
|
|
|
|
fields1);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*s2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*s2));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
fields1.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("c"),
|
|
|
|
context.fTypes.fBool.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s3 = SkSL::Type::MakeStructType(SkSL::Position(), std::string("s3"),
|
|
|
|
fields1);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 32 == layout.size(*s3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*s3));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
|
|
|
// struct 2
|
|
|
|
std::vector<SkSL::Type::Field> fields2;
|
2022-04-05 12:52:53 +00:00
|
|
|
fields2.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("a"),
|
|
|
|
context.fTypes.fInt.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s4 = SkSL::Type::MakeStructType(SkSL::Position(), std::string("s4"),
|
|
|
|
fields2);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*s4));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*s4));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
fields2.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("b"),
|
|
|
|
context.fTypes.fFloat3.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s5 = SkSL::Type::MakeStructType(SkSL::Position(), std::string("s5"),
|
|
|
|
fields2);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 32 == layout.size(*s5));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*s5));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
|
|
|
// arrays
|
2020-12-11 21:55:58 +00:00
|
|
|
std::unique_ptr<SkSL::Type> array1 =
|
2022-02-02 21:51:18 +00:00
|
|
|
SkSL::Type::MakeArrayType(std::string("float[4]"), *context.fTypes.fFloat, 4);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 64 == layout.size(*array1));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*array1));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.stride(*array1));
|
|
|
|
|
|
|
|
std::unique_ptr<SkSL::Type> array2 =
|
2022-02-02 21:51:18 +00:00
|
|
|
SkSL::Type::MakeArrayType(std::string("float4[4]"), *context.fTypes.fFloat4, 4);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 64 == layout.size(*array2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*array2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.stride(*array2));
|
2016-11-22 16:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(SkSLMemoryLayout430Test, r) {
|
2021-01-11 21:13:08 +00:00
|
|
|
SkSL::TestingOnly_AbortErrorReporter errors;
|
2021-11-19 15:59:59 +00:00
|
|
|
SkSL::ShaderCaps caps;
|
2021-09-16 17:18:10 +00:00
|
|
|
SkSL::Mangler mangler;
|
|
|
|
SkSL::Context context(errors, caps, mangler);
|
2016-11-22 16:39:36 +00:00
|
|
|
SkSL::MemoryLayout layout(SkSL::MemoryLayout::k430_Standard);
|
|
|
|
|
|
|
|
// basic types
|
2021-01-11 19:22:36 +00:00
|
|
|
REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fFloat));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.size(*context.fTypes.fFloat2));
|
|
|
|
REPORTER_ASSERT(r, 12 == layout.size(*context.fTypes.fFloat3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fFloat4));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fInt));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.size(*context.fTypes.fInt2));
|
|
|
|
REPORTER_ASSERT(r, 12 == layout.size(*context.fTypes.fInt3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fInt4));
|
|
|
|
REPORTER_ASSERT(r, 1 == layout.size(*context.fTypes.fBool));
|
|
|
|
REPORTER_ASSERT(r, 2 == layout.size(*context.fTypes.fBool2));
|
|
|
|
REPORTER_ASSERT(r, 3 == layout.size(*context.fTypes.fBool3));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.size(*context.fTypes.fBool4));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*context.fTypes.fFloat2x2));
|
|
|
|
REPORTER_ASSERT(r, 32 == layout.size(*context.fTypes.fFloat2x4));
|
|
|
|
REPORTER_ASSERT(r, 48 == layout.size(*context.fTypes.fFloat3x3));
|
|
|
|
REPORTER_ASSERT(r, 32 == layout.size(*context.fTypes.fFloat4x2));
|
|
|
|
REPORTER_ASSERT(r, 64 == layout.size(*context.fTypes.fFloat4x4));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fFloat));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fFloat2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fInt));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fInt2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fInt3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fInt4));
|
|
|
|
REPORTER_ASSERT(r, 1 == layout.alignment(*context.fTypes.fBool));
|
|
|
|
REPORTER_ASSERT(r, 2 == layout.alignment(*context.fTypes.fBool2));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fBool3));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*context.fTypes.fBool4));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fFloat2x2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat2x4));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat3x3));
|
|
|
|
REPORTER_ASSERT(r, 8 == layout.alignment(*context.fTypes.fFloat4x2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*context.fTypes.fFloat4x4));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
|
|
|
// struct 1
|
|
|
|
std::vector<SkSL::Type::Field> fields1;
|
2022-04-05 12:52:53 +00:00
|
|
|
fields1.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("a"),
|
2021-01-11 19:22:36 +00:00
|
|
|
context.fTypes.fFloat3.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s1 = SkSL::Type::MakeStructType(SkSL::Position(), std::string("s1"),
|
|
|
|
fields1);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*s1));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*s1));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
fields1.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("b"),
|
|
|
|
context.fTypes.fFloat.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s2 = SkSL::Type::MakeStructType(SkSL::Position(), std::string("s2"),
|
|
|
|
fields1);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*s2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*s2));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
fields1.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("c"),
|
|
|
|
context.fTypes.fBool.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s3 = SkSL::Type::MakeStructType(SkSL::Position(), std::string("s3"),
|
|
|
|
fields1);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 32 == layout.size(*s3));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*s3));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
|
|
|
// struct 2
|
|
|
|
std::vector<SkSL::Type::Field> fields2;
|
2022-04-05 12:52:53 +00:00
|
|
|
fields2.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("a"),
|
|
|
|
context.fTypes.fInt.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s4 = SkSL::Type::MakeStructType(SkSL::Position(), std::string("s4"),
|
|
|
|
fields2);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 4 == layout.size(*s4));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*s4));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
fields2.emplace_back(SkSL::Position(), SkSL::Modifiers(), std::string_view("b"),
|
|
|
|
context.fTypes.fFloat3.get());
|
2022-03-14 15:11:37 +00:00
|
|
|
std::unique_ptr<SkSL::Type> s5 = SkSL::Type::MakeStructType(SkSL::Position(), std::string("s5"),
|
|
|
|
fields2);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 32 == layout.size(*s5));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*s5));
|
2016-11-22 16:39:36 +00:00
|
|
|
|
|
|
|
// arrays
|
2020-12-11 21:55:58 +00:00
|
|
|
std::unique_ptr<SkSL::Type> array1 =
|
2022-02-02 21:51:18 +00:00
|
|
|
SkSL::Type::MakeArrayType(std::string("float[4]"), *context.fTypes.fFloat, 4);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 16 == layout.size(*array1));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.alignment(*array1));
|
|
|
|
REPORTER_ASSERT(r, 4 == layout.stride(*array1));
|
|
|
|
|
|
|
|
std::unique_ptr<SkSL::Type> array2 =
|
2022-02-02 21:51:18 +00:00
|
|
|
SkSL::Type::MakeArrayType(std::string("float4[4]"), *context.fTypes.fFloat4, 4);
|
2020-12-11 21:55:58 +00:00
|
|
|
REPORTER_ASSERT(r, 64 == layout.size(*array2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.alignment(*array2));
|
|
|
|
REPORTER_ASSERT(r, 16 == layout.stride(*array2));
|
2016-11-22 16:39:36 +00:00
|
|
|
}
|