forked from PixarAnimationStudios/OpenUSD
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathblitCmds.mm
More file actions
532 lines (451 loc) · 17 KB
/
blitCmds.mm
File metadata and controls
532 lines (451 loc) · 17 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//
// Copyright 2020 Pixar
//
// Licensed under the terms set forth in the LICENSE.txt file available at
// https://openusd.org/license.
//
#include <Metal/Metal.h>
#include "pxr/imaging/hgiMetal/hgi.h"
#include "pxr/imaging/hgiMetal/blitCmds.h"
#include "pxr/imaging/hgiMetal/buffer.h"
#include "pxr/imaging/hgiMetal/capabilities.h"
#include "pxr/imaging/hgiMetal/conversions.h"
#include "pxr/imaging/hgiMetal/diagnostic.h"
#include "pxr/imaging/hgiMetal/texture.h"
#include "pxr/imaging/hgi/blitCmdsOps.h"
#include "pxr/imaging/hgi/types.h"
#include "pxr/base/arch/pragmas.h"
PXR_NAMESPACE_OPEN_SCOPE
HgiMetalBlitCmds::HgiMetalBlitCmds(HgiMetal *hgi)
: _hgi(hgi)
, _commandBuffer(nil)
, _blitEncoder(nil)
, _label(nil)
, _secondaryCommandBuffer(false)
{
}
HgiMetalBlitCmds::~HgiMetalBlitCmds()
{
TF_VERIFY(_blitEncoder == nil, "Encoder created, but never commited.");
if (_label) {
[_label release];
_label = nil;
}
}
void
HgiMetalBlitCmds::_CreateEncoder()
{
if (!_blitEncoder) {
_commandBuffer = _hgi->GetPrimaryCommandBuffer(this);
if (_commandBuffer == nil) {
_commandBuffer = _hgi->GetSecondaryCommandBuffer();
_secondaryCommandBuffer = true;
}
_blitEncoder = [_commandBuffer blitCommandEncoder];
if (_label) {
[_blitEncoder pushDebugGroup:_label];
[_label release];
_label = nil;
}
if (!_secondaryCommandBuffer) {
_hgi->SetHasWork();
}
}
}
void
HgiMetalBlitCmds::PushDebugGroup(const char* label)
{
if (!HgiMetalDebugEnabled()) {
return;
}
if (_blitEncoder) {
HGIMETAL_DEBUG_PUSH_GROUP(_blitEncoder, label)
}
else {
_label = [@(label) copy];
}
}
void
HgiMetalBlitCmds::PopDebugGroup()
{
if (_blitEncoder) {
HGIMETAL_DEBUG_POP_GROUP(_blitEncoder)
}
}
void
HgiMetalBlitCmds::CopyTextureGpuToCpu(
HgiTextureGpuToCpuOp const& copyOp)
{
HgiTextureHandle texHandle = copyOp.gpuSourceTexture;
HgiMetalTexture* srcTexture =static_cast<HgiMetalTexture*>(texHandle.Get());
if (!TF_VERIFY(srcTexture && srcTexture->GetTextureId(),
"Invalid texture handle")) {
return;
}
if (copyOp.destinationBufferByteSize == 0) {
TF_WARN("The size of the data to copy was zero (aborted)");
return;
}
HgiTextureDesc const& texDesc = srcTexture->GetDescriptor();
MTLPixelFormat metalFormat = HgiMetalConversions::GetPixelFormat(
texDesc.format, texDesc.usage);
if (metalFormat == MTLPixelFormatDepth32Float_Stencil8) {
TF_WARN("Cannot read back depth stencil on Metal.");
return;
}
id<MTLDevice> device = _hgi->GetPrimaryDevice();
MTLResourceOptions options = _hgi->GetCapabilities()->defaultStorageMode;
size_t bytesPerPixel = HgiGetDataSizeOfFormat(texDesc.format);
id<MTLBuffer> cpuBuffer =
[device newBufferWithBytesNoCopy:copyOp.cpuDestinationBuffer
length:copyOp.destinationBufferByteSize
options:options
deallocator:nil];
bool isTexArray = texDesc.layerCount>1;
int depthOffset = isTexArray ? 0 : copyOp.sourceTexelOffset[2];
MTLOrigin origin = MTLOriginMake(
copyOp.sourceTexelOffset[0],
copyOp.sourceTexelOffset[1],
depthOffset);
MTLSize size = MTLSizeMake(
texDesc.dimensions[0] - copyOp.sourceTexelOffset[0],
texDesc.dimensions[1] - copyOp.sourceTexelOffset[1],
texDesc.dimensions[2] - depthOffset);
MTLBlitOption blitOptions = MTLBlitOptionNone;
_CreateEncoder();
[_blitEncoder copyFromTexture:srcTexture->GetTextureId()
sourceSlice:isTexArray ? copyOp.sourceTexelOffset[2] : 0
sourceLevel:copyOp.mipLevel
sourceOrigin:origin
sourceSize:size
toBuffer:cpuBuffer
destinationOffset:0
destinationBytesPerRow:(bytesPerPixel * texDesc.dimensions[0])
destinationBytesPerImage:(bytesPerPixel * texDesc.dimensions[0] *
texDesc.dimensions[1])
options:blitOptions];
#if defined(ARCH_OS_OSX)
if ([cpuBuffer storageMode] == MTLStorageModeManaged) {
[_blitEncoder performSelector:@selector(synchronizeResource:)
withObject:cpuBuffer];
}
#endif
// Offset into the dst buffer
char* dst = ((char*) copyOp.cpuDestinationBuffer) +
copyOp.destinationByteOffset;
// bytes to copy
size_t byteSize = copyOp.destinationBufferByteSize;
[_commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer)
{
const char* src = (const char*) [cpuBuffer contents];
memcpy(dst, src, byteSize);
[cpuBuffer release];
}];
}
void
HgiMetalBlitCmds::CopyTextureCpuToGpu(
HgiTextureCpuToGpuOp const& copyOp)
{
HgiMetalTexture* dstTexture = static_cast<HgiMetalTexture*>(
copyOp.gpuDestinationTexture.Get());
id<MTLTexture> dstTextureId = dstTexture->GetTextureId();
HgiTextureDesc const& dstTexDesc = dstTexture->GetDescriptor();
const size_t width = dstTexDesc.dimensions[0];
const size_t height = dstTexDesc.dimensions[1];
const size_t depth = dstTexDesc.dimensions[2];
// Depth, stencil, depth-stencil, and multisample textures must be
// allocated with MTLStorageModePrivate, which should not be used with
// replaceRegion. We create a temporary, non-private texture to fill with
// the cpu texture data, then blit the data from that texture to our
// private destination texture.
// Create texture descriptor to describe the temp texture.
MTLTextureDescriptor* mtlDesc;
MTLPixelFormat mtlFormat = HgiMetalConversions::GetPixelFormat(
dstTexDesc.format, HgiTextureUsageBitsShaderRead);
mtlDesc = [MTLTextureDescriptor
texture2DDescriptorWithPixelFormat:mtlFormat
width:width
height:height
mipmapped:NO];
mtlDesc.mipmapLevelCount = dstTexDesc.mipLevels;
mtlDesc.arrayLength = dstTexDesc.layerCount;
#if defined(ARCH_OS_OSX)
mtlDesc.resourceOptions = MTLResourceStorageModeManaged;
#else
mtlDesc.resourceOptions = MTLResourceStorageModeShared;
#endif
mtlDesc.sampleCount = 1;
if (dstTexDesc.type == HgiTextureType3D) {
mtlDesc.depth = depth;
mtlDesc.textureType = MTLTextureType3D;
} else if (dstTexDesc.type == HgiTextureType2DArray) {
mtlDesc.textureType = MTLTextureType2DArray;
} else if (dstTexDesc.type == HgiTextureType1D) {
mtlDesc.textureType = MTLTextureType1D;
} else if (dstTexDesc.type == HgiTextureType1DArray) {
mtlDesc.textureType = MTLTextureType1DArray;
}
mtlDesc.usage = MTLTextureUsageShaderRead;
// Create temp texture and fill with initial data.
id<MTLTexture> tempTextureId =
[_hgi->GetPrimaryDevice() newTextureWithDescriptor:mtlDesc];
const bool isTexArray = dstTexDesc.layerCount>1;
GfVec3i const& offsets = copyOp.destinationTexelOffset;
int depthOffset = isTexArray ? 0 : offsets[2];
if (dstTexDesc.type == HgiTextureType1D) {
[tempTextureId
replaceRegion:MTLRegionMake1D(offsets[0], width)
mipmapLevel:copyOp.mipLevel
withBytes:copyOp.cpuSourceBuffer
bytesPerRow:copyOp.bufferByteSize];
} else if (dstTexDesc.type == HgiTextureType2D) {
[tempTextureId
replaceRegion:MTLRegionMake2D(
offsets[0], offsets[1], width, height)
mipmapLevel:copyOp.mipLevel
withBytes:copyOp.cpuSourceBuffer
bytesPerRow:copyOp.bufferByteSize / height];
} else {
[tempTextureId
replaceRegion:MTLRegionMake3D(
offsets[0], offsets[1], depthOffset, width, height, depth)
mipmapLevel:copyOp.mipLevel
slice:isTexArray ? offsets[2] : 0
withBytes:copyOp.cpuSourceBuffer
bytesPerRow:copyOp.bufferByteSize / height / width
bytesPerImage:copyOp.bufferByteSize / depth];
}
// Blit data from temp texture to destination texture.
id<MTLCommandBuffer> commandBuffer = [_hgi->GetQueue() commandBuffer];
id<MTLBlitCommandEncoder> blitCommandEncoder =
[commandBuffer blitCommandEncoder];
int sliceCount = 1;
if (dstTexDesc.type == HgiTextureType1DArray ||
dstTexDesc.type == HgiTextureType2DArray) {
sliceCount = dstTexDesc.layerCount;
}
[blitCommandEncoder copyFromTexture:tempTextureId
sourceSlice:0
sourceLevel:copyOp.mipLevel
toTexture:dstTextureId
destinationSlice:0
destinationLevel:copyOp.mipLevel
sliceCount:sliceCount
levelCount:1];
[blitCommandEncoder endEncoding];
[commandBuffer commit];
[tempTextureId release];
}
void
HgiMetalBlitCmds::CopyBufferGpuToGpu(
HgiBufferGpuToGpuOp const& copyOp)
{
HgiBufferHandle const& srcBufHandle = copyOp.gpuSourceBuffer;
HgiMetalBuffer* srcBuffer =static_cast<HgiMetalBuffer*>(srcBufHandle.Get());
if (!TF_VERIFY(srcBuffer && srcBuffer->GetBufferId(),
"Invalid source buffer handle")) {
return;
}
HgiBufferHandle const& dstBufHandle = copyOp.gpuDestinationBuffer;
HgiMetalBuffer* dstBuffer =static_cast<HgiMetalBuffer*>(dstBufHandle.Get());
if (!TF_VERIFY(dstBuffer && dstBuffer->GetBufferId(),
"Invalid destination buffer handle")) {
return;
}
if (copyOp.byteSize == 0) {
TF_WARN("The size of the data to copy was zero (aborted)");
return;
}
_CreateEncoder();
[_blitEncoder copyFromBuffer:srcBuffer->GetBufferId()
sourceOffset:copyOp.sourceByteOffset
toBuffer:dstBuffer->GetBufferId()
destinationOffset:copyOp.destinationByteOffset
size:copyOp.byteSize];
#if defined(ARCH_OS_OSX)
// APPLE METAL: We need to do this copy host side, otherwise later
// cpu copies into OTHER parts of this destination buffer see some of
// our GPU copied range trampled by alignment of the blit. The Metal
// spec says bytes outside of the range may be copied when calling
// didModifyRange
// We still need this for the staging buffer on AMD GPUs, so I'd like
// to investigate further.
if ([srcBuffer->GetBufferId() storageMode] == MTLStorageModeManaged) {
memcpy((char*)dstBuffer->GetCPUStagingAddress() + copyOp.destinationByteOffset,
(char*)srcBuffer->GetCPUStagingAddress() + copyOp.sourceByteOffset,
copyOp.byteSize);
}
#endif
}
void HgiMetalBlitCmds::CopyBufferCpuToGpu(
HgiBufferCpuToGpuOp const& copyOp)
{
if (copyOp.byteSize == 0 ||
!copyOp.cpuSourceBuffer ||
!copyOp.gpuDestinationBuffer)
{
return;
}
HgiMetalBuffer* metalBuffer = static_cast<HgiMetalBuffer*>(
copyOp.gpuDestinationBuffer.Get());
bool sharedBuffer =
[metalBuffer->GetBufferId() storageMode] == MTLStorageModeShared;
uint8_t *dst = static_cast<uint8_t*>([metalBuffer->GetBufferId() contents]);
size_t dstOffset = copyOp.destinationByteOffset;
// If we used GetCPUStagingAddress as the cpuSourceBuffer when the copyOp
// was created, we can skip the memcpy since the src and dst buffer are
// the same and dst already contains the desired data.
// See also: HgiBuffer::GetCPUStagingAddress.
if (copyOp.cpuSourceBuffer != dst ||
copyOp.sourceByteOffset != copyOp.destinationByteOffset) {
// Offset into the src buffer
const char* src = ((const char*) copyOp.cpuSourceBuffer) +
copyOp.sourceByteOffset;
// Offset into the dst buffer
memcpy(dst + dstOffset, src, copyOp.byteSize);
}
if (!sharedBuffer &&
[metalBuffer->GetBufferId()
respondsToSelector:@selector(didModifyRange:)]) {
NSRange range = NSMakeRange(dstOffset, copyOp.byteSize);
id<MTLResource> resource = metalBuffer->GetBufferId();
ARCH_PRAGMA_PUSH
ARCH_PRAGMA_INSTANCE_METHOD_NOT_FOUND
[resource didModifyRange:range];
ARCH_PRAGMA_POP
}
}
void
HgiMetalBlitCmds::CopyBufferGpuToCpu(HgiBufferGpuToCpuOp const& copyOp)
{
if (copyOp.byteSize == 0 ||
!copyOp.cpuDestinationBuffer ||
!copyOp.gpuSourceBuffer)
{
return;
}
HgiMetalBuffer* metalBuffer = static_cast<HgiMetalBuffer*>(
copyOp.gpuSourceBuffer.Get());
_CreateEncoder();
#if defined(ARCH_OS_OSX)
if ([metalBuffer->GetBufferId() storageMode] == MTLStorageModeManaged) {
[_blitEncoder performSelector:@selector(synchronizeResource:)
withObject:metalBuffer->GetBufferId()];
}
#endif
// Offset into the dst buffer
char* dst = ((char*) copyOp.cpuDestinationBuffer) +
copyOp.destinationByteOffset;
// Offset into the src buffer
const char* src = ((const char*) metalBuffer->GetCPUStagingAddress()) +
copyOp.sourceByteOffset;
// bytes to copy
size_t size = copyOp.byteSize;
[_commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer)
{
memcpy(dst, src, size);
}];
}
void
HgiMetalBlitCmds::CopyTextureToBuffer(HgiTextureToBufferOp const& copyOp)
{
TF_CODING_ERROR("Missing Implementation");
}
void
HgiMetalBlitCmds::CopyBufferToTexture(HgiBufferToTextureOp const& copyOp)
{
TF_CODING_ERROR("Missing Implementation");
}
void
HgiMetalBlitCmds::FillBuffer(HgiBufferHandle const& buffer, uint8_t value)
{
HgiMetalBuffer* metalBuf = static_cast<HgiMetalBuffer*>(buffer.Get());
if (metalBuf) {
_CreateEncoder();
size_t length = metalBuf->GetDescriptor().byteSize;
[_blitEncoder fillBuffer:metalBuf->GetBufferId()
range:NSMakeRange(0, length)
value:value];
}
}
static bool
_HgiTextureCanBeFiltered(HgiTextureDesc const &descriptor)
{
HgiFormat const componentFormat =
HgiGetComponentBaseFormat(descriptor.format);
if (componentFormat == HgiFormatInt16 ||
componentFormat == HgiFormatUInt16 ||
componentFormat == HgiFormatInt32) {
return false;
}
GfVec3i const dims = descriptor.dimensions;
switch (descriptor.type) {
case HgiTextureType1D:
case HgiTextureType1DArray:
return (dims[0] > 1);
case HgiTextureType2D:
case HgiTextureType2DArray:
return (dims[0] > 1 || dims[1] > 1);
case HgiTextureType3D:
return (dims[0] > 1 || dims[1] > 1 || dims[2] > 1);
default:
TF_CODING_ERROR("Unsupported HgiTextureType enum value");
return false;
}
return false;
}
void
HgiMetalBlitCmds::GenerateMipMaps(HgiTextureHandle const& texture)
{
HgiMetalTexture* metalTex = static_cast<HgiMetalTexture*>(texture.Get());
if (metalTex) {
if (_HgiTextureCanBeFiltered(metalTex->GetDescriptor())) {
_CreateEncoder();
[_blitEncoder generateMipmapsForTexture:metalTex->GetTextureId()];
}
}
}
void
HgiMetalBlitCmds::InsertMemoryBarrier(HgiMemoryBarrier barrier)
{
TF_VERIFY(barrier==HgiMemoryBarrierAll, "Unknown barrier");
// Do nothing. All blit encoder work will be visible to next encoder.
}
bool
HgiMetalBlitCmds::_Submit(Hgi* hgi, HgiSubmitWaitType wait)
{
bool submittedWork = false;
if (_blitEncoder) {
[_blitEncoder endEncoding];
_blitEncoder = nil;
submittedWork = true;
HgiMetal::CommitCommandBufferWaitType waitType;
switch(wait) {
case HgiSubmitWaitTypeNoWait:
waitType = HgiMetal::CommitCommandBuffer_NoWait;
break;
case HgiSubmitWaitTypeWaitUntilCompleted:
waitType = HgiMetal::CommitCommandBuffer_WaitUntilCompleted;
break;
}
if (_secondaryCommandBuffer) {
_hgi->CommitSecondaryCommandBuffer(_commandBuffer, waitType);
}
else {
if (waitType != HgiMetal::CommitCommandBuffer_WaitUntilCompleted) {
// Ordering problems between CPU updates and GPU updates to a buffer
// result in incorrect buffer contents. This is avoided by waiting
// until work is scheduled, before future calls to didModifyRange
// are made
waitType = HgiMetal::CommitCommandBuffer_WaitUntilScheduled;
}
_hgi->CommitPrimaryCommandBuffer(waitType);
}
}
if (_secondaryCommandBuffer) {
_hgi->ReleaseSecondaryCommandBuffer(_commandBuffer);
}
_commandBuffer = nil;
return submittedWork;
}
PXR_NAMESPACE_CLOSE_SCOPE