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 #include "gpu_resource_util.h"
17 
18 #include <base/containers/byte_array.h>
19 #include <render/device/intf_device.h>
20 #include <render/namespace.h>
21 #include <render/resource_handle.h>
22 
23 #include "device/gpu_buffer.h"
24 #include "device/gpu_resource_handle_util.h"
25 #include "device/gpu_resource_manager.h"
26 #include "util/log.h"
27 #if RENDER_HAS_VULKAN_BACKEND
28 #include "vulkan/gpu_resource_util_vk.h"
29 #endif
30 #if RENDER_HAS_GLES_BACKEND || RENDER_HAS_GL_BACKEND
31 #include "gles/gpu_resource_util_gles.h"
32 #endif
33 
34 RENDER_BEGIN_NAMESPACE()
35 namespace GpuResourceUtil {
CopyGpuResource(const IDevice & device,const GpuResourceManager & gpuResourceMgr,const RenderHandle handle,BASE_NS::ByteArray & byteArray)36 void CopyGpuResource(const IDevice& device, const GpuResourceManager& gpuResourceMgr, const RenderHandle handle,
37     BASE_NS::ByteArray& byteArray)
38 {
39     const RenderHandleType handleType = RenderHandleUtil::GetHandleType(handle);
40     PLUGIN_ASSERT_MSG(handleType == RenderHandleType::GPU_BUFFER, "only gpu buffers supported");
41     if (handleType == RenderHandleType::GPU_BUFFER) {
42         GpuBuffer* resource = gpuResourceMgr.GetBuffer(handle);
43         PLUGIN_ASSERT(resource);
44         if (resource == nullptr) {
45             return;
46         }
47 
48         const DeviceBackendType backendType = device.GetBackendType();
49 #if RENDER_HAS_VULKAN_BACKEND
50         if (backendType == DeviceBackendType::VULKAN) {
51             CopyGpuBufferVk(*resource, byteArray);
52         }
53 #endif
54 #if RENDER_HAS_GLES_BACKEND || RENDER_HAS_GL_BACKEND
55         if ((backendType == DeviceBackendType::OPENGL) || (backendType == DeviceBackendType::OPENGLES)) {
56             CopyGpuBufferGLES(*resource, byteArray);
57         }
58 #endif
59     }
60 }
61 
DebugBufferName(const IDevice & device,const GpuBuffer & buffer,const BASE_NS::string_view name)62 void DebugBufferName(const IDevice& device, const GpuBuffer& buffer, const BASE_NS::string_view name)
63 {
64     const DeviceBackendType backendType = device.GetBackendType();
65 #if RENDER_HAS_VULKAN_BACKEND
66     if (backendType == DeviceBackendType::VULKAN) {
67         DebugBufferNameVk(device, buffer, name);
68     }
69 #endif
70 #if RENDER_HAS_GLES_BACKEND || RENDER_HAS_GL_BACKEND
71     if (backendType == DeviceBackendType::OPENGL) {
72         DebugBufferNameGLES(device, buffer, name);
73     }
74 #endif
75 }
76 
DebugImageName(const IDevice & device,const GpuImage & image,const BASE_NS::string_view name)77 void DebugImageName(const IDevice& device, const GpuImage& image, const BASE_NS::string_view name)
78 {
79     const DeviceBackendType backendType = device.GetBackendType();
80 #if RENDER_HAS_VULKAN_BACKEND
81     if (backendType == DeviceBackendType::VULKAN) {
82         DebugImageNameVk(device, image, name);
83     }
84 #endif
85 #if RENDER_HAS_GLES_BACKEND || RENDER_HAS_GL_BACKEND
86     if (backendType == DeviceBackendType::OPENGL) {
87         DebugImageNameGLES(device, image, name);
88     }
89 #endif
90 }
91 
DebugSamplerName(const IDevice & device,const GpuSampler & sampler,const BASE_NS::string_view name)92 void DebugSamplerName(const IDevice& device, const GpuSampler& sampler, const BASE_NS::string_view name)
93 {
94     const DeviceBackendType backendType = device.GetBackendType();
95 #if RENDER_HAS_VULKAN_BACKEND
96     if (backendType == DeviceBackendType::VULKAN) {
97         DebugSamplerNameVk(device, sampler, name);
98     }
99 #endif
100 #if RENDER_HAS_GLES_BACKEND || RENDER_HAS_GL_BACKEND
101     if (backendType == DeviceBackendType::OPENGL) {
102         DebugSamplerNameGLES(device, sampler, name);
103     }
104 #endif
105 }
106 
107 } // namespace GpuResourceUtil
108 RENDER_END_NAMESPACE()
109