| 33 | : options_(options), gpu_type_(gpu_info.type) {} |
| 34 | |
| 35 | Status ShaderCodegen::Build(CompiledNodeAttributes attr, |
| 36 | ShaderCode* shader_code) const { |
| 37 | VariableAccessor variable_accessor(options_.inline_parameters); |
| 38 | ObjectAccessor object_accessor(gpu_type_ == GpuType::MALI, |
| 39 | options_.sampler_textures, &variable_accessor); |
| 40 | |
| 41 | const auto add_object = [&](const std::string& name, Object&& object) { |
| 42 | if (!object_accessor.AddObject(name, std::forward<Object>(object))) { |
| 43 | return AlreadyExistsError(absl::StrCat("Object \"", name, "\"")); |
| 44 | } |
| 45 | return OkStatus(); |
| 46 | }; |
| 47 | |
| 48 | const auto add_uniform_parameter = [&](Variable&& variable) { |
| 49 | const std::string name = variable.name; |
| 50 | if (!variable_accessor.AddUniformParameter(std::move(variable))) { |
| 51 | return AlreadyExistsError( |
| 52 | absl::StrCat("Uniform parameter \"", name, "\"")); |
| 53 | } |
| 54 | return OkStatus(); |
| 55 | }; |
| 56 | |
| 57 | for (auto&& object : attr.code.objects) { |
| 58 | RETURN_IF_ERROR(add_object(object.first, std::move(object.second))); |
| 59 | } |
| 60 | |
| 61 | for (auto&& variable : attr.code.shared_variables) { |
| 62 | const std::string name = variable.name; |
| 63 | if (!variable_accessor.AddSharedVariable(std::move(variable))) { |
| 64 | return AlreadyExistsError(absl::StrCat("Shared variable \"", name, "\"")); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | for (auto&& variable : attr.code.parameters) { |
| 69 | RETURN_IF_ERROR(add_uniform_parameter(std::move(variable))); |
| 70 | } |
| 71 | |
| 72 | int index = 0; |
| 73 | for (auto&& input : attr.inputs) { |
| 74 | RETURN_IF_ERROR( |
| 75 | add_object(absl::StrCat("input_data_", index++), std::move(input))); |
| 76 | } |
| 77 | index = 0; |
| 78 | for (auto&& output : attr.outputs) { |
| 79 | RETURN_IF_ERROR( |
| 80 | add_object(absl::StrCat("output_data_", index++), std::move(output))); |
| 81 | } |
| 82 | |
| 83 | // TODO(akulik): workload params need to go away and be replaced with |
| 84 | // output_data_0_w |
| 85 | RETURN_IF_ERROR(add_uniform_parameter( |
| 86 | {"workload_x", static_cast<int32_t>(attr.code.workload.x)})); |
| 87 | RETURN_IF_ERROR(add_uniform_parameter( |
| 88 | {"workload_y", static_cast<int32_t>(attr.code.workload.y)})); |
| 89 | RETURN_IF_ERROR(add_uniform_parameter( |
| 90 | {"workload_z", static_cast<int32_t>(attr.code.workload.z)})); |
| 91 | |
| 92 | std::string main_source_code = R"( |
no test coverage detected