diff --git a/fuzzers/data_builder.h b/fuzzers/data_builder.h index 39625ef545..a931fe0898 100644 --- a/fuzzers/data_builder.h +++ b/fuzzers/data_builder.h @@ -18,9 +18,12 @@ #include #include #include +#include #include #include "fuzzers/random_generator.h" +#include "src/writer/hlsl/generator.h" +#include "src/writer/msl/generator.h" namespace tint { namespace fuzzers { @@ -106,6 +109,14 @@ class DataBuilder { generator_.GetNBytes(reinterpret_cast(out), n); } + /// Generate pseudo-random data of a specific type into an output var + /// @tparam T - type of data to produce + /// @param out - output var to generate into + template + void build(T& out) { + out = build(); + } + /// Implementation of ::build() /// @tparam T - type of data to produce template @@ -145,6 +156,70 @@ class DataBuilder { /// @returns a boolean with even odds of being true or false static bool impl(DataBuilder* b) { return b->generator_.GetBool(); } }; + + /// Specialization for writer::msl::Options + template <> + struct BuildImpl { + /// Generate a pseudo-random writer::msl::Options struct + /// @param b - data builder to use + /// @returns writer::msl::Options filled with pseudo-random data + static writer::msl::Options impl(DataBuilder* b) { + writer::msl::Options out{}; + b->build(out.buffer_size_ubo_index); + b->build(out.fixed_sample_mask); + b->build(out.emit_vertex_point_size); + b->build(out.disable_workgroup_init); + b->build(out.array_length_from_uniform); + return out; + } + }; + + /// Specialization for writer::hlsl::Options + template <> + struct BuildImpl { + /// Generate a pseudo-random writer::hlsl::Options struct + /// @param b - data builder to use + /// @returns writer::hlsl::Options filled with pseudo-random data + static writer::hlsl::Options impl(DataBuilder* b) { + writer::hlsl::Options out{}; + b->build(out.root_constant_binding_point); + b->build(out.disable_workgroup_init); + b->build(out.array_length_from_uniform); + return out; + } + }; + + /// Specialization for writer::ArrayLengthFromUniformOptions + template <> + struct BuildImpl { + /// Generate a pseudo-random writer::ArrayLengthFromUniformOptions struct + /// @param b - data builder to use + /// @returns writer::ArrayLengthFromUniformOptions filled with pseudo-random + /// data + static writer::ArrayLengthFromUniformOptions impl(DataBuilder* b) { + writer::ArrayLengthFromUniformOptions out{}; + b->build(out.ubo_binding); + b->build(out.bindpoint_to_size_index); + return out; + } + }; + + /// Specialization for std::unordered_map + template + struct BuildImpl> { + /// Generate a pseudo-random std::unordered_map + /// @param b - data builder to use + /// @returns std::unordered_map filled with + /// pseudo-random data + static std::unordered_map impl(DataBuilder* b) { + std::unordered_map out; + uint8_t count = b->build(); + for (uint8_t i = 0; i < count; ++i) { + out.emplace(b->build(), b->build()); + } + return out; + } + }; }; } // namespace fuzzers diff --git a/src/writer/array_length_from_uniform_options.h b/src/writer/array_length_from_uniform_options.h index ae92d4f028..2c06287c23 100644 --- a/src/writer/array_length_from_uniform_options.h +++ b/src/writer/array_length_from_uniform_options.h @@ -44,6 +44,9 @@ struct ArrayLengthFromUniformOptions { /// The mapping from storage buffer binding points to the index into the /// uniform buffer where the length of the buffer is stored. std::unordered_map bindpoint_to_size_index; + + // NOTE: Update fuzzers/data_builder.h when adding or changing any struct + // members. }; } // namespace writer diff --git a/src/writer/hlsl/generator.h b/src/writer/hlsl/generator.h index 4dacbd4cda..4013cfa57e 100644 --- a/src/writer/hlsl/generator.h +++ b/src/writer/hlsl/generator.h @@ -56,6 +56,9 @@ struct Options { /// Options used to specify a mapping of binding points to indices into a UBO /// from which to load buffer sizes. ArrayLengthFromUniformOptions array_length_from_uniform = {}; + + // NOTE: Update fuzzers/data_builder.h when adding or changing any struct + // members. }; /// The result produced when generating HLSL. diff --git a/src/writer/msl/generator.h b/src/writer/msl/generator.h index 63d793957c..7f8f92f958 100644 --- a/src/writer/msl/generator.h +++ b/src/writer/msl/generator.h @@ -64,6 +64,9 @@ struct Options { /// Options used to specify a mapping of binding points to indices into a UBO /// from which to load buffer sizes. ArrayLengthFromUniformOptions array_length_from_uniform = {}; + + // NOTE: Update fuzzers/data_builder.h when adding or changing any struct + // members. }; /// The result produced when generating MSL.