1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef GLES_GPU_PROGRAM_GLES_H
17 #define GLES_GPU_PROGRAM_GLES_H
18 
19 #include <base/containers/array_view.h>
20 #include <base/containers/unique_ptr.h>
21 #include <render/device/gpu_resource_desc.h>
22 #include <render/device/pipeline_state_desc.h>
23 #include <render/namespace.h>
24 
25 #include "device/gpu_program.h"
26 #include "gles/spirv_cross_helper_structs_gles.h"
27 
28 RENDER_BEGIN_NAMESPACE()
29 class Device;
30 class DeviceGLES;
31 class ShaderModuleGLES;
32 struct PushConstantReflection;
33 struct OES_Bind {
34     uint8_t set { 0 }, bind { 0 };
35 };
36 struct Binder {
37     uint32_t set;
38     uint32_t bind;
39     DescriptorType type;
40     // [descriptorarrayindex][glindices] for separate sample/image case, there can be multiple texture units to bind to,
41     // for other resources the second(inner) vector length is always 1.
42     BASE_NS::vector<BASE_NS::vector<uint32_t>> id;
43 };
44 
45 struct GpuShaderProgramPlatformDataGL final {
46     uint32_t program { 0 };
47     int32_t flipLocation { Gles::INVALID_LOCATION };
48     BASE_NS::array_view<Binder> resourceList;
49     BASE_NS::array_view<Gles::PushConstantReflection> pushConstants;
50     int32_t inputs[Gles::ResourceLimits::MAX_VERTEXINPUT_ATTRIBUTES] {};
51     const ShaderModuleGLES* vertShaderModule_ { nullptr };
52     const ShaderModuleGLES* fragShaderModule_ { nullptr };
53 };
54 
55 class GpuShaderProgramGLES final : public GpuShaderProgram {
56 public:
57     GpuShaderProgramGLES(Device& device, const GpuShaderProgramCreateData& createData);
58     ~GpuShaderProgramGLES();
59 
60     const GpuShaderProgramPlatformDataGL& GetPlatformData() const;
61     const ShaderReflection& GetReflection() const override;
62 
63     BASE_NS::unique_ptr<GpuShaderProgramGLES> Specialize(
64         const ShaderSpecializationConstantDataView& specialization, uint32_t views) const;
65 
66     BASE_NS::unique_ptr<GpuShaderProgramGLES> OesPatch(
67         const BASE_NS::array_view<const OES_Bind>& binds, uint32_t views) const;
68 
69 private:
70     BASE_NS::unique_ptr<GpuShaderProgramGLES> Specialize(const ShaderSpecializationConstantDataView& specData,
71         const BASE_NS::array_view<const OES_Bind>& oesBinds, uint32_t views) const;
72     void FilterInputs(GpuShaderProgramGLES& ret) const;
73     GpuShaderProgramGLES(Device& device);
74     DeviceGLES& device_;
75     GpuShaderProgramPlatformDataGL plat_;
76     BASE_NS::vector<ShaderSpecialization::Constant> constants_;
77     ShaderReflection reflection_;
78 
79     BASE_NS::vector<Binder> resourceList;
80     BASE_NS::vector<Gles::PushConstantReflection> pushConstants;
81     // copy of specialization data used..
82     BASE_NS::vector<uint32_t> specializedWith;
83 };
84 
85 struct GpuComputeProgramPlatformDataGL final {
86     uint32_t program { 0 };
87     int32_t flipLocation { Gles::INVALID_LOCATION };
88     BASE_NS::array_view<Binder> resourceList;
89     BASE_NS::array_view<Gles::PushConstantReflection> pushConstants;
90     const ShaderModuleGLES* module_ { nullptr };
91 };
92 
93 class GpuComputeProgramGLES final : public GpuComputeProgram {
94 public:
95     GpuComputeProgramGLES(Device& device, const GpuComputeProgramCreateData& createData);
96     ~GpuComputeProgramGLES();
97 
98     const GpuComputeProgramPlatformDataGL& GetPlatformData() const;
99     const ComputeShaderReflection& GetReflection() const override;
100 
101     BASE_NS::unique_ptr<GpuComputeProgramGLES> Specialize(
102         const ShaderSpecializationConstantDataView& specialization) const;
103 
104 private:
105     GpuComputeProgramGLES(Device& device);
106     DeviceGLES& device_;
107     GpuComputeProgramPlatformDataGL plat_;
108     BASE_NS::vector<ShaderSpecialization::Constant> constants_;
109     ComputeShaderReflection reflection_;
110     BASE_NS::vector<Binder> resourceList;
111     BASE_NS::vector<Gles::PushConstantReflection> pushConstants;
112 };
113 RENDER_END_NAMESPACE()
114 
115 #endif // GLES_GPU_PROGRAM_GLES_H
116