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 API_RENDER_RENDER_DATA_STORE_RENDER_PODS_H
17 #define API_RENDER_RENDER_DATA_STORE_RENDER_PODS_H
18 
19 #include <cstdint>
20 
21 #include <base/math/vector.h>
22 #include <render/namespace.h>
23 #include <render/resource_handle.h>
24 
25 RENDER_BEGIN_NAMESPACE()
26 /** \addtogroup group_render_renderdatastorerenderpods
27  * @{
28  */
29 /** A POD for back buffer configuration
30  * Deprecated: prefer using IRenderFrameUtil::SetBackBufferConfiguration()
31  */
32 struct NodeGraphBackBufferConfiguration {
33     /** Max back buffer name length */
34     static constexpr uint32_t CORE_MAX_BACK_BUFFER_NAME_LENGTH { 128 };
35 
36     /** Back buffer type */
37     enum class BackBufferType : uint32_t {
38         /** Undefined */
39         UNDEFINED,
40         /** Swapchain */
41         SWAPCHAIN,
42         /** GPU image */
43         GPU_IMAGE,
44         /** GPU image buffer copy */
45         GPU_IMAGE_BUFFER_COPY,
46     };
47 
48     /** Name of the GpuImage in render node graph json where rendering happens.
49      * Image with this name must have been created by the application (or by the engine). */
50     char backBufferName[CORE_MAX_BACK_BUFFER_NAME_LENGTH];
51 
52     /** Back buffer type */
53     BackBufferType backBufferType = { BackBufferType::UNDEFINED };
54 
55     /** Handle to the final target.
56      * If backbufferType is SWAPCHAIN this handle is not used.
57      * If backbufferType is GPU_IMAGE this must point to a valid GpuImage.
58      * If backbufferType is GPU_IMAGE_BUFFER_COPY this must point to a valid (linear) GpuImage. */
59     RenderHandle backBufferHandle;
60     /* If backbufferType is GPU_IMAGE_BUFFER_COPY this must point to a valid GpuBuffer where image data is copied. */
61     RenderHandle gpuBufferHandle;
62 
63     /** Present */
64     bool present { false };
65 
66     /** Binary semaphore for signaling end of frame, i.e. when rendered to back buffer */
67     uint64_t gpuSemaphoreHandle { 0 };
68 };
69 
70 /** Post process name constants. */
71 struct PostProcessConstants {
72     /** Render built-in post process indices */
73     enum POST_PROCESS_INDICES {
74         /** Render tonemap */
75         RENDER_TONEMAP = 0,
76         /** Render vignette */
77         RENDER_VIGNETTE = 1,
78         /** Render dither */
79         RENDER_DITHER = 2,
80         /** Render color conversion */
81         RENDER_COLOR_CONVERSION = 3,
82         /** Render fringe */
83         RENDER_COLOR_FRINGE_BIT = 4,
84 
85         /** Render empty */
86         RENDER_EMPTY_5 = 5,
87         /** Render empty */
88         RENDER_EMPTY_6 = 6,
89         /** Render empty */
90         RENDER_EMPTY_7 = 7,
91 
92         /** Render blur index */
93         RENDER_BLUR = 8,
94         /** Render bloom index */
95         RENDER_BLOOM = 9,
96         /** Render fxaa index */
97         RENDER_FXAA = 10,
98         /** Render taa index */
99         RENDER_TAA = 11,
100         /** Render dof index */
101         RENDER_DOF = 12,
102         /** Render motion blur */
103         RENDER_MOTION_BLUR = 13,
104         /** Built-in meaningfull post process count */
105         POST_PROCESS_COUNT = 14,
106     };
107 
108     /** Render built-in post process names */
109     static constexpr BASE_NS::string_view POST_PROCESS_NAMES[POST_PROCESS_INDICES::POST_PROCESS_COUNT] {
110         "render_tonemap",
111         "render_vignette",
112         "render_dither",
113         "render_color_conversion",
114         "render_color_fringe",
115         "",
116         "",
117         "",
118         "render_blur",
119         "render_bloom",
120         "render_fxaa",
121         "render_taa",
122         "render_dof",
123         "render_motion_blur",
124     };
125 
126     /** First available post process id for user custom effects */
127     static constexpr uint32_t FIRST_USER_POST_PROCESS_ID { 16u };
128     /** Last available post process id for user custom effects */
129     static constexpr uint32_t LAST_USER_POST_PROCESS_ID { 31u };
130 
131     /** Global post process factor count */
132     static constexpr uint32_t GLOBAL_FACTOR_COUNT { POST_PROCESS_INDICES::POST_PROCESS_COUNT };
133     /** User global post process factor count */
134     static constexpr uint32_t USER_GLOBAL_FACTOR_COUNT { 16u };
135     /** User local post process factor count */
136     static constexpr uint32_t USER_LOCAL_FACTOR_COUNT { USER_GLOBAL_FACTOR_COUNT };
137     /** User local post process factor byte size */
138     static constexpr uint32_t USER_LOCAL_FACTOR_BYTE_SIZE { 256u };
139 };
140 
141 /** Bloom post process configuration. */
142 struct BloomConfiguration {
143     /** Bloom quality type enum */
144     enum BloomQualityType {
145         /** Low quality */
146         QUALITY_TYPE_LOW = 0,
147         /** Normal quality */
148         QUALITY_TYPE_NORMAL = 1,
149         /** High quality */
150         QUALITY_TYPE_HIGH = 2,
151     };
152     /** Bloom type enum */
153     enum BloomType {
154         /** Normal, smooth to every direction */
155         TYPE_NORMAL = 0,
156         /** Blurred/Blooms more in horizontal direction */
157         TYPE_HORIZONTAL = 1,
158         /** Blurred/Blooms more in vertical direction */
159         TYPE_VERTICAL = 2,
160         /** Bilateral filter, uses depth if available */
161         TYPE_BILATERAL = 3,
162     };
163 
164     /** Bloom type */
165     BloomType bloomType { BloomType::TYPE_NORMAL };
166     /** Bloom quality type */
167     BloomQualityType bloomQualityType { BloomQualityType::QUALITY_TYPE_NORMAL };
168 
169     /** Threshold hard (Default: 1.0f) */
170     float thresholdHard { 1.0f };
171     /** Threshold soft (Default: 2.0f) */
172     float thresholdSoft { 2.0f };
173     /** Coefficient amount */
174     float amountCoefficient { 0.25f };
175     /** Dirt mask coefficient */
176     float dirtMaskCoefficient { 0.0f };
177 
178     /** Optional dirt mask image handle */
179     RenderHandle dirtMaskImage {};
180     /** Use compute dispatches for bloom */
181     bool useCompute { false };
182 };
183 
184 /** Vignette post process configuration. */
185 struct VignetteConfiguration {
186     /** Coefficient (Default: 0.5f) */
187     float coefficient { 0.5f };
188     /** Power (Default: 0.4f) */
189     float power { 0.4f };
190 };
191 
192 /** Color fringe post process configuration. */
193 struct ColorFringeConfiguration {
194     /** Coefficient */
195     float coefficient { 1.0f };
196     /** Distance coefficient */
197     float distanceCoefficient { 2.0f };
198 };
199 
200 /** Dither post process configuration. */
201 struct DitherConfiguration {
202     /** Dither type enum */
203     enum DitherType {
204         /** Interleaved noise */
205         INTERLEAVED_NOISE = 0,
206         /** Interleaved noise */
207         TRIANGLE_NOISE = 0,
208         /** Interleaved noise */
209         TRIANGLE_NOISE_RGB = 0,
210     };
211 
212     /* Dither type */
213     DitherType ditherType { DitherType::INTERLEAVED_NOISE };
214 
215     /* Amount coefficient */
216     float amountCoefficient { 0.1f };
217 };
218 
219 struct BlurConfiguration {
220     /** Blur quality type enum */
221     enum BlurQualityType {
222         /** Low */
223         QUALITY_TYPE_LOW = 0,
224         /** Normal */
225         QUALITY_TYPE_NORMAL = 1,
226         /** Gaussian like, more smooth */
227         QUALITY_TYPE_HIGH = 2,
228     };
229     /** Blur type enum */
230     enum BlurType {
231         /** Normal, smooth to every direction */
232         TYPE_NORMAL = 0,
233         /** Blurred more in horizontal direction */
234         TYPE_HORIZONTAL = 1,
235         /** Blurred more in vertical direction */
236         TYPE_VERTICAL = 2,
237     };
238 
239     /** Blur type */
240     BlurType blurType { BlurType::TYPE_NORMAL };
241     /** Blur quality type */
242     BlurQualityType blurQualityType { BlurQualityType::QUALITY_TYPE_NORMAL };
243     /** Blur desired filter size in pixels. Might not be exact. */
244     float filterSize { 1.0f };
245     /** Blur quality max mip. If mips available. With this one could only blur to first (few) mips */
246     uint32_t maxMipLevel { ~0u };
247 };
248 
249 /** Tonemap configuration. */
250 struct TonemapConfiguration {
251     /** Tonemap type */
252     enum TonemapType {
253         /** Aces */
254         TONEMAP_ACES = 0,
255         /** Aces 2020 */
256         TONEMAP_ACES_2020 = 1,
257         /** Filmic */
258         TONEMAP_FILMIC = 2,
259     };
260 
261     /** Tonemap type */
262     TonemapType tonemapType { TonemapType::TONEMAP_ACES };
263     /** Exposure */
264     float exposure { 0.7f };
265 };
266 
267 /** Opto-electronic conversion functions. */
268 struct ColorConversionConfiguration {
269     /** Tonemap type */
270     enum ConversionFunctionType {
271         /** Linear -> no conversion in normal situation. */
272         CONVERSION_LINEAR = 0,
273         /** Linear to sRGB conversion */
274         CONVERSION_LINEAR_TO_SRGB = 1,
275     };
276 
277     /** Conversion function type */
278     ConversionFunctionType conversionFunctionType { ConversionFunctionType::CONVERSION_LINEAR };
279 };
280 
281 /** Fxaa Antialiasing configuration. */
282 struct FxaaConfiguration {
283     enum class Sharpness { SOFT = 0, MEDIUM = 1, SHARP = 2 };
284     enum class Quality { LOW = 0, MEDIUM = 1, HIGH = 2 };
285 
286     /** Edge sharpness. */
287     Sharpness sharpness { Sharpness::SHARP };
288     /** Overall edge quality. */
289     Quality quality { Quality::MEDIUM };
290 };
291 
292 /** Taa Antialiasing configuration. */
293 struct TaaConfiguration {
294     enum class Sharpness { SOFT = 0, MEDIUM = 1, SHARP = 2 };
295     enum class Quality { LOW = 0, MEDIUM = 1, HIGH = 2 };
296 
297     /** Edge sharpness. */
298     Sharpness sharpness { Sharpness::SHARP };
299     /** Overall edge quality. */
300     Quality quality { Quality::MEDIUM };
301 };
302 
303 /** Depth of field configuration.
304  * This is expected to be filled so that focusPoint is between nearPlane and farPlane.
305  * Amount of blurriness:
306  * nearPlane                             focusPoint                         farPlane
307  *      nearBlur |   (nearBlur..0)     |      0     |      (0..farBlur)   |  farBlur
308  *               | nearTransitionRange | focusRange |  farTransitionRange |
309  */
310 struct DofConfiguration {
311     /** Distance to point of focus. */
312     float focusPoint { 3.f };
313     /** Range around focusPoint which is in focus */
314     float focusRange { 1.f };
315     /** Range before focusRange where the view transitions from focused to blurred. */
316     float nearTransitionRange { 1.f };
317     /** Range after focusRange where the view transitions from blurred to focused. */
318     float farTransitionRange { 1.f };
319     /** Blur level used close to the viewer. */
320     float nearBlur { 2.f };
321     /** Blur level used far away from the viewer. */
322     float farBlur { 2.f };
323     /** View near plane. */
324     float nearPlane { 0.1f };
325     /** View far plane. */
326     float farPlane { 1000.f };
327 };
328 
329 /** Motion blur configuration.
330  */
331 struct MotionBlurConfiguration {
332     /** Sharpness of the effect. */
333     enum class Sharpness : uint32_t { SOFT = 0, MEDIUM = 1, SHARP = 2 };
334     /** Quality of the effect. */
335     enum class Quality : uint32_t { LOW = 0U, MEDIUM = 1U, HIGH = 2U };
336 
337     /** Sharpness. */
338     Sharpness sharpness { Sharpness::SHARP };
339     /** Quality. */
340     Quality quality { Quality::MEDIUM };
341     /** Alpha blending. 1.0 -> fully motion blur sample. */
342     float alpha { 1.0f };
343     /** Velocity coefficient. */
344     float velocityCoefficient { 1.0f };
345 };
346 
347 /** Post process configuration POD. */
348 struct PostProcessConfiguration {
349     /** Post process enable flags. Used in shader as well, must match render_post_process_structs_common.h */
350     enum PostProcessEnableFlagBits : uint32_t {
351         /** Enable tonemap */
352         ENABLE_TONEMAP_BIT = (1 << 0),
353         /** Enable vignette */
354         ENABLE_VIGNETTE_BIT = (1 << 1),
355         /** Enable dither */
356         ENABLE_DITHER_BIT = (1 << 2),
357         /** Enable color conversion */
358         ENABLE_COLOR_CONVERSION_BIT = (1 << 3),
359         /** Enable fringe */
360         ENABLE_COLOR_FRINGE_BIT = (1 << 4),
361 
362         /** Enable blur */
363         ENABLE_BLUR_BIT = (1 << 8),
364         /** Enable bloom */
365         ENABLE_BLOOM_BIT = (1 << 9),
366         /** Enable FXAA */
367         ENABLE_FXAA_BIT = (1 << 10),
368         /** Enable TAA */
369         ENABLE_TAA_BIT = (1 << 11),
370         /** Enable depth of field */
371         ENABLE_DOF_BIT = (1 << 12),
372         /** Enable motion blur */
373         ENABLE_MOTION_BLUR_BIT = (1 << 13),
374     };
375     using PostProcessEnableFlags = uint32_t;
376 
377     /** Factor indices. */
378     enum PostProcessFactorIndices : uint32_t {
379         INDEX_TONEMAP = 0,
380         INDEX_VIGNETTE = 1,
381         INDEX_DITHER = 2,
382         INDEX_COLOR_CONVERSION = 3,
383         INDEX_COLOR_FRINGE = 4,
384 
385         INDEX_BLUR = 8,
386         INDEX_BLOOM = 9,
387         INDEX_FXAA = 10,
388         INDEX_TAA = 11,
389         INDEX_DOF = 12,
390         INDEX_MOTION_BLUR = 13,
391 
392         INDEX_FACTOR_COUNT = 14,
393     };
394     /** Enabled flags */
395     PostProcessEnableFlags enableFlags { 0u };
396 
397     /** Tonemap configuration */
398     TonemapConfiguration tonemapConfiguration;
399     /** Vignette configuration */
400     VignetteConfiguration vignetteConfiguration;
401     /** Dither configuration */
402     DitherConfiguration ditherConfiguration;
403     /** Color conversion configuration */
404     ColorConversionConfiguration colorConversionConfiguration;
405 
406     /** Color fringe configuration */
407     ColorFringeConfiguration colorFringeConfiguration;
408 
409     /** Bloom configuration */
410     BloomConfiguration bloomConfiguration;
411     /** Blur configuration */
412     BlurConfiguration blurConfiguration;
413 
414     /** Fxaa antialiasing configuration */
415     FxaaConfiguration fxaaConfiguration;
416     /** Taa antialiasing configuration */
417     TaaConfiguration taaConfiguration;
418 
419     /** Depth of field configuration */
420     DofConfiguration dofConfiguration;
421 
422     /** Motion blur configuration */
423     MotionBlurConfiguration motionBlurConfiguration;
424 
425     /** User post process factors which are automatically mapped and can be used easily anywhere in the pipeline */
426     BASE_NS::Math::Vec4 userFactors[PostProcessConstants::USER_GLOBAL_FACTOR_COUNT];
427 };
428 
429 /** Render post process configuration (must match render_post_process_structs_common.h */
430 struct RenderPostProcessConfiguration {
431     /* Single uvec4 for flags .x has post process specialization flags */
432     BASE_NS::Math::UVec4 flags { 0, 0, 0, 0 };
433     // .x = delta time (ms), .y = tick delta time (ms), .z = tick total time (s), .w = frame index (asuint)
434     BASE_NS::Math::Vec4 renderTimings { 0.0f, 0.0f, 0.0f, 0.0f };
435     /* All built-in post process factors */
436     BASE_NS::Math::Vec4 factors[PostProcessConstants::GLOBAL_FACTOR_COUNT];
437 
438     /* All user post process factors */
439     BASE_NS::Math::Vec4 userFactors[PostProcessConstants::USER_GLOBAL_FACTOR_COUNT];
440 };
441 
442 /** Default render pod store for shader specialization */
443 struct ShaderSpecializationRenderPod {
444     /* Max supported specialization constants */
445     static constexpr uint32_t MAX_SPECIALIZATION_CONSTANT_COUNT { 8u };
446     /* Constant flags are mapped in order, use constant_id 0, 1.. in shaders.
447      * Do not leave unused constant ids.
448      */
449     struct ConstantFlags {
450         /** Value */
451         uint32_t value { 0u };
452     };
453 
454     /** Count of constants */
455     uint32_t specializationConstantCount { 0u };
456     /** Specialization data */
457     ConstantFlags specializationFlags[MAX_SPECIALIZATION_CONSTANT_COUNT] {};
458 };
459 /** @} */
460 RENDER_END_NAMESPACE()
461 
462 #endif // API_RENDER_RENDER_DATA_STORE_RENDER_PODS_H
463