2020-10-16 02:26:54 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "src/inspector/inspector.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2020-11-03 16:26:09 +00:00
|
|
|
#include <utility>
|
2020-10-16 02:26:54 +00:00
|
|
|
|
|
|
|
#include "src/ast/bool_literal.h"
|
|
|
|
#include "src/ast/constructor_expression.h"
|
|
|
|
#include "src/ast/float_literal.h"
|
|
|
|
#include "src/ast/function.h"
|
|
|
|
#include "src/ast/null_literal.h"
|
|
|
|
#include "src/ast/scalar_constructor_expression.h"
|
|
|
|
#include "src/ast/sint_literal.h"
|
2020-11-03 22:19:50 +00:00
|
|
|
#include "src/ast/type/access_control_type.h"
|
2020-11-12 21:50:50 +00:00
|
|
|
#include "src/ast/type/array_type.h"
|
2020-11-30 23:30:58 +00:00
|
|
|
#include "src/ast/type/f32_type.h"
|
2020-11-30 23:30:58 +00:00
|
|
|
#include "src/ast/type/i32_type.h"
|
2020-11-12 21:50:50 +00:00
|
|
|
#include "src/ast/type/matrix_type.h"
|
|
|
|
#include "src/ast/type/multisampled_texture_type.h"
|
|
|
|
#include "src/ast/type/sampled_texture_type.h"
|
2020-10-22 19:31:31 +00:00
|
|
|
#include "src/ast/type/struct_type.h"
|
2020-11-10 22:30:56 +00:00
|
|
|
#include "src/ast/type/texture_type.h"
|
2020-11-03 16:53:59 +00:00
|
|
|
#include "src/ast/type/type.h"
|
2020-11-30 23:30:58 +00:00
|
|
|
#include "src/ast/type/u32_type.h"
|
2020-11-12 21:50:50 +00:00
|
|
|
#include "src/ast/type/vector_type.h"
|
2020-10-16 02:26:54 +00:00
|
|
|
#include "src/ast/uint_literal.h"
|
2020-12-11 13:07:02 +00:00
|
|
|
#include "src/ast/variable.h"
|
2020-10-16 02:26:54 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace inspector {
|
|
|
|
|
2020-12-02 21:17:58 +00:00
|
|
|
Inspector::Inspector(const ast::Module& module) : module_(module) {}
|
2020-10-16 02:26:54 +00:00
|
|
|
|
2020-12-02 21:17:58 +00:00
|
|
|
Inspector::~Inspector() = default;
|
2020-10-16 02:26:54 +00:00
|
|
|
|
|
|
|
std::vector<EntryPoint> Inspector::GetEntryPoints() {
|
|
|
|
std::vector<EntryPoint> result;
|
|
|
|
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* func : module_.functions()) {
|
2020-10-16 02:26:54 +00:00
|
|
|
if (!func->IsEntryPoint()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
EntryPoint entry_point;
|
2020-11-11 21:35:36 +00:00
|
|
|
entry_point.name = func->name();
|
2020-12-02 21:17:58 +00:00
|
|
|
entry_point.remapped_name = func->name();
|
2020-10-16 02:26:54 +00:00
|
|
|
entry_point.stage = func->pipeline_stage();
|
|
|
|
std::tie(entry_point.workgroup_size_x, entry_point.workgroup_size_y,
|
|
|
|
entry_point.workgroup_size_z) = func->workgroup_size();
|
|
|
|
|
|
|
|
for (auto* var : func->referenced_module_variables()) {
|
|
|
|
if (var->storage_class() == ast::StorageClass::kInput) {
|
|
|
|
entry_point.input_variables.push_back(var->name());
|
|
|
|
} else {
|
|
|
|
entry_point.output_variables.push_back(var->name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push_back(std::move(entry_point));
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:43:00 +00:00
|
|
|
std::string Inspector::GetRemappedNameForEntryPoint(
|
2020-11-11 21:35:36 +00:00
|
|
|
const std::string& entry_point) {
|
2020-11-12 15:43:00 +00:00
|
|
|
// TODO(rharrison): Reenable once all of the backends are using the renamed
|
|
|
|
// entry points.
|
|
|
|
|
|
|
|
// auto* func = FindEntryPointByName(entry_point);
|
|
|
|
// if (!func) {
|
|
|
|
// return {};
|
|
|
|
// }
|
2020-12-02 21:17:58 +00:00
|
|
|
// return func->name();
|
2020-11-12 15:43:00 +00:00
|
|
|
return entry_point;
|
2020-11-11 21:35:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 02:26:54 +00:00
|
|
|
std::map<uint32_t, Scalar> Inspector::GetConstantIDs() {
|
|
|
|
std::map<uint32_t, Scalar> result;
|
2020-11-16 16:31:07 +00:00
|
|
|
for (auto* var : module_.global_variables()) {
|
2020-12-11 13:07:02 +00:00
|
|
|
if (!var->HasConstantIdDecoration()) {
|
2020-10-16 02:26:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are conflicting defintions for a constant id, that is invalid
|
|
|
|
// WGSL, so the validator should catch it. Thus here the inspector just
|
|
|
|
// assumes all definitians of the constant id are the same, so only needs
|
|
|
|
// to find the first reference to constant id.
|
2020-12-11 13:07:02 +00:00
|
|
|
uint32_t constant_id = var->constant_id();
|
2020-10-16 02:26:54 +00:00
|
|
|
if (result.find(constant_id) != result.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!var->has_constructor()) {
|
|
|
|
result[constant_id] = Scalar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* expression = var->constructor();
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* constructor = expression->As<ast::ConstructorExpression>();
|
|
|
|
if (constructor == nullptr) {
|
2020-10-16 02:26:54 +00:00
|
|
|
// This is invalid WGSL, but handling gracefully.
|
|
|
|
result[constant_id] = Scalar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* scalar_constructor =
|
|
|
|
constructor->As<ast::ScalarConstructorExpression>();
|
|
|
|
if (scalar_constructor == nullptr) {
|
2020-10-16 02:26:54 +00:00
|
|
|
// This is invalid WGSL, but handling gracefully.
|
|
|
|
result[constant_id] = Scalar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* literal = scalar_constructor->literal();
|
2020-10-16 02:26:54 +00:00
|
|
|
if (!literal) {
|
|
|
|
// This is invalid WGSL, but handling gracefully.
|
|
|
|
result[constant_id] = Scalar();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* l = literal->As<ast::BoolLiteral>()) {
|
|
|
|
result[constant_id] = Scalar(l->IsTrue());
|
2020-10-16 02:26:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* l = literal->As<ast::UintLiteral>()) {
|
|
|
|
result[constant_id] = Scalar(l->value());
|
2020-10-16 02:26:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* l = literal->As<ast::SintLiteral>()) {
|
|
|
|
result[constant_id] = Scalar(l->value());
|
2020-10-16 02:26:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* l = literal->As<ast::FloatLiteral>()) {
|
|
|
|
result[constant_id] = Scalar(l->value());
|
2020-10-16 02:26:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
result[constant_id] = Scalar();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:31:31 +00:00
|
|
|
std::vector<ResourceBinding> Inspector::GetUniformBufferResourceBindings(
|
|
|
|
const std::string& entry_point) {
|
2020-10-27 18:03:09 +00:00
|
|
|
auto* func = FindEntryPointByName(entry_point);
|
2020-10-22 19:31:31 +00:00
|
|
|
if (!func) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ResourceBinding> result;
|
|
|
|
|
|
|
|
for (auto& ruv : func->referenced_uniform_variables()) {
|
|
|
|
ResourceBinding entry;
|
|
|
|
ast::Variable* var = nullptr;
|
|
|
|
ast::Function::BindingInfo binding_info;
|
|
|
|
std::tie(var, binding_info) = ruv;
|
2020-11-30 23:30:58 +00:00
|
|
|
if (!var->type()->Is<ast::type::AccessControl>()) {
|
2020-11-04 16:08:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto* unwrapped_type = var->type()->UnwrapIfNeeded();
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
auto* str = unwrapped_type->As<ast::type::Struct>();
|
|
|
|
if (str == nullptr) {
|
2020-10-22 19:31:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
if (!str->IsBlockDecorated()) {
|
2020-10-22 19:31:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.bind_group = binding_info.set->value();
|
|
|
|
entry.binding = binding_info.binding->value();
|
2020-11-03 16:53:59 +00:00
|
|
|
entry.min_buffer_binding_size = var->type()->MinBufferBindingSize(
|
|
|
|
ast::type::MemoryLayout::kUniformBuffer);
|
2020-10-22 19:31:31 +00:00
|
|
|
|
2020-11-16 16:41:47 +00:00
|
|
|
result.push_back(entry);
|
2020-10-22 19:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-27 18:03:09 +00:00
|
|
|
std::vector<ResourceBinding> Inspector::GetStorageBufferResourceBindings(
|
|
|
|
const std::string& entry_point) {
|
2020-11-03 22:19:50 +00:00
|
|
|
return GetStorageBufferResourceBindingsImpl(entry_point, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ResourceBinding>
|
|
|
|
Inspector::GetReadOnlyStorageBufferResourceBindings(
|
|
|
|
const std::string& entry_point) {
|
|
|
|
return GetStorageBufferResourceBindingsImpl(entry_point, true);
|
|
|
|
}
|
|
|
|
|
2020-11-05 20:09:50 +00:00
|
|
|
std::vector<ResourceBinding> Inspector::GetSamplerResourceBindings(
|
|
|
|
const std::string& entry_point) {
|
|
|
|
auto* func = FindEntryPointByName(entry_point);
|
|
|
|
if (!func) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ResourceBinding> result;
|
|
|
|
|
|
|
|
for (auto& rs : func->referenced_sampler_variables()) {
|
|
|
|
ResourceBinding entry;
|
|
|
|
ast::Variable* var = nullptr;
|
|
|
|
ast::Function::BindingInfo binding_info;
|
|
|
|
std::tie(var, binding_info) = rs;
|
|
|
|
|
|
|
|
entry.bind_group = binding_info.set->value();
|
|
|
|
entry.binding = binding_info.binding->value();
|
|
|
|
|
2020-11-16 16:41:47 +00:00
|
|
|
result.push_back(entry);
|
2020-11-05 20:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:53:45 +00:00
|
|
|
std::vector<ResourceBinding> Inspector::GetComparisonSamplerResourceBindings(
|
|
|
|
const std::string& entry_point) {
|
|
|
|
auto* func = FindEntryPointByName(entry_point);
|
|
|
|
if (!func) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ResourceBinding> result;
|
|
|
|
|
|
|
|
for (auto& rcs : func->referenced_comparison_sampler_variables()) {
|
|
|
|
ResourceBinding entry;
|
|
|
|
ast::Variable* var = nullptr;
|
|
|
|
ast::Function::BindingInfo binding_info;
|
|
|
|
std::tie(var, binding_info) = rcs;
|
|
|
|
|
|
|
|
entry.bind_group = binding_info.set->value();
|
|
|
|
entry.binding = binding_info.binding->value();
|
|
|
|
|
2020-11-16 16:41:47 +00:00
|
|
|
result.push_back(entry);
|
2020-11-06 17:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-10 22:30:56 +00:00
|
|
|
std::vector<ResourceBinding> Inspector::GetSampledTextureResourceBindings(
|
|
|
|
const std::string& entry_point) {
|
2020-11-12 21:50:50 +00:00
|
|
|
return GetSampledTextureResourceBindingsImpl(entry_point, false);
|
|
|
|
}
|
2020-11-10 22:30:56 +00:00
|
|
|
|
2020-11-12 21:50:50 +00:00
|
|
|
std::vector<ResourceBinding> Inspector::GetMultisampledTextureResourceBindings(
|
|
|
|
const std::string& entry_point) {
|
|
|
|
return GetSampledTextureResourceBindingsImpl(entry_point, true);
|
2020-11-10 22:30:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 22:19:50 +00:00
|
|
|
ast::Function* Inspector::FindEntryPointByName(const std::string& name) {
|
2020-12-11 18:24:53 +00:00
|
|
|
auto* func = module_.FindFunctionBySymbol(module_.GetSymbol(name));
|
2020-11-03 22:19:50 +00:00
|
|
|
if (!func) {
|
|
|
|
error_ += name + " was not found!";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!func->IsEntryPoint()) {
|
|
|
|
error_ += name + " is not an entry point!";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ResourceBinding> Inspector::GetStorageBufferResourceBindingsImpl(
|
|
|
|
const std::string& entry_point,
|
|
|
|
bool read_only) {
|
2020-10-27 18:03:09 +00:00
|
|
|
auto* func = FindEntryPointByName(entry_point);
|
|
|
|
if (!func) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ResourceBinding> result;
|
2020-11-05 20:09:50 +00:00
|
|
|
for (auto& rsv : func->referenced_storagebuffer_variables()) {
|
2020-10-27 18:03:09 +00:00
|
|
|
ResourceBinding entry;
|
|
|
|
ast::Variable* var = nullptr;
|
|
|
|
ast::Function::BindingInfo binding_info;
|
2020-11-05 20:09:50 +00:00
|
|
|
std::tie(var, binding_info) = rsv;
|
2020-12-01 21:07:27 +00:00
|
|
|
|
|
|
|
auto* ac_type = var->type()->As<ast::type::AccessControl>();
|
|
|
|
if (ac_type == nullptr) {
|
2020-11-03 22:19:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (read_only != ac_type->IsReadOnly()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
if (!var->type()->UnwrapIfNeeded()->Is<ast::type::Struct>()) {
|
2020-10-27 18:03:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry.bind_group = binding_info.set->value();
|
|
|
|
entry.binding = binding_info.binding->value();
|
2020-11-03 16:53:59 +00:00
|
|
|
entry.min_buffer_binding_size = var->type()->MinBufferBindingSize(
|
|
|
|
ast::type::MemoryLayout::kStorageBuffer);
|
2020-10-27 18:03:09 +00:00
|
|
|
|
2020-11-16 16:41:47 +00:00
|
|
|
result.push_back(entry);
|
2020-10-27 18:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:50:50 +00:00
|
|
|
std::vector<ResourceBinding> Inspector::GetSampledTextureResourceBindingsImpl(
|
|
|
|
const std::string& entry_point,
|
|
|
|
bool multisampled_only) {
|
|
|
|
auto* func = FindEntryPointByName(entry_point);
|
|
|
|
if (!func) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ResourceBinding> result;
|
|
|
|
auto& referenced_variables =
|
|
|
|
multisampled_only ? func->referenced_multisampled_texture_variables()
|
|
|
|
: func->referenced_sampled_texture_variables();
|
|
|
|
for (auto& ref : referenced_variables) {
|
|
|
|
ResourceBinding entry;
|
|
|
|
ast::Variable* var = nullptr;
|
|
|
|
ast::Function::BindingInfo binding_info;
|
|
|
|
std::tie(var, binding_info) = ref;
|
|
|
|
|
|
|
|
entry.bind_group = binding_info.set->value();
|
|
|
|
entry.binding = binding_info.binding->value();
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
auto* texture_type =
|
2020-11-30 23:30:58 +00:00
|
|
|
var->type()->UnwrapIfNeeded()->As<ast::type::Texture>();
|
2020-11-12 21:50:50 +00:00
|
|
|
switch (texture_type->dim()) {
|
|
|
|
case ast::type::TextureDimension::k1d:
|
|
|
|
entry.dim = ResourceBinding::TextureDimension::k1d;
|
|
|
|
break;
|
|
|
|
case ast::type::TextureDimension::k1dArray:
|
|
|
|
entry.dim = ResourceBinding::TextureDimension::k1dArray;
|
|
|
|
break;
|
|
|
|
case ast::type::TextureDimension::k2d:
|
|
|
|
entry.dim = ResourceBinding::TextureDimension::k2d;
|
|
|
|
break;
|
|
|
|
case ast::type::TextureDimension::k2dArray:
|
|
|
|
entry.dim = ResourceBinding::TextureDimension::k2dArray;
|
|
|
|
break;
|
|
|
|
case ast::type::TextureDimension::k3d:
|
|
|
|
entry.dim = ResourceBinding::TextureDimension::k3d;
|
|
|
|
break;
|
|
|
|
case ast::type::TextureDimension::kCube:
|
|
|
|
entry.dim = ResourceBinding::TextureDimension::kCube;
|
|
|
|
break;
|
|
|
|
case ast::type::TextureDimension::kCubeArray:
|
|
|
|
entry.dim = ResourceBinding::TextureDimension::kCubeArray;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
entry.dim = ResourceBinding::TextureDimension::kNone;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::type::Type* base_type = nullptr;
|
|
|
|
if (multisampled_only) {
|
2020-11-30 23:30:58 +00:00
|
|
|
base_type = texture_type->As<ast::type::MultisampledTexture>()
|
2020-11-30 23:30:58 +00:00
|
|
|
->type()
|
|
|
|
->UnwrapIfNeeded();
|
2020-11-12 21:50:50 +00:00
|
|
|
} else {
|
2020-11-30 23:30:58 +00:00
|
|
|
base_type = texture_type->As<ast::type::SampledTexture>()
|
2020-11-30 23:30:58 +00:00
|
|
|
->type()
|
|
|
|
->UnwrapIfNeeded();
|
2020-11-12 21:50:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 21:07:27 +00:00
|
|
|
if (auto* at = base_type->As<ast::type::Array>()) {
|
|
|
|
base_type = at->type();
|
|
|
|
} else if (auto* mt = base_type->As<ast::type::Matrix>()) {
|
|
|
|
base_type = mt->type();
|
|
|
|
} else if (auto* vt = base_type->As<ast::type::Vector>()) {
|
|
|
|
base_type = vt->type();
|
2020-11-12 21:50:50 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 23:30:58 +00:00
|
|
|
if (base_type->Is<ast::type::F32>()) {
|
2020-11-12 21:50:50 +00:00
|
|
|
entry.sampled_kind = ResourceBinding::SampledKind::kFloat;
|
2020-11-30 23:30:58 +00:00
|
|
|
} else if (base_type->Is<ast::type::U32>()) {
|
2020-11-12 21:50:50 +00:00
|
|
|
entry.sampled_kind = ResourceBinding::SampledKind::kUInt;
|
2020-11-30 23:30:58 +00:00
|
|
|
} else if (base_type->Is<ast::type::I32>()) {
|
2020-11-12 21:50:50 +00:00
|
|
|
entry.sampled_kind = ResourceBinding::SampledKind::kSInt;
|
|
|
|
} else {
|
|
|
|
entry.sampled_kind = ResourceBinding::SampledKind::kUnknown;
|
|
|
|
}
|
|
|
|
|
2020-11-16 16:41:47 +00:00
|
|
|
result.push_back(entry);
|
2020-11-12 21:50:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-16 02:26:54 +00:00
|
|
|
} // namespace inspector
|
|
|
|
} // namespace tint
|