1 //
2 // Copyright 2025 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // trace_fixture_cl.cpp:
7 // OpenCL-specific code for the ANGLE trace replays.
8 //
9
10 #include "trace_fixture_cl.h"
11 #include <string>
12
13 namespace
14 {
15
16 angle::TraceCallbacks *gTraceCallbacks = nullptr;
17
18 }
19
20 cl_platform_id *clPlatformsMap;
21 cl_device_id *clDevicesMap;
22 cl_context *clContextsMap;
23 cl_command_queue *clCommandQueuesMap;
24 cl_mem *clMemMap;
25 cl_event *clEventsMap;
26 cl_program *clProgramsMap;
27 cl_kernel *clKernelsMap;
28 cl_sampler *clSamplerMap;
29 void **clVoidMap;
30
31 std::vector<cl_platform_id> temporaryPlatformsList;
32 std::vector<cl_device_id> temporaryDevicesList;
33 std::vector<cl_kernel> temporaryKernelsList;
34 std::vector<cl_mem> temporaryBuffersList;
35 std::vector<cl_program> temporaryProgramsList;
36 std::vector<cl_event> temporaryEventsList;
37 cl_image_desc temporaryImageDesc;
38 std::vector<cl_context_properties> temporaryContextProps;
39 std::vector<const char *> temporaryCharPointerList;
40 std::vector<const void *> temporaryVoidPtrList;
41 std::vector<const unsigned char *> temporaryUnsignedCharPointerList;
42 void *temporaryVoidPtr;
43
44 angle::ReplayResourceMode gReplayResourceMode = angle::ReplayResourceMode::Active;
45
46 uint8_t *gBinaryData;
47 uint8_t *gReadBuffer;
48 std::string gBinaryDataDir = ".";
49
50 template <typename T>
AllocateZeroedValues(size_t count)51 T *AllocateZeroedValues(size_t count)
52 {
53 T *mem = new T[count + 1];
54 memset(mem, 0, sizeof(T) * (count + 1));
55 return mem;
56 }
57
AllocateZeroedUints(size_t count)58 GLuint *AllocateZeroedUints(size_t count)
59 {
60 return AllocateZeroedValues<GLuint>(count);
61 }
62
InitializeReplayCL(const char * binaryDataFileName,size_t maxClientArraySize,size_t readBufferSize,uint32_t maxCLPlatform,uint32_t maxCLDevices,uint32_t maxCLContexts,uint32_t maxCLCommandQueues,uint32_t maxCLMem,uint32_t maxCLEvents,uint32_t maxCLPrograms,uint32_t maxCLKernels,uint32_t maxCLSamplers,uint32_t maxCLVoidPointer)63 void InitializeReplayCL(const char *binaryDataFileName,
64 size_t maxClientArraySize,
65 size_t readBufferSize,
66 uint32_t maxCLPlatform,
67 uint32_t maxCLDevices,
68 uint32_t maxCLContexts,
69 uint32_t maxCLCommandQueues,
70 uint32_t maxCLMem,
71 uint32_t maxCLEvents,
72 uint32_t maxCLPrograms,
73 uint32_t maxCLKernels,
74 uint32_t maxCLSamplers,
75 uint32_t maxCLVoidPointer)
76 {
77 gBinaryData = gTraceCallbacks->LoadBinaryData(binaryDataFileName);
78 gReadBuffer = new uint8_t[readBufferSize];
79
80 clPlatformsMap = AllocateZeroedValues<cl_platform_id>(maxCLPlatform);
81 clDevicesMap = AllocateZeroedValues<cl_device_id>(maxCLDevices);
82 clContextsMap = AllocateZeroedValues<cl_context>(maxCLContexts);
83 clCommandQueuesMap = AllocateZeroedValues<cl_command_queue>(maxCLCommandQueues);
84 clMemMap = AllocateZeroedValues<cl_mem>(maxCLMem);
85 clEventsMap = AllocateZeroedValues<cl_event>(maxCLEvents);
86 clProgramsMap = AllocateZeroedValues<cl_program>(maxCLPrograms);
87 clKernelsMap = AllocateZeroedValues<cl_kernel>(maxCLKernels);
88 clSamplerMap = AllocateZeroedValues<cl_sampler>(maxCLSamplers);
89 clVoidMap = AllocateZeroedValues<void *>(maxCLVoidPointer);
90 }
91
FinishReplay()92 void FinishReplay()
93 {
94 delete[] gReadBuffer;
95
96 delete[] clPlatformsMap;
97 delete[] clDevicesMap;
98 delete[] clContextsMap;
99 delete[] clCommandQueuesMap;
100 delete[] clMemMap;
101 delete[] clEventsMap;
102 delete[] clProgramsMap;
103 delete[] clKernelsMap;
104 delete[] clSamplerMap;
105 delete[] clVoidMap;
106 }
107
108 angle::TraceInfo gTraceInfo;
109 std::string gTraceGzPath;
110
111 struct TraceFunctionsImplCL : angle::TraceFunctions
112 {
SetupReplayTraceFunctionsImplCL113 void SetupReplay() override { ::SetupReplay(); }
114
ReplayFrameTraceFunctionsImplCL115 void ReplayFrame(uint32_t frameIndex) override { ::ReplayFrame(frameIndex); }
116
ResetReplayTraceFunctionsImplCL117 void ResetReplay() override { ::ResetReplay(); }
118
SetupFirstFrameTraceFunctionsImplCL119 void SetupFirstFrame() override { ::SetupFirstFrame(); }
120
FinishReplayTraceFunctionsImplCL121 void FinishReplay() override { ::FinishReplay(); }
122
SetBinaryDataDirTraceFunctionsImplCL123 void SetBinaryDataDir(const char *dataDir) override { gBinaryDataDir = dataDir; }
124
SetReplayResourceModeTraceFunctionsImplCL125 void SetReplayResourceMode(const angle::ReplayResourceMode resourceMode) override
126 {
127 gReplayResourceMode = resourceMode;
128 }
129
SetTraceInfoTraceFunctionsImplCL130 void SetTraceInfo(const angle::TraceInfo &traceInfo) override { gTraceInfo = traceInfo; }
131
SetTraceGzPathTraceFunctionsImplCL132 void SetTraceGzPath(const std::string &traceGzPath) override { gTraceGzPath = traceGzPath; }
133 };
134
135 TraceFunctionsImplCL gTraceFunctionsImpl;
136
SetupEntryPoints(angle::TraceCallbacks * traceCallbacks,angle::TraceFunctions ** traceFunctions)137 void SetupEntryPoints(angle::TraceCallbacks *traceCallbacks, angle::TraceFunctions **traceFunctions)
138 {
139 gTraceCallbacks = traceCallbacks;
140 *traceFunctions = &gTraceFunctionsImpl;
141 }
142
UpdateCLContextPropertiesNoPlatform(size_t propSize,const cl_context_properties * propData)143 void UpdateCLContextPropertiesNoPlatform(size_t propSize, const cl_context_properties *propData)
144 {
145 temporaryContextProps.resize(propSize);
146 std::memcpy(temporaryContextProps.data(), propData, propSize);
147 }
148
UpdateCLContextPropertiesWithPlatform(size_t propSize,const cl_context_properties * propData,size_t platformIdxInProps,size_t platformIdxInMap)149 void UpdateCLContextPropertiesWithPlatform(size_t propSize,
150 const cl_context_properties *propData,
151 size_t platformIdxInProps,
152 size_t platformIdxInMap)
153 {
154 UpdateCLContextPropertiesNoPlatform(propSize, propData);
155 std::memcpy(&temporaryContextProps.data()[platformIdxInProps],
156 &clPlatformsMap[platformIdxInMap], sizeof(cl_platform_id));
157 }
158