1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
// cpu-device.cpp
#include "cpu-device.h"
#include "cpu-buffer.h"
#include "cpu-pipeline-state.h"
#include "cpu-query.h"
#include "cpu-resource-views.h"
#include "cpu-shader-object.h"
#include "cpu-shader-program.h"
#include "cpu-texture.h"
#include <chrono>
namespace gfx
{
using namespace Slang;
namespace cpu
{
DeviceImpl::~DeviceImpl()
{
m_currentPipeline = nullptr;
m_currentRootObject = nullptr;
}
SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::initialize(const Desc& desc)
{
SLANG_RETURN_ON_FAIL(slangContext.initialize(
desc.slang,
desc.extendedDescCount,
desc.extendedDescs,
SLANG_SHADER_HOST_CALLABLE,
"sm_5_1",
makeArray(slang::PreprocessorMacroDesc{"__CPU__", "1"}).getView()));
SLANG_RETURN_ON_FAIL(RendererBase::initialize(desc));
// Initialize DeviceInfo
{
m_info.deviceType = DeviceType::CPU;
m_info.bindingStyle = BindingStyle::CUDA;
m_info.projectionStyle = ProjectionStyle::DirectX;
m_info.apiName = "CPU";
static const float kIdentity[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
::memcpy(m_info.identityProjectionMatrix, kIdentity, sizeof(kIdentity));
m_info.adapterName = "CPU";
m_info.timestampFrequency = 1000000000;
}
// Can support pointers (or something akin to that)
{
m_features.add("has-ptr");
}
return SLANG_OK;
}
SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createTextureResource(
const ITextureResource::Desc& desc,
const ITextureResource::SubresourceData* initData,
ITextureResource** outResource)
{
TextureResource::Desc srcDesc = fixupTextureDesc(desc);
RefPtr<TextureResourceImpl> texture = new TextureResourceImpl(srcDesc);
SLANG_RETURN_ON_FAIL(texture->init(initData));
returnComPtr(outResource, texture);
return SLANG_OK;
}
SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createBufferResource(
const IBufferResource::Desc& descIn,
const void* initData,
IBufferResource** outResource)
{
auto desc = fixupBufferDesc(descIn);
RefPtr<BufferResourceImpl> resource = new BufferResourceImpl(desc);
SLANG_RETURN_ON_FAIL(resource->init());
if (initData)
{
SLANG_RETURN_ON_FAIL(resource->setData(0, desc.sizeInBytes, initData));
}
returnComPtr(outResource, resource);
return SLANG_OK;
}
SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createTextureView(
ITextureResource* inTexture,
IResourceView::Desc const& desc,
IResourceView** outView)
{
auto texture = static_cast<TextureResourceImpl*>(inTexture);
RefPtr<TextureResourceViewImpl> view = new TextureResourceViewImpl(desc, texture);
returnComPtr(outView, view);
return SLANG_OK;
}
SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createBufferView(
IBufferResource* inBuffer,
IBufferResource* counterBuffer,
IResourceView::Desc const& desc,
IResourceView** outView)
{
auto buffer = static_cast<BufferResourceImpl*>(inBuffer);
RefPtr<BufferResourceViewImpl> view = new BufferResourceViewImpl(desc, buffer);
returnComPtr(outView, view);
return SLANG_OK;
}
Result DeviceImpl::createShaderObjectLayout(
slang::ISession* session,
slang::TypeLayoutReflection* typeLayout,
ShaderObjectLayoutBase** outLayout)
{
RefPtr<ShaderObjectLayoutImpl> cpuLayout =
new ShaderObjectLayoutImpl(this, session, typeLayout);
returnRefPtrMove(outLayout, cpuLayout);
return SLANG_OK;
}
Result DeviceImpl::createShaderObject(ShaderObjectLayoutBase* layout, IShaderObject** outObject)
{
auto cpuLayout = static_cast<ShaderObjectLayoutImpl*>(layout);
RefPtr<ShaderObjectImpl> result = new ShaderObjectImpl();
SLANG_RETURN_ON_FAIL(result->init(this, cpuLayout));
returnComPtr(outObject, result);
return SLANG_OK;
}
Result DeviceImpl::createMutableShaderObject(
ShaderObjectLayoutBase* layout,
IShaderObject** outObject)
{
auto cpuLayout = static_cast<ShaderObjectLayoutImpl*>(layout);
RefPtr<MutableShaderObjectImpl> result = new MutableShaderObjectImpl();
SLANG_RETURN_ON_FAIL(result->init(this, cpuLayout));
returnComPtr(outObject, result);
return SLANG_OK;
}
Result DeviceImpl::createRootShaderObject(IShaderProgram* program, ShaderObjectBase** outObject)
{
auto cpuProgram = static_cast<ShaderProgramImpl*>(program);
auto cpuProgramLayout = cpuProgram->layout;
RefPtr<RootShaderObjectImpl> result = new RootShaderObjectImpl();
SLANG_RETURN_ON_FAIL(result->init(this, cpuProgramLayout));
returnRefPtrMove(outObject, result);
return SLANG_OK;
}
SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createProgram(
const IShaderProgram::Desc& desc,
IShaderProgram** outProgram,
ISlangBlob** outDiagnosticBlob)
{
RefPtr<ShaderProgramImpl> cpuProgram = new ShaderProgramImpl();
cpuProgram->init(desc);
auto slangGlobalScope = cpuProgram->linkedProgram;
if (slangGlobalScope)
{
auto slangProgramLayout = slangGlobalScope->getLayout();
if (!slangProgramLayout)
return SLANG_FAIL;
RefPtr<RootShaderObjectLayoutImpl> cpuProgramLayout = new RootShaderObjectLayoutImpl(
this,
slangGlobalScope->getSession(),
slangProgramLayout);
cpuProgramLayout->m_programLayout = slangProgramLayout;
cpuProgram->layout = cpuProgramLayout;
}
returnComPtr(outProgram, cpuProgram);
return SLANG_OK;
}
SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createComputePipelineState(
const ComputePipelineStateDesc& desc,
IPipelineState** outState)
{
RefPtr<PipelineStateImpl> state = new PipelineStateImpl();
state->init(desc);
returnComPtr(outState, state);
return Result();
}
SLANG_NO_THROW Result SLANG_MCALL
DeviceImpl::createQueryPool(const IQueryPool::Desc& desc, IQueryPool** outPool)
{
RefPtr<QueryPoolImpl> pool = new QueryPoolImpl();
pool->init(desc);
returnComPtr(outPool, pool);
return SLANG_OK;
}
void DeviceImpl::writeTimestamp(IQueryPool* pool, GfxIndex index)
{
static_cast<QueryPoolImpl*>(pool)->m_queries[index] =
std::chrono::high_resolution_clock::now().time_since_epoch().count();
}
SLANG_NO_THROW const DeviceInfo& SLANG_MCALL DeviceImpl::getDeviceInfo() const
{
return m_info;
}
SLANG_NO_THROW Result SLANG_MCALL
DeviceImpl::createSamplerState(ISamplerState::Desc const& desc, ISamplerState** outSampler)
{
SLANG_UNUSED(desc);
*outSampler = nullptr;
return SLANG_OK;
}
void* DeviceImpl::map(IBufferResource* buffer, MapFlavor flavor)
{
SLANG_UNUSED(flavor);
auto bufferImpl = static_cast<BufferResourceImpl*>(buffer);
return bufferImpl->m_data;
}
void DeviceImpl::unmap(IBufferResource* buffer, size_t offsetWritten, size_t sizeWritten)
{
SLANG_UNUSED(buffer);
SLANG_UNUSED(offsetWritten);
SLANG_UNUSED(sizeWritten);
}
void DeviceImpl::setPipelineState(IPipelineState* state)
{
m_currentPipeline = static_cast<PipelineStateImpl*>(state);
}
void DeviceImpl::bindRootShaderObject(IShaderObject* object)
{
m_currentRootObject = static_cast<RootShaderObjectImpl*>(object);
}
void DeviceImpl::dispatchCompute(int x, int y, int z)
{
int entryPointIndex = 0;
int targetIndex = 0;
// Specialize the compute kernel based on the shader object bindings.
RefPtr<PipelineStateBase> newPipeline;
maybeSpecializePipeline(m_currentPipeline, m_currentRootObject, newPipeline);
m_currentPipeline = static_cast<PipelineStateImpl*>(newPipeline.Ptr());
auto program = m_currentPipeline->getProgram();
auto entryPointLayout = m_currentRootObject->getLayout()->getEntryPoint(entryPointIndex);
auto entryPointName = entryPointLayout->getEntryPointName();
auto entryPointObject = m_currentRootObject->getEntryPoint(entryPointIndex);
ComPtr<ISlangSharedLibrary> sharedLibrary;
ComPtr<ISlangBlob> diagnostics;
auto compileResult = program->slangGlobalScope->getEntryPointHostCallable(
entryPointIndex,
targetIndex,
sharedLibrary.writeRef(),
diagnostics.writeRef());
if (diagnostics)
{
getDebugCallback()->handleMessage(
compileResult == SLANG_OK ? DebugMessageType::Warning : DebugMessageType::Error,
DebugMessageSource::Slang,
(char*)diagnostics->getBufferPointer());
}
if (SLANG_FAILED(compileResult))
return;
auto func = (slang_prelude::ComputeFunc)sharedLibrary->findSymbolAddressByName(entryPointName);
slang_prelude::ComputeVaryingInput varyingInput;
varyingInput.startGroupID.x = 0;
varyingInput.startGroupID.y = 0;
varyingInput.startGroupID.z = 0;
varyingInput.endGroupID.x = x;
varyingInput.endGroupID.y = y;
varyingInput.endGroupID.z = z;
auto globalParamsData = m_currentRootObject->getDataBuffer();
auto entryPointParamsData = entryPointObject->getDataBuffer();
func(&varyingInput, entryPointParamsData, globalParamsData);
}
void DeviceImpl::copyBuffer(
IBufferResource* dst,
size_t dstOffset,
IBufferResource* src,
size_t srcOffset,
size_t size)
{
auto dstImpl = static_cast<BufferResourceImpl*>(dst);
auto srcImpl = static_cast<BufferResourceImpl*>(src);
memcpy((uint8_t*)dstImpl->m_data + dstOffset, (uint8_t*)srcImpl->m_data + srcOffset, size);
}
} // namespace cpu
Result SLANG_MCALL createCPUDevice(const IDevice::Desc* desc, IDevice** outDevice)
{
RefPtr<cpu::DeviceImpl> result = new cpu::DeviceImpl();
SLANG_RETURN_ON_FAIL(result->initialize(*desc));
returnComPtr(outDevice, result);
return SLANG_OK;
}
} // namespace gfx
|