mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Format
This commit is contained in:
@@ -34,20 +34,24 @@ THE SOFTWARE.
|
||||
namespace cc {
|
||||
namespace gfx {
|
||||
|
||||
CCMTLBuffer::CCMTLBuffer(Device *device) : Buffer(device) {
|
||||
CCMTLBuffer::CCMTLBuffer(Device *device) : Buffer(device)
|
||||
{
|
||||
_mtlDevice = id<MTLDevice>(((CCMTLDevice *)_device)->getMTLDevice());
|
||||
}
|
||||
|
||||
bool CCMTLBuffer::initialize(const BufferInfo &info) {
|
||||
bool CCMTLBuffer::initialize(const BufferInfo &info)
|
||||
{
|
||||
_usage = info.usage;
|
||||
_memUsage = info.memUsage;
|
||||
_size = info.size;
|
||||
_stride = std::max(info.stride, 1U);
|
||||
_count = _size / _stride;
|
||||
_flags = info.flags;
|
||||
_isIndirectDrawSupported = static_cast<CCMTLDevice *>(_device)->isIndirectDrawSupported();
|
||||
if (_usage & BufferUsage::INDEX) {
|
||||
switch (_stride) {
|
||||
_indirectDrawSupported = static_cast<CCMTLDevice *>(_device)->isIndirectDrawSupported();
|
||||
if (_usage & BufferUsage::INDEX)
|
||||
{
|
||||
switch (_stride)
|
||||
{
|
||||
case 4: _indexType = MTLIndexTypeUInt32; break;
|
||||
case 2: _indexType = MTLIndexTypeUInt16; break;
|
||||
default:
|
||||
@@ -56,11 +60,15 @@ bool CCMTLBuffer::initialize(const BufferInfo &info) {
|
||||
}
|
||||
}
|
||||
|
||||
if ((_flags & BufferFlagBit::BAKUP_BUFFER) && _size > 0) {
|
||||
if ((_flags & BufferFlagBit::BAKUP_BUFFER) && _size > 0)
|
||||
{
|
||||
_buffer = (uint8_t *)CC_MALLOC(_size);
|
||||
if (_buffer)
|
||||
{
|
||||
_device->getMemoryStatus().bufferSize += _size;
|
||||
else {
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_LOG_ERROR("CCMTLBuffer: Failed to create backup buffer.");
|
||||
return false;
|
||||
}
|
||||
@@ -68,10 +76,15 @@ bool CCMTLBuffer::initialize(const BufferInfo &info) {
|
||||
|
||||
if (_usage & BufferUsageBit::VERTEX ||
|
||||
_usage & BufferUsageBit::UNIFORM ||
|
||||
_usage & BufferUsageBit::INDEX) {
|
||||
_usage & BufferUsageBit::INDEX)
|
||||
{
|
||||
createMTLBuffer(_size, _memUsage);
|
||||
} else if (_usage & BufferUsageBit::INDIRECT) {
|
||||
if (_isIndirectDrawSupported) {
|
||||
}
|
||||
else if (_usage & BufferUsageBit::INDIRECT)
|
||||
{
|
||||
_drawInfos.resize(_count);
|
||||
if (_indirectDrawSupported)
|
||||
{
|
||||
createMTLBuffer(_size, _memUsage);
|
||||
_primitiveIndirectArguments.resize(_count);
|
||||
_indexedPrimitivesIndirectArguments.resize(_count);
|
||||
@@ -83,21 +96,25 @@ bool CCMTLBuffer::initialize(const BufferInfo &info) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CCMTLBuffer::initialize(const BufferViewInfo &info) {
|
||||
bool CCMTLBuffer::initialize(const BufferViewInfo &info)
|
||||
{
|
||||
*this = *static_cast<CCMTLBuffer *>(info.buffer);
|
||||
_bufferViewOffset = info.offset;
|
||||
_isBufferView = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CCMTLBuffer::createMTLBuffer(uint size, MemoryUsage usage) {
|
||||
bool CCMTLBuffer::createMTLBuffer(uint size, MemoryUsage usage)
|
||||
{
|
||||
_mtlResourceOptions = mu::toMTLResourseOption(usage);
|
||||
|
||||
if (_mtlBuffer) {
|
||||
if (_mtlBuffer)
|
||||
{
|
||||
[_mtlBuffer release];
|
||||
}
|
||||
_mtlBuffer = [_mtlDevice newBufferWithLength:size options:_mtlResourceOptions];
|
||||
if (_mtlBuffer == nil) {
|
||||
if (_mtlBuffer == nil)
|
||||
{
|
||||
CCASSERT(false, "Failed to create MTLBuffer.");
|
||||
return false;
|
||||
}
|
||||
@@ -107,17 +124,21 @@ bool CCMTLBuffer::createMTLBuffer(uint size, MemoryUsage usage) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void CCMTLBuffer::destroy() {
|
||||
if (_isBufferView) {
|
||||
void CCMTLBuffer::destroy()
|
||||
{
|
||||
if (_isBufferView)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_mtlBuffer) {
|
||||
if (_mtlBuffer)
|
||||
{
|
||||
[_mtlBuffer release];
|
||||
_mtlBuffer = nil;
|
||||
}
|
||||
|
||||
if (_buffer) {
|
||||
if (_buffer)
|
||||
{
|
||||
CC_FREE(_buffer);
|
||||
_device->getMemoryStatus().bufferSize -= _size;
|
||||
_buffer = nullptr;
|
||||
@@ -129,8 +150,10 @@ void CCMTLBuffer::destroy() {
|
||||
_device->getMemoryStatus().bufferSize -= _size;
|
||||
}
|
||||
|
||||
void CCMTLBuffer::resize(uint size) {
|
||||
if (_isBufferView) {
|
||||
void CCMTLBuffer::resize(uint size)
|
||||
{
|
||||
if (_isBufferView)
|
||||
{
|
||||
CC_LOG_WARNING("Cannot resize a buffer view.");
|
||||
return;
|
||||
}
|
||||
@@ -140,7 +163,8 @@ void CCMTLBuffer::resize(uint size) {
|
||||
|
||||
if (_usage & BufferUsageBit::VERTEX ||
|
||||
_usage & BufferUsageBit::INDEX ||
|
||||
_usage & BufferUsageBit::UNIFORM) {
|
||||
_usage & BufferUsageBit::UNIFORM)
|
||||
{
|
||||
createMTLBuffer(size, _memUsage);
|
||||
}
|
||||
|
||||
@@ -148,8 +172,11 @@ void CCMTLBuffer::resize(uint size) {
|
||||
_size = size;
|
||||
_count = _size / _stride;
|
||||
resizeBuffer(&_buffer, _size, oldSize);
|
||||
if (_usage & BufferUsageBit::INDIRECT) {
|
||||
if (_isIndirectDrawSupported) {
|
||||
if (_usage & BufferUsageBit::INDIRECT)
|
||||
{
|
||||
_drawInfos.resize(_count);
|
||||
if (_indirectDrawSupported)
|
||||
{
|
||||
createMTLBuffer(size, _memUsage);
|
||||
_primitiveIndirectArguments.resize(_count);
|
||||
_indexedPrimitivesIndirectArguments.resize(_count);
|
||||
@@ -166,11 +193,14 @@ void CCMTLBuffer::resizeBuffer(uint8_t **buffer, uint size, uint oldSize) {
|
||||
MemoryStatus &status = _device->getMemoryStatus();
|
||||
const uint8_t *oldBuffer = *buffer;
|
||||
uint8_t *temp = (uint8_t *)CC_MALLOC(size);
|
||||
if (temp) {
|
||||
if (temp)
|
||||
{
|
||||
memcpy(temp, oldBuffer, std::min(oldSize, size));
|
||||
*buffer = temp;
|
||||
status.bufferSize += size;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
CC_LOG_ERROR("Failed to resize buffer.");
|
||||
return;
|
||||
}
|
||||
@@ -179,8 +209,10 @@ void CCMTLBuffer::resizeBuffer(uint8_t **buffer, uint size, uint oldSize) {
|
||||
status.bufferSize -= oldSize;
|
||||
}
|
||||
|
||||
void CCMTLBuffer::update(void *buffer, uint size) {
|
||||
if (_isBufferView) {
|
||||
void CCMTLBuffer::update(void *buffer, uint size)
|
||||
{
|
||||
if (_isBufferView)
|
||||
{
|
||||
CC_LOG_WARNING("Cannot update a buffer view.");
|
||||
return;
|
||||
}
|
||||
@@ -188,21 +220,27 @@ void CCMTLBuffer::update(void *buffer, uint size) {
|
||||
if (_buffer)
|
||||
memcpy(_buffer, buffer, size);
|
||||
|
||||
if (_usage & BufferUsageBit::INDIRECT) {
|
||||
if (_usage & BufferUsageBit::INDIRECT)
|
||||
{
|
||||
auto drawInfoCount = size / _stride;
|
||||
for (int i = 0; i < drawInfoCount; ++i) {
|
||||
for (uint i = 0; i < drawInfoCount; ++i)
|
||||
{
|
||||
memcpy(&_drawInfos[i], static_cast<uint8_t *>(buffer) + i * _stride, _stride);
|
||||
}
|
||||
|
||||
if (!_indirectDrawSupported) {
|
||||
if (!_indirectDrawSupported)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (drawInfoCount > 0) {
|
||||
if (_drawInfos[0].indexCount) {
|
||||
if (drawInfoCount > 0)
|
||||
{
|
||||
if (_drawInfos[0].indexCount)
|
||||
{
|
||||
vector<MTLDrawIndexedPrimitivesIndirectArguments> arguments(drawInfoCount);
|
||||
int i = 0;
|
||||
for (auto &argument : arguments) {
|
||||
for (auto &argument : arguments)
|
||||
{
|
||||
const auto &drawInfo = _drawInfos[i++];
|
||||
argument.indexCount = drawInfo.indexCount;
|
||||
argument.instanceCount = drawInfo.instanceCount == 0 ? 1 : drawInfo.instanceCount;
|
||||
@@ -211,10 +249,13 @@ void CCMTLBuffer::update(void *buffer, uint size) {
|
||||
argument.baseInstance = drawInfo.firstInstance;
|
||||
}
|
||||
memcpy(static_cast<uint8_t *>(_mtlBuffer.contents), arguments.data(), drawInfoCount * sizeof(MTLDrawIndexedPrimitivesIndirectArguments));
|
||||
} else if (_drawInfos[0].vertexCount) {
|
||||
}
|
||||
else if (_drawInfos[0].vertexCount)
|
||||
{
|
||||
vector<MTLDrawPrimitivesIndirectArguments> arguments(drawInfoCount);
|
||||
int i = 0;
|
||||
for (auto &argument : arguments) {
|
||||
for (auto &argument : arguments)
|
||||
{
|
||||
const auto &drawInfo = _drawInfos[i++];
|
||||
argument.vertexCount = drawInfo.vertexCount;
|
||||
argument.instanceCount = drawInfo.instanceCount == 0 ? 1 : drawInfo.instanceCount;
|
||||
@@ -231,8 +272,8 @@ void CCMTLBuffer::update(void *buffer, uint size) {
|
||||
}
|
||||
}
|
||||
|
||||
void CCMTLBuffer::updateMTLBuffer(void *buffer, uint offset, uint size) {
|
||||
if (_mtlBuffer) {
|
||||
if (_mtlBuffer)
|
||||
{
|
||||
CommandBuffer *cmdBuffer = _device->getCommandBuffer();
|
||||
cmdBuffer->begin();
|
||||
static_cast<CCMTLCommandBuffer *>(cmdBuffer)->updateBuffer(this, buffer, size);
|
||||
@@ -243,33 +284,21 @@ void CCMTLBuffer::updateMTLBuffer(void *buffer, uint offset, uint size) {
|
||||
}
|
||||
}
|
||||
|
||||
void CCMTLBuffer::encodeBuffer(CCMTLRenderCommandEncoder &encoder, uint offset, uint binding, ShaderStageFlags stages) {
|
||||
if (_isBufferView) {
|
||||
void CCMTLBuffer::encodeBuffer(CCMTLRenderCommandEncoder &encoder, uint offset, uint binding, ShaderStageFlags stages)
|
||||
{
|
||||
if (_isBufferView)
|
||||
{
|
||||
offset += _bufferViewOffset;
|
||||
}
|
||||
|
||||
if (stages & ShaderStageFlagBit::VERTEX) {
|
||||
if (stages & ShaderStageFlagBit::VERTEX)
|
||||
{
|
||||
encoder.setVertexBuffer(_mtlBuffer, offset, binding);
|
||||
}
|
||||
|
||||
if (stages & ShaderStageFlagBit::FRAGMENT) {
|
||||
[encoder setFragmentBuffer:_mtlBuffer
|
||||
offset:offset
|
||||
atIndex:binding];
|
||||
}
|
||||
}
|
||||
|
||||
void CCMTLBuffer::begin() {
|
||||
_inflightDirty = true;
|
||||
}
|
||||
|
||||
void CCMTLBuffer::updateInflightBuffer(uint offset, uint size) {
|
||||
if (_tripleEnabled && _mtlResourceOptions != MTLResourceStorageModePrivate && _inflightDirty) {
|
||||
_inflightIndex = ((_inflightIndex + 1) % MAX_INFLIGHT_BUFFER);
|
||||
id<MTLBuffer> prevFrameBuffer = _mtlBuffer;
|
||||
_mtlBuffer = _dynamicDataBuffers[_inflightIndex];
|
||||
memcpy((uint8_t *)_mtlBuffer.contents, prevFrameBuffer.contents, offset + size);
|
||||
_inflightDirty = false;
|
||||
if (stages & ShaderStageFlagBit::FRAGMENT)
|
||||
{
|
||||
encoder.setFragmentBuffer(_mtlBuffer, offset, binding);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,8 +54,6 @@ CCMTLCommandBuffer::CCMTLCommandBuffer(Device *device)
|
||||
_indirectDrawSuppotred = _mtlDevice->isIndirectDrawSupported();
|
||||
}
|
||||
|
||||
CCMTLCommandBuffer::~CCMTLCommandBuffer() { destroy(); }
|
||||
|
||||
bool CCMTLCommandBuffer::initialize(const CommandBufferInfo &info)
|
||||
{
|
||||
_type = info.type;
|
||||
@@ -77,6 +75,17 @@ id<CAMetalDrawable> CCMTLCommandBuffer::getCurrentDrawable()
|
||||
return _currDrawable;
|
||||
}
|
||||
|
||||
bool CCMTLCommandBuffer::isRenderingEntireDrawable(const Rect &rect, const CCMTLRenderPass *renderPass)
|
||||
{
|
||||
const int num = renderPass->getColorRenderTargetNums();
|
||||
if (num == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
const auto &renderTargetSize = renderPass->getRenderTargetSizes()[0];
|
||||
return rect.x == 0 && rect.y == 0 && rect.width == renderTargetSize.x && rect.height == renderTargetSize.y;
|
||||
}
|
||||
|
||||
void CCMTLCommandBuffer::begin(RenderPass *renderPass, uint subpass, Framebuffer *frameBuffer, int submitIndex)
|
||||
{
|
||||
if (_commandBufferBegan) return;
|
||||
@@ -91,7 +100,8 @@ void CCMTLCommandBuffer::begin(RenderPass *renderPass, uint subpass, Framebuffer
|
||||
_numInstances = 0;
|
||||
|
||||
_GPUDescriptorSets.assign(_GPUDescriptorSets.size(), nullptr);
|
||||
for (auto &dynamicOffset : _dynamicOffsets) {
|
||||
for (auto &dynamicOffset : _dynamicOffsets)
|
||||
{
|
||||
dynamicOffset.clear();
|
||||
}
|
||||
_firstDirtyDescriptorSet = UINT_MAX;
|
||||
@@ -120,17 +130,6 @@ void CCMTLCommandBuffer::end()
|
||||
}
|
||||
}
|
||||
|
||||
bool CCMTLCommandBuffer::isRenderingEntireDrawable(const Rect &rect, const CCMTLRenderPass *renderPass)
|
||||
{
|
||||
const int num = renderPass->getColorRenderTargetNums();
|
||||
if (num == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
const auto &renderTargetSize = renderPass->getRenderTargetSizes()[0];
|
||||
return rect.x == 0 && rect.y == 0 && rect.width == renderTargetSize.x && rect.height == renderTargetSize.y;
|
||||
}
|
||||
|
||||
void CCMTLCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *fbo, const Rect &renderArea, const Color *colors, float depth, int stencil, bool fromSecondaryCB)
|
||||
{
|
||||
auto isOffscreen = static_cast<CCMTLFramebuffer *>(fbo)->isOffscreen();
|
||||
@@ -170,9 +169,7 @@ void CCMTLCommandBuffer::beginRenderPass(RenderPass *renderPass, Framebuffer *fb
|
||||
|
||||
void CCMTLCommandBuffer::endRenderPass()
|
||||
{
|
||||
[_mtlEncoder endEncoding];
|
||||
[_mtlEncoder release];
|
||||
_mtlEncoder = nil;
|
||||
_commandEncoder.endEncoding();
|
||||
}
|
||||
|
||||
void CCMTLCommandBuffer::bindPipelineState(PipelineState *pso)
|
||||
@@ -191,22 +188,23 @@ void CCMTLCommandBuffer::bindPipelineState(PipelineState *pso)
|
||||
|
||||
if (_gpuPipelineState->mtlDepthStencilState)
|
||||
{
|
||||
[_mtlEncoder setStencilFrontReferenceValue:_gpuPipelineState->stencilRefFront
|
||||
backReferenceValue:_gpuPipelineState->stencilRefBack];
|
||||
[_mtlEncoder setDepthStencilState:_gpuPipelineState->mtlDepthStencilState];
|
||||
_commandEncoder.setStencilFrontBackReferenceValue(_gpuPipelineState->stencilRefFront, _gpuPipelineState->stencilRefBack);
|
||||
_commandEncoder.setDepthStencilState(_gpuPipelineState->mtlDepthStencilState);
|
||||
}
|
||||
}
|
||||
|
||||
void CCMTLCommandBuffer::bindDescriptorSet(uint set, DescriptorSet *descriptorSet, uint dynamicOffsetCount, const uint *dynamicOffsets)
|
||||
{
|
||||
CCASSERT(set < _GPUDescriptorSets.size(), "Invalid set index");
|
||||
if (dynamicOffsetCount) {
|
||||
if (dynamicOffsetCount)
|
||||
{
|
||||
_dynamicOffsets[set].assign(dynamicOffsets, dynamicOffsets + dynamicOffsetCount);
|
||||
if (set < _firstDirtyDescriptorSet) _firstDirtyDescriptorSet = set;
|
||||
}
|
||||
|
||||
auto gpuDescriptorSet = static_cast<CCMTLDescriptorSet *>(descriptorSet)->gpuDescriptorSet();
|
||||
if (_GPUDescriptorSets[set] != gpuDescriptorSet) {
|
||||
if (_GPUDescriptorSets[set] != gpuDescriptorSet)
|
||||
{
|
||||
_GPUDescriptorSets[set] = gpuDescriptorSet;
|
||||
if (set < _firstDirtyDescriptorSet) _firstDirtyDescriptorSet = set;
|
||||
}
|
||||
@@ -234,20 +232,12 @@ void CCMTLCommandBuffer::bindInputAssembler(InputAssembler *ia)
|
||||
|
||||
void CCMTLCommandBuffer::setViewport(const Viewport &vp)
|
||||
{
|
||||
if (_currentViewport == vp)
|
||||
return;
|
||||
|
||||
_currentViewport = vp;
|
||||
[_mtlEncoder setViewport:mu::toMTLViewport(_currentViewport)];
|
||||
_commandEncoder.setViewport(vp);
|
||||
}
|
||||
|
||||
void CCMTLCommandBuffer::setScissor(const Rect &rect)
|
||||
{
|
||||
if (_currentScissor == rect)
|
||||
return;
|
||||
|
||||
_currentScissor = rect;
|
||||
[_mtlEncoder setScissorRect:mu::toMTLScissorRect(_currentScissor)];
|
||||
_commandEncoder.setScissor(rect);
|
||||
}
|
||||
|
||||
void CCMTLCommandBuffer::setLineWidth(const float width)
|
||||
@@ -257,35 +247,12 @@ void CCMTLCommandBuffer::setLineWidth(const float width)
|
||||
|
||||
void CCMTLCommandBuffer::setDepthBias(float constant, float clamp, float slope)
|
||||
{
|
||||
if (math::IsNotEqualF(constant, _depthBias.depthBias) ||
|
||||
math::IsNotEqualF(clamp, _depthBias.clamp) ||
|
||||
math::IsNotEqualF(slope, _depthBias.slopeScale))
|
||||
{
|
||||
_depthBias.depthBias = constant;
|
||||
_depthBias.slopeScale = slope;
|
||||
_depthBias.clamp = clamp;
|
||||
[_mtlEncoder setDepthBias:_depthBias.depthBias
|
||||
slopeScale:_depthBias.slopeScale
|
||||
clamp:_depthBias.clamp];
|
||||
}
|
||||
_commandEncoder.setDepthBias(constant, clamp, slope);
|
||||
}
|
||||
|
||||
void CCMTLCommandBuffer::setBlendConstants(const Color &constants)
|
||||
{
|
||||
if (math::IsNotEqualF(constants.x, _blendConstants.x) ||
|
||||
math::IsNotEqualF(constants.y, _blendConstants.y) ||
|
||||
math::IsNotEqualF(constants.z, _blendConstants.z) ||
|
||||
math::IsNotEqualF(constants.w, _blendConstants.w))
|
||||
{
|
||||
_blendConstants.x = constants.x;
|
||||
_blendConstants.y = constants.y;
|
||||
_blendConstants.z = constants.z;
|
||||
_blendConstants.w = constants.w;
|
||||
[_mtlEncoder setBlendColorRed:_blendConstants.x
|
||||
green:_blendConstants.y
|
||||
blue:_blendConstants.z
|
||||
alpha:_blendConstants.w];
|
||||
}
|
||||
_commandEncoder.setBlendColor(constants);
|
||||
}
|
||||
|
||||
void CCMTLCommandBuffer::setDepthBound(float minBounds, float maxBounds)
|
||||
@@ -310,72 +277,79 @@ void CCMTLCommandBuffer::draw(InputAssembler *ia)
|
||||
bindDescriptorSets();
|
||||
}
|
||||
|
||||
const auto indirectBuffer = ia->getIndirectBuffer();
|
||||
const auto indirectBuffer = static_cast<CCMTLBuffer *>(ia->getIndirectBuffer());
|
||||
const auto indexBuffer = static_cast<CCMTLBuffer *>(ia->getIndexBuffer());
|
||||
auto mtlEncoder = _commandEncoder.getMTLEncoder();
|
||||
if (_type == CommandBufferType::PRIMARY)
|
||||
{
|
||||
if (indirectBuffer)
|
||||
{
|
||||
const auto count = indirectBuffer->getCount();
|
||||
const auto &indirectInfos = static_cast<CCMTLBuffer *>(indirectBuffer)->getDrawInfos();
|
||||
uint count = indirectBuffer->getCount();
|
||||
const auto &drawInfos = indirectBuffer->getDrawInfos();
|
||||
_numDrawCalls += count;
|
||||
for (size_t j = 0; j < count; j++)
|
||||
for (uint i = 0; i < count; ++i)
|
||||
{
|
||||
const auto &drawInfo = drawInfos[i];
|
||||
|
||||
if (_indirectDrawSuppotred)
|
||||
{
|
||||
if (_gpuIndexBuffer.mtlBuffer)
|
||||
if (indexBuffer)
|
||||
{
|
||||
[_mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexType:_indexType
|
||||
indexBuffer:_gpuIndexBuffer.mtlBuffer
|
||||
indexBufferOffset:j * _gpuIndexBuffer.stride
|
||||
indirectBuffer:static_cast<CCMTLBuffer *>(indirectBuffer)->getMTLBuffer()
|
||||
indirectBufferOffset:j * sizeof(MTLDrawIndexedPrimitivesIndirectArguments)];
|
||||
} else
|
||||
if (drawInfo.indexCount)
|
||||
{
|
||||
[mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexType:_indexType
|
||||
indexBuffer:indexBuffer->getMTLBuffer()
|
||||
indexBufferOffset:i * indirectBuffer->getStride()
|
||||
indirectBuffer:indirectBuffer->getMTLBuffer()
|
||||
indirectBufferOffset:i * sizeof(MTLDrawIndexedPrimitivesIndirectArguments)];
|
||||
}
|
||||
}
|
||||
else if (drawInfo.vertexCount)
|
||||
{
|
||||
[_mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
indirectBuffer:static_cast<CCMTLBuffer *>(indirectBuffer)->getMTLBuffer()
|
||||
indirectBufferOffset:j * sizeof(MTLDrawIndexedPrimitivesIndirectArguments)];
|
||||
[mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
indirectBuffer:indirectBuffer->getMTLBuffer()
|
||||
indirectBufferOffset:i * sizeof(MTLDrawIndexedPrimitivesIndirectArguments)];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto &drawInfo = indirectInfos[j];
|
||||
NSUInteger offset = 0;
|
||||
offset += drawInfo.firstIndex * _gpuIndexBuffer.stride;
|
||||
offset += drawInfo.firstIndex * indirectBuffer->getStride();
|
||||
if (drawInfo.indexCount)
|
||||
{
|
||||
if (drawInfo.instanceCount == 0)
|
||||
{
|
||||
[_mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexCount:drawInfo.indexCount
|
||||
indexType:_indexType
|
||||
indexBuffer:_gpuIndexBuffer.mtlBuffer
|
||||
indexBufferOffset:offset];
|
||||
[mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexCount:drawInfo.indexCount
|
||||
indexType:_indexType
|
||||
indexBuffer:indexBuffer->getMTLBuffer()
|
||||
indexBufferOffset:offset];
|
||||
}
|
||||
else
|
||||
{
|
||||
[_mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexCount:drawInfo.indexCount
|
||||
indexType:_indexType
|
||||
indexBuffer:_gpuIndexBuffer.mtlBuffer
|
||||
indexBufferOffset:offset
|
||||
instanceCount:drawInfo.instanceCount];
|
||||
[mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexCount:drawInfo.indexCount
|
||||
indexType:_indexType
|
||||
indexBuffer:indexBuffer->getMTLBuffer()
|
||||
indexBufferOffset:offset
|
||||
instanceCount:drawInfo.instanceCount];
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (drawInfo.vertexCount)
|
||||
{
|
||||
if (drawInfo.instanceCount == 0)
|
||||
{
|
||||
[_mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
vertexStart:drawInfo.firstIndex
|
||||
vertexCount:drawInfo.vertexCount];
|
||||
[mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
vertexStart:drawInfo.firstIndex
|
||||
vertexCount:drawInfo.vertexCount];
|
||||
}
|
||||
else
|
||||
{
|
||||
[_mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
vertexStart:drawInfo.firstIndex
|
||||
vertexCount:drawInfo.vertexCount
|
||||
instanceCount:drawInfo.instanceCount];
|
||||
[mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
vertexStart:drawInfo.firstIndex
|
||||
vertexCount:drawInfo.vertexCount
|
||||
instanceCount:drawInfo.instanceCount];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -388,39 +362,39 @@ void CCMTLCommandBuffer::draw(InputAssembler *ia)
|
||||
if (drawInfo.indexCount > 0)
|
||||
{
|
||||
NSUInteger offset = 0;
|
||||
offset += drawInfo.firstIndex * _gpuIndexBuffer.stride;
|
||||
offset += drawInfo.firstIndex * indexBuffer->getStride();
|
||||
if (drawInfo.instanceCount == 0)
|
||||
{
|
||||
[_mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexCount:drawInfo.indexCount
|
||||
indexType:_indexType
|
||||
indexBuffer:_gpuIndexBuffer.mtlBuffer
|
||||
indexBufferOffset:offset];
|
||||
[mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexCount:drawInfo.indexCount
|
||||
indexType:_indexType
|
||||
indexBuffer:indexBuffer->getMTLBuffer()
|
||||
indexBufferOffset:offset];
|
||||
}
|
||||
else
|
||||
{
|
||||
[_mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexCount:drawInfo.indexCount
|
||||
indexType:_indexType
|
||||
indexBuffer:_gpuIndexBuffer.mtlBuffer
|
||||
indexBufferOffset:offset
|
||||
instanceCount:drawInfo.instanceCount];
|
||||
[mtlEncoder drawIndexedPrimitives:_mtlPrimitiveType
|
||||
indexCount:drawInfo.indexCount
|
||||
indexType:_indexType
|
||||
indexBuffer:indexBuffer->getMTLBuffer()
|
||||
indexBufferOffset:offset
|
||||
instanceCount:drawInfo.instanceCount];
|
||||
}
|
||||
}
|
||||
else
|
||||
else if (drawInfo.vertexCount)
|
||||
{
|
||||
if (drawInfo.instanceCount == 0)
|
||||
{
|
||||
[_mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
vertexStart:drawInfo.firstIndex
|
||||
vertexCount:drawInfo.vertexCount];
|
||||
[mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
vertexStart:drawInfo.firstIndex
|
||||
vertexCount:drawInfo.vertexCount];
|
||||
}
|
||||
else
|
||||
{
|
||||
[_mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
vertexStart:drawInfo.firstIndex
|
||||
vertexCount:drawInfo.vertexCount
|
||||
instanceCount:drawInfo.instanceCount];
|
||||
[mtlEncoder drawPrimitives:_mtlPrimitiveType
|
||||
vertexStart:drawInfo.firstIndex
|
||||
vertexCount:drawInfo.vertexCount
|
||||
instanceCount:drawInfo.instanceCount];
|
||||
}
|
||||
}
|
||||
_numInstances += drawInfo.instanceCount;
|
||||
@@ -500,10 +474,12 @@ void CCMTLCommandBuffer::copyBuffersToTexture(const uint8_t *const *buffers, Tex
|
||||
stagingRegion.sourceSize = {w, h, region.texExtent.depth};
|
||||
stagingRegion.destinationSlice = region.texSubres.baseArrayLayer;
|
||||
stagingRegion.destinationLevel = region.texSubres.mipLevel;
|
||||
stagingRegion.destinationOrigin = {
|
||||
stagingRegion.destinationOrigin =
|
||||
{
|
||||
static_cast<uint>(region.texOffset.x),
|
||||
static_cast<uint>(region.texOffset.y),
|
||||
static_cast<uint>(region.texOffset.z)};
|
||||
static_cast<uint>(region.texOffset.z)
|
||||
};
|
||||
totalSize += stagingRegion.sourceBytesPerImage;
|
||||
}
|
||||
|
||||
@@ -527,7 +503,8 @@ void CCMTLCommandBuffer::copyBuffersToTexture(const uint8_t *const *buffers, Tex
|
||||
bytesPerImage:bytesPerImage];
|
||||
|
||||
offset += stagingRegion.sourceBytesPerImage;
|
||||
if (convertedData != buffers[i]) {
|
||||
if (convertedData != buffers[i])
|
||||
{
|
||||
CC_FREE(convertedData);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user