2020-12-09 15:57:00 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
2020-12-08 21:07:24 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
#ifndef SRC_TRANSFORM_FIRST_INDEX_OFFSET_H_
|
|
|
|
#define SRC_TRANSFORM_FIRST_INDEX_OFFSET_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2020-12-10 17:47:41 +00:00
|
|
|
#include "src/ast/variable_decl_statement.h"
|
2020-12-08 21:07:24 +00:00
|
|
|
#include "src/transform/transform.h"
|
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace transform {
|
|
|
|
|
|
|
|
/// Adds firstVertex/Instance (injected via root constants) to
|
2021-01-18 15:51:13 +00:00
|
|
|
/// vertex/instance index builtins.
|
2020-12-08 21:07:24 +00:00
|
|
|
///
|
|
|
|
/// This transform assumes that Name transform has been run before.
|
|
|
|
///
|
|
|
|
/// Unlike other APIs, D3D always starts vertex and instance numbering at 0,
|
|
|
|
/// regardless of the firstVertex/Instance value specified. This transformer
|
|
|
|
/// adds the value of firstVertex/Instance to each builtin. This action is
|
|
|
|
/// performed by adding a new constant equal to original builtin +
|
|
|
|
/// firstVertex/Instance to each function that references one of these builtins.
|
|
|
|
///
|
|
|
|
/// Note that D3D does not have any semantics for firstVertex/Instance.
|
|
|
|
/// Therefore, these values must by passed to the shader.
|
|
|
|
///
|
|
|
|
/// Before:
|
|
|
|
/// [[builtin(vertex_index)]] var<in> vert_idx : u32;
|
|
|
|
/// fn func() -> u32 {
|
|
|
|
/// return vert_idx;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// After:
|
|
|
|
/// [[block]]
|
|
|
|
/// struct TintFirstIndexOffsetData {
|
2021-03-15 10:43:11 +00:00
|
|
|
/// tint_first_vertex_index : u32;
|
|
|
|
/// tint_first_instance_index : u32;
|
2020-12-08 21:07:24 +00:00
|
|
|
/// };
|
|
|
|
/// [[builtin(vertex_index)]] var<in> tint_first_index_offset_vert_idx : u32;
|
2021-01-15 12:22:16 +00:00
|
|
|
/// [[binding(N), group(M)]] var<uniform> tint_first_index_data :
|
2020-12-08 21:07:24 +00:00
|
|
|
/// TintFirstIndexOffsetData;
|
|
|
|
/// fn func() -> u32 {
|
|
|
|
/// const vert_idx = (tint_first_index_offset_vert_idx +
|
|
|
|
/// tint_first_index_data.tint_first_vertex_index);
|
|
|
|
/// return vert_idx;
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
class FirstIndexOffset : public Transform {
|
|
|
|
public:
|
2021-03-29 21:03:59 +00:00
|
|
|
/// Data is outputted by the FirstIndexOffset transform.
|
2021-02-24 15:55:24 +00:00
|
|
|
/// Data holds information about shader usage and constant buffer offsets.
|
|
|
|
struct Data : public Castable<Data, transform::Data> {
|
|
|
|
/// Constructor
|
|
|
|
/// @param has_vtx_index True if the shader uses vertex_index
|
|
|
|
/// @param has_inst_index True if the shader uses instance_index
|
|
|
|
/// @param first_vtx_offset Offset of first vertex into constant buffer
|
2021-02-25 14:11:50 +00:00
|
|
|
/// @param first_inst_offset Offset of first instance into constant buffer
|
2021-02-24 15:55:24 +00:00
|
|
|
Data(bool has_vtx_index,
|
|
|
|
bool has_inst_index,
|
|
|
|
uint32_t first_vtx_offset,
|
2021-02-25 14:11:50 +00:00
|
|
|
uint32_t first_inst_offset);
|
2021-02-24 15:55:24 +00:00
|
|
|
|
|
|
|
/// Copy constructor
|
|
|
|
Data(const Data&);
|
|
|
|
|
|
|
|
/// Destructor
|
|
|
|
~Data() override;
|
|
|
|
|
|
|
|
/// True if the shader uses vertex_index
|
|
|
|
bool const has_vertex_index;
|
|
|
|
/// True if the shader uses instance_index
|
|
|
|
bool const has_instance_index;
|
|
|
|
/// Offset of first vertex into constant buffer
|
|
|
|
uint32_t const first_vertex_offset;
|
|
|
|
/// Offset of first instance into constant buffer
|
2021-02-25 14:11:50 +00:00
|
|
|
uint32_t const first_instance_offset;
|
2021-02-24 15:55:24 +00:00
|
|
|
};
|
|
|
|
|
2020-12-08 21:07:24 +00:00
|
|
|
/// Constructor
|
|
|
|
/// @param binding the binding() for firstVertex/Instance uniform
|
2021-01-15 12:22:16 +00:00
|
|
|
/// @param group the group() for firstVertex/Instance uniform
|
|
|
|
FirstIndexOffset(uint32_t binding, uint32_t group);
|
2020-12-08 21:07:24 +00:00
|
|
|
~FirstIndexOffset() override;
|
|
|
|
|
2021-01-26 16:57:10 +00:00
|
|
|
/// Runs the transform on `program`, returning the transformation result.
|
|
|
|
/// @param program the source program to transform
|
2021-03-29 21:03:59 +00:00
|
|
|
/// @param data optional extra transform-specific input data
|
2020-12-08 21:07:24 +00:00
|
|
|
/// @returns the transformation result
|
2021-03-29 21:03:59 +00:00
|
|
|
Output Run(const Program* program, const DataMap& data = {}) override;
|
2020-12-08 21:07:24 +00:00
|
|
|
|
|
|
|
private:
|
2021-03-02 11:53:42 +00:00
|
|
|
struct State {
|
|
|
|
/// Adds uniform buffer with firstVertex/Instance to the program builder
|
|
|
|
/// @returns variable of new uniform buffer
|
|
|
|
ast::Variable* AddUniformBuffer();
|
|
|
|
/// Adds constant with modified original_name builtin to func
|
|
|
|
/// @param original_name the name of the original builtin used in function
|
|
|
|
/// @param field_name name of field in firstVertex/Instance buffer
|
|
|
|
/// @param buffer_var variable of firstVertex/Instance buffer
|
|
|
|
ast::VariableDeclStatement* CreateFirstIndexOffset(
|
|
|
|
const std::string& original_name,
|
|
|
|
const std::string& field_name,
|
|
|
|
ast::Variable* buffer_var);
|
|
|
|
|
|
|
|
ProgramBuilder* const dst;
|
|
|
|
uint32_t const binding;
|
|
|
|
uint32_t const group;
|
|
|
|
|
|
|
|
bool has_vertex_index = false;
|
|
|
|
bool has_instance_index = false;
|
|
|
|
uint32_t vertex_index_offset = 0;
|
|
|
|
uint32_t instance_index_offset = 0;
|
|
|
|
};
|
2020-12-08 21:07:24 +00:00
|
|
|
|
|
|
|
uint32_t binding_;
|
2021-01-15 12:22:16 +00:00
|
|
|
uint32_t group_;
|
2020-12-08 21:07:24 +00:00
|
|
|
};
|
|
|
|
} // namespace transform
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
#endif // SRC_TRANSFORM_FIRST_INDEX_OFFSET_H_
|