fix metal buffer usage (#2477)

* fix metal buffer usage

* fix cmake

* synchronize the managed buffer
This commit is contained in:
coulsonwang
2020-05-25 18:28:08 +08:00
committed by GitHub
parent bd0b835095
commit 6093b65ca2
17 changed files with 239 additions and 101 deletions
+5 -3
View File
@@ -625,8 +625,8 @@ if(CC_USE_METAL)
cocos/renderer/gfx-metal/MTLCommands.mm
cocos/renderer/gfx-metal/MTLDevice.h
cocos/renderer/gfx-metal/MTLDevice.mm
cocos/renderer/gfx-metal/MTLFrameBuffer.cpp
cocos/renderer/gfx-metal/MTLFrameBuffer.h
cocos/renderer/gfx-metal/MTLFramebuffer.mm
cocos/renderer/gfx-metal/MTLFramebuffer.h
cocos/renderer/gfx-metal/MTLGPUObjects.h
cocos/renderer/gfx-metal/MTLInputAssembler.h
cocos/renderer/gfx-metal/MTLInputAssembler.mm
@@ -1010,7 +1010,9 @@ if(CC_USE_VULKAN)
else()
target_compile_definitions(cocos2d PUBLIC VK_USE_PLATFORM_XCB_KHR)
endif()
elseif(CC_USE_METAL)
target_compile_definitions(cocos2d PUBLIC USE_METAL)
endif()
target_include_directories(cocos2d PUBLIC
@@ -475,7 +475,6 @@
46BFDFF623CC573500CC5F8E /* MTLPipelineLayout.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BFDFCC23CC573200CC5F8E /* MTLPipelineLayout.cpp */; };
46BFDFF723CC573500CC5F8E /* MTLCommandBuffer.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46BFDFCD23CC573200CC5F8E /* MTLCommandBuffer.mm */; };
46BFDFF823CC573500CC5F8E /* MTLShader.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46BFDFCE23CC573200CC5F8E /* MTLShader.mm */; };
46BFDFF923CC573500CC5F8E /* MTLFrameBuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46BFDFCF23CC573200CC5F8E /* MTLFrameBuffer.cpp */; };
46BFDFFA23CC573500CC5F8E /* MTLGPUObjects.h in Headers */ = {isa = PBXBuildFile; fileRef = 46BFDFD023CC573200CC5F8E /* MTLGPUObjects.h */; };
46BFDFFB23CC573500CC5F8E /* MTLCommands.h in Headers */ = {isa = PBXBuildFile; fileRef = 46BFDFD123CC573200CC5F8E /* MTLCommands.h */; };
46BFDFFC23CC573500CC5F8E /* MTLWindow.h in Headers */ = {isa = PBXBuildFile; fileRef = 46BFDFD223CC573300CC5F8E /* MTLWindow.h */; };
@@ -934,6 +933,7 @@
EDAD7A4623FFDE040014B660 /* jsb_gfx_manual.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EDAD7A4323FFDE040014B660 /* jsb_gfx_manual.cpp */; };
EDAD7A4723FFDE040014B660 /* jsb_gfx_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = EDAD7A4423FFDE040014B660 /* jsb_gfx_manual.hpp */; };
EDAD7A4823FFDE040014B660 /* jsb_gfx_manual.hpp in Headers */ = {isa = PBXBuildFile; fileRef = EDAD7A4423FFDE040014B660 /* jsb_gfx_manual.hpp */; };
EDC3CF752475381F00C9E38B /* MTLFrameBuffer.mm in Sources */ = {isa = PBXBuildFile; fileRef = EDC3CF742475381F00C9E38B /* MTLFrameBuffer.mm */; };
EDE5DFF81C0D6B3F0014147A /* libwebsockets.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EDE5DFF71C0D6B3F0014147A /* libwebsockets.a */; };
/* End PBXBuildFile section */
@@ -1215,7 +1215,6 @@
46BFDFCC23CC573200CC5F8E /* MTLPipelineLayout.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MTLPipelineLayout.cpp; sourceTree = "<group>"; };
46BFDFCD23CC573200CC5F8E /* MTLCommandBuffer.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTLCommandBuffer.mm; sourceTree = "<group>"; };
46BFDFCE23CC573200CC5F8E /* MTLShader.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTLShader.mm; sourceTree = "<group>"; };
46BFDFCF23CC573200CC5F8E /* MTLFrameBuffer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MTLFrameBuffer.cpp; sourceTree = "<group>"; };
46BFDFD023CC573200CC5F8E /* MTLGPUObjects.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTLGPUObjects.h; sourceTree = "<group>"; };
46BFDFD123CC573200CC5F8E /* MTLCommands.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTLCommands.h; sourceTree = "<group>"; };
46BFDFD223CC573300CC5F8E /* MTLWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTLWindow.h; sourceTree = "<group>"; };
@@ -1508,6 +1507,7 @@
EDAD7A3E23FFDDEC0014B660 /* jsb_gfx_auto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_gfx_auto.cpp; sourceTree = "<group>"; };
EDAD7A4323FFDE040014B660 /* jsb_gfx_manual.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsb_gfx_manual.cpp; sourceTree = "<group>"; };
EDAD7A4423FFDE040014B660 /* jsb_gfx_manual.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsb_gfx_manual.hpp; sourceTree = "<group>"; };
EDC3CF742475381F00C9E38B /* MTLFrameBuffer.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MTLFrameBuffer.mm; sourceTree = "<group>"; };
EDE5DFF71C0D6B3F0014147A /* libwebsockets.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libwebsockets.a; path = ../external/ios/libs/libwebsockets.a; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -2390,7 +2390,7 @@
46BFDFEB23CC573400CC5F8E /* MTLCommands.mm */,
46BFDFD523CC573300CC5F8E /* MTLDevice.h */,
46BFDFE723CC573400CC5F8E /* MTLDevice.mm */,
46BFDFCF23CC573200CC5F8E /* MTLFrameBuffer.cpp */,
EDC3CF742475381F00C9E38B /* MTLFrameBuffer.mm */,
46BFDFEC23CC573400CC5F8E /* MTLFrameBuffer.h */,
46BFDFD023CC573200CC5F8E /* MTLGPUObjects.h */,
46BFDFD623CC573300CC5F8E /* MTLInputAssembler.h */,
@@ -3203,7 +3203,6 @@
469304262046AE06004A3D6C /* jsb_conversions.cpp in Sources */,
469304582046AE06004A3D6C /* EventDispatcher.cpp in Sources */,
1A52DAF8205BB81400350EE3 /* CCThreadPool.cpp in Sources */,
46BFDFF923CC573500CC5F8E /* MTLFrameBuffer.cpp in Sources */,
BA06A68023583DBD007740C5 /* AllocatedObj.cc in Sources */,
BA06A6E223583DBD007740C5 /* GFXContext.cc in Sources */,
BA06A6B423583DBD007740C5 /* GFXTextureView.cc in Sources */,
@@ -3253,6 +3252,7 @@
1AAAC873205CB647005321B9 /* jsb_cocos2dx_audioengine_auto.cpp in Sources */,
46930468204FE20F004A3D6C /* CCLog.cpp in Sources */,
1A28FF991F20AFAB007A1D9D /* SRSecurityPolicy.m in Sources */,
EDC3CF752475381F00C9E38B /* MTLFrameBuffer.mm in Sources */,
46BFE01C23CC573500CC5F8E /* MTLRenderPass.mm in Sources */,
BA06A6CC23583DBD007740C5 /* GFXInputAssembler.cc in Sources */,
BA06A6DC23583DBD007740C5 /* GFXBindingLayout.cc in Sources */,
@@ -30,10 +30,13 @@ else()
add_library(${TARGET_NAME} SHARED ${HEADER_FILES} ${ALL_FILES})
endif()
target_compile_definitions(${TARGET_NAME} PRIVATE USE_METAL)
find_library(METAL_LIBRARY Metal)
find_library(METALKIT_LIBRARY MetalKit)
target_link_libraries(${TARGET_NAME} Core ${METAL_LIBRARY} ${METALKIT_LIBRARY})
set(glslang_libs_name glslang OGLCompiler OSDependent SPIRV)
set(spirv-cross_libs_name spirv-cross-core spirv-cross-glsl spirv-cross-msl)
target_link_libraries(${TARGET_NAME} Core ${METAL_LIBRARY} ${METALKIT_LIBRARY} ${glslang_libs_name} ${spirv-cross_libs_name})
target_include_directories(${TARGET_NAME} PUBLIC ${COCOS_EXTERNAL_PATH}/mac/include)
set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME CCGFXMetal)
set_target_properties(${TARGET_NAME} PROPERTIES OUTPUT_NAME_DEBUG CCGFXMetal_d)
@@ -2,6 +2,7 @@
#import <Metal/MTLBuffer.h>
#import <Metal/MTLStageInputOutputDescriptor.h>
#import <Metal/MTLRenderCommandEncoder.h>
NS_CC_BEGIN
@@ -20,15 +21,21 @@ public:
CC_INLINE uint8_t* getTransferBuffer() const { return _transferBuffer; }
CC_INLINE MTLIndexType getIndexType() const { return _indexType; }
CC_INLINE const GFXDrawInfoList& getIndirects() const { return _indirects; }
void encodeBuffer(id<MTLRenderCommandEncoder> encoder, uint offset, uint binding, GFXShaderType stages);
private:
void resizeBuffer(uint8_t**, uint, uint);
bool createMTLBuffer(uint size, GFXMemoryUsage usage);
id<MTLBuffer> _mtlBuffer = nullptr;
uint8_t* _transferBuffer = nullptr;
MTLIndexType _indexType = MTLIndexTypeUInt16;
MTLResourceOptions _mtlResourceOptions = MTLResourceStorageModePrivate;
GFXDrawInfoList _indirects;
// to store vertex and ubo data when size is small, otherwise use MTLBuffer instead.
bool _useOptimizedBufferEncoder = false;
uint8_t* _bufferBytes = nullptr;
};
NS_CC_END
+103 -33
View File
@@ -1,22 +1,13 @@
#include "MTLStd.h"
#include "MTLBuffer.h"
#include "MTLDevice.h"
#include "MTLUtils.h"
#import <Metal/Metal.h>
NS_CC_BEGIN
namespace
{
//FIXME: not sure if it is correct to map like this
MTLResourceOptions toMTLResourseOption(GFXMemoryUsage usage)
{
if (usage == GFXMemoryUsage::DEVICE)
return MTLResourceStorageModePrivate;
else
return MTLResourceStorageModeShared;
}
namespace {
#define MINIMUMR_REQUIRED_SIZE_4KB 4096
}
CCMTLBuffer::CCMTLBuffer(GFXDevice* device) : GFXBuffer(device) {}
CCMTLBuffer::~CCMTLBuffer()
@@ -58,21 +49,24 @@ bool CCMTLBuffer::initialize(const GFXBufferInfo& info)
}
if (_usage & GFXBufferUsage::VERTEX ||
_usage & GFXBufferUsage::INDEX ||
_usage & GFXBufferUsage::UNIFORM)
{
_mtlResourceOptions = toMTLResourseOption(info.memUsage);
if (_size > 0)
//for single-use data smaller than 4 KB, use setVertexBytes:length:atIndex: instead
//see more detail at https://developer.apple.com/documentation/metal/mtlrendercommandencoder/1515846-setvertexbytes?language=objc
if (_size < MINIMUMR_REQUIRED_SIZE_4KB)
{
_mtlBuffer = [id<MTLDevice>(((CCMTLDevice*)_device)->getMTLDevice() ) newBufferWithLength:_size
options:_mtlResourceOptions];
if (_mtlBuffer == nil)
{
_status = GFXStatus::FAILED;
CCASSERT(false, "Failed to create MTLBuffer.");
return false;
}
_useOptimizedBufferEncoder = true;
_bufferBytes = static_cast<uint8_t*>(CC_MALLOC(_size));
_device->getMemoryStatus().bufferSize += _size;
}
else
{
createMTLBuffer(_size, _memUsage);
}
}
else if(_usage & GFXBufferUsage::INDEX)
{
createMTLBuffer(_size, _memUsage);
}
else if (_usage & GFXBufferUsageBit::INDIRECT)
{
@@ -101,6 +95,23 @@ bool CCMTLBuffer::initialize(const GFXBufferInfo& info)
return true;
}
bool CCMTLBuffer::createMTLBuffer(uint size, GFXMemoryUsage usage)
{
if(_mtlBuffer)
[_mtlBuffer release];
_mtlResourceOptions = mu::toMTLResourseOption(usage);
_mtlBuffer = [id<MTLDevice>(((CCMTLDevice*)_device)->getMTLDevice() ) newBufferWithLength:size
options:_mtlResourceOptions];
if (_mtlBuffer == nil)
{
_status = GFXStatus::FAILED;
CCASSERT(false, "Failed to create MTLBuffer.");
return false;
}
return true;
}
void CCMTLBuffer::destroy()
{
if (_mtlBuffer)
@@ -122,6 +133,13 @@ void CCMTLBuffer::destroy()
_device->getMemoryStatus().bufferSize -= _size;
_buffer = nullptr;
}
if(_bufferBytes)
{
CC_FREE(_bufferBytes);
_device->getMemoryStatus().bufferSize -= _size;
_bufferBytes = nullptr;
}
_status = GFXStatus::UNREADY;
}
@@ -134,15 +152,13 @@ void CCMTLBuffer::resize(uint size)
_usage & GFXBufferUsage::INDEX ||
_usage & GFXBufferUsage::UNIFORM)
{
if (_mtlBuffer) [_mtlBuffer release];
_mtlBuffer = [id<MTLDevice>(((CCMTLDevice*)_device)->getMTLDevice() ) newBufferWithLength:size
options:_mtlResourceOptions];
if (!_mtlBuffer)
if (_useOptimizedBufferEncoder)
{
_status = GFXStatus::FAILED;
CC_LOG_ERROR("Failed to resize buffer for metal buffer.");
return;
resizeBuffer(&_bufferBytes, size, _size);
}
else
{
createMTLBuffer(size, _memUsage);
}
}
@@ -191,11 +207,23 @@ void CCMTLBuffer::update(void* buffer, uint offset, uint size)
memcpy((uint8_t*)_indirects.data() + offset, buffer, size);
return;
}
if(_bufferBytes)
memcpy(_bufferBytes + offset, buffer, size);
if(_mtlBuffer)
{
uint8_t* dst = (uint8_t*)(_mtlBuffer.contents) + offset;
memcpy(dst, buffer, size);
if(_mtlResourceOptions == MTLResourceStorageModePrivate)
{
static_cast<CCMTLDevice*>(_device)->blitBuffer(buffer, offset, size, _mtlBuffer);
}
else
{
uint8_t* dst = (uint8_t*)(_mtlBuffer.contents) + offset;
memcpy(dst, buffer, size);
if(_mtlResourceOptions == MTLResourceStorageModeManaged)
[_mtlBuffer didModifyRange:NSMakeRange(0, _size)]; // Synchronize the managed buffer.
}
return;
}
@@ -206,4 +234,46 @@ void CCMTLBuffer::update(void* buffer, uint offset, uint size)
}
}
void CCMTLBuffer::encodeBuffer(id<MTLRenderCommandEncoder> encoder, uint offset, uint binding, GFXShaderType stages)
{
if(encoder == nil)
{
CC_LOG_ERROR("CCMTLBuffer::encodeBuffer: MTLRenderCommandEncoder should not be nil.");
return;
}
if(stages & GFXShaderType::VERTEX)
{
if(_useOptimizedBufferEncoder)
{
[encoder setVertexBytes:_bufferBytes
length:_size
atIndex:binding];
}
else
{
[encoder setVertexBuffer:_mtlBuffer
offset:offset
atIndex:binding];
}
}
if(stages & GFXShaderType::FRAGMENT)
{
if(_useOptimizedBufferEncoder)
{
[encoder setFragmentBytes:_bufferBytes
length:_size
atIndex:binding];
}
else
{
[encoder setFragmentBuffer:_mtlBuffer
offset:offset
atIndex:binding];
}
}
}
NS_CC_END
@@ -83,7 +83,7 @@ void CCMTLCommandBuffer::beginRenderPass(GFXFramebuffer* fbo, const GFXRect& ren
_isInRenderPass = true;
CCMTLCmdBeginRenderPass* cmd = _MTLCommandAllocator->_beginRenderPassCmdPool.alloc();
cmd->frameBuffer = (CCMTLFrameBuffer*)fbo;
cmd->frameBuffer = (CCMTLFramebuffer*)fbo;
cmd->clearFlags = clear_flags;
cmd->renderArea = render_area;
cmd->clearStencil = stencil;
@@ -4,7 +4,7 @@
NS_CC_BEGIN
class CCMTLFrameBuffer;
class CCMTLFramebuffer;
struct CCMTLGPUPipelineState;
class CCMTLInputAssembler;
class CCMTLBindingLayout;
@@ -30,7 +30,7 @@ class CCMTLCmdBeginRenderPass : public GFXCmd
public:
GFXRect renderArea;
GFXClearFlags clearFlags = GFXClearFlags::NONE;
CCMTLFrameBuffer* frameBuffer = nullptr;
CCMTLFramebuffer* frameBuffer = nullptr;
uint numOfClearColor = 0;
vector<GFXColor>::type clearColors;
float clearDepth = 1.f;
@@ -31,6 +31,7 @@ public:
virtual GFXPipelineState* createPipelineState(const GFXPipelineStateInfo& info) override;
virtual GFXPipelineLayout* createPipelineLayout(const GFXPipelineLayoutInfo& info) override;
virtual void copyBuffersToTexture(const GFXDataArray& buffers, GFXTexture* dst, const GFXBufferTextureCopyList& regions) override;
virtual void blitBuffer(void* srcBuffer, uint offset, uint size, void* dstBuffer);
CC_INLINE void* getMTKView() const { return _mtkView; }
CC_INLINE void* getMTLDevice() const { return _mtlDevice; }
@@ -47,6 +48,7 @@ private:
uint _maxSamplerUnits = 0;
uint _maxColorRenderTargets = 0;
bool _icbSuppored = false;
void* _blitedBuffer = nullptr;
};
NS_CC_END
+32 -4
View File
@@ -16,8 +16,10 @@
#include "MTLTexture.h"
#include "MTLTextureView.h"
#include "MTLSampler.h"
#import <MetalKit/MTKView.h>
#include "MTLUtils.h"
#include <platform/mac/CCView.h>
#import <MetalKit/MTKView.h>
NS_CC_BEGIN
@@ -38,7 +40,7 @@ bool CCMTLDevice::initialize(const GFXDeviceInfo& info)
_mtkView = (MTKView*)_windowHandle;
_mtlDevice = ((MTKView*)_mtkView).device;
GFXWindowInfo window_info;
window_info.isOffscreen = false;
_window = createWindow(window_info);
@@ -51,7 +53,6 @@ bool CCMTLDevice::initialize(const GFXDeviceInfo& info)
_cmdAllocator = createCommandAllocator(cmd_alloc_info);
_minClipZ = 0;
_depthBits = 24;
_stencilBits = 8;
//TODO: other information
@@ -246,7 +247,7 @@ GFXRenderPass* CCMTLDevice::createRenderPass(const GFXRenderPassInfo& info)
GFXFramebuffer* CCMTLDevice::createFramebuffer(const GFXFramebufferInfo& info)
{
auto frameBuffer = CC_NEW(CCMTLFrameBuffer(this) );
auto frameBuffer = CC_NEW(CCMTLFramebuffer(this) );
if (frameBuffer && frameBuffer->initialize(info) )
return frameBuffer;
@@ -289,4 +290,31 @@ void CCMTLDevice::copyBuffersToTexture(const GFXDataArray& buffers, GFXTexture*
static_cast<CCMTLTexture*>(dst)->update(buffers.datas.data(), regions);
}
void CCMTLDevice::blitBuffer(void* srcData, uint offset, uint size, void* dstBuffer)
{
id<MTLBuffer> sourceBuffer = id<MTLBuffer>(_blitedBuffer);
if(sourceBuffer == nil || sourceBuffer.allocatedSize < size)
{
if(sourceBuffer)
[sourceBuffer release];
sourceBuffer = [id<MTLDevice>(_mtlDevice) newBufferWithBytes:srcData
length:size
options:MTLResourceStorageModeShared];
}
// Create a command buffer for GPU work.
id <MTLCommandBuffer> commandBuffer = [static_cast<View*>(_mtkView).mtlCommandQueue commandBuffer];
// Encode a blit pass to copy data from the source buffer to the private buffer.
id <MTLBlitCommandEncoder> blitCommandEncoder = [commandBuffer blitCommandEncoder];
[blitCommandEncoder copyFromBuffer:sourceBuffer
sourceOffset:0
toBuffer:id<MTLBuffer>(dstBuffer)
destinationOffset:offset
size:size];
[blitCommandEncoder endEncoding];
[commandBuffer commit];
}
NS_CC_END
@@ -1,25 +0,0 @@
#include "MTLStd.h"
#include "MTLFrameBuffer.h"
NS_CC_BEGIN
CCMTLFrameBuffer::CCMTLFrameBuffer(GFXDevice* device) : GFXFramebuffer(device) {}
CCMTLFrameBuffer::~CCMTLFrameBuffer() { destroy(); }
bool CCMTLFrameBuffer::initialize(const GFXFramebufferInfo& info)
{
_renderPass = info.renderPass;
_colorViews = info.colorViews;
_depthStencilView = info.depthStencilView;
_isOffscreen = info.isOffscreen;
_status = GFXStatus::SUCCESS;
return true;
}
void CCMTLFrameBuffer::destroy()
{
_status = GFXStatus::UNREADY;
}
NS_CC_END
@@ -2,11 +2,11 @@
NS_CC_BEGIN
class CCMTLFrameBuffer : public GFXFramebuffer
class CCMTLFramebuffer : public GFXFramebuffer
{
public:
CCMTLFrameBuffer(GFXDevice* device);
~CCMTLFrameBuffer();
CCMTLFramebuffer(GFXDevice* device);
~CCMTLFramebuffer();
virtual bool initialize(const GFXFramebufferInfo& info) override;
virtual void destroy() override;
@@ -0,0 +1,44 @@
#include "MTLStd.h"
#include "MTLFrameBuffer.h"
#include "MTLRenderPass.h"
#include "MTLTextureView.h"
NS_CC_BEGIN
CCMTLFramebuffer::CCMTLFramebuffer(GFXDevice* device) : GFXFramebuffer(device) {}
CCMTLFramebuffer::~CCMTLFramebuffer() { destroy(); }
bool CCMTLFramebuffer::initialize(const GFXFramebufferInfo& info)
{
_renderPass = info.renderPass;
_colorViews = info.colorViews;
_depthStencilView = info.depthStencilView;
_isOffscreen = info.isOffscreen;
if(_isOffscreen)
{
auto* mtlRenderPass = static_cast<CCMTLRenderPass*>(_renderPass);
size_t slot = 0;
for (const auto& colorView : info.colorViews) {
id<MTLTexture> texture = static_cast<CCMTLTextureView*>(colorView)->getMTLTexture();
mtlRenderPass->setColorAttachment(texture, slot);
}
if(_depthStencilView)
{
id<MTLTexture> texture = static_cast<CCMTLTextureView*>(_depthStencilView)->getMTLTexture();
mtlRenderPass->setDepthStencilAttachment(texture);
}
}
_status = GFXStatus::SUCCESS;
return true;
}
void CCMTLFramebuffer::destroy()
{
_status = GFXStatus::UNREADY;
}
NS_CC_END
+5 -13
View File
@@ -180,12 +180,11 @@ void CCMTLQueue::executeCommands(const CCMTLCommandPackage* commandPackage, id<M
for(const auto& binding : cmd->bindingLayout->getBindingUnits())
{
if(binding.buffer)
static_cast<CCMTLBuffer*>(binding.buffer)->encodeBuffer(encoder, 0, binding.binding, binding.shaderStages);
if(binding.shaderStages & GFXShaderType::VERTEX)
{
if(binding.buffer)
[encoder setVertexBuffer:static_cast<CCMTLBuffer*>(binding.buffer)->getMTLBuffer()
offset:0 atIndex:binding.binding];
if(binding.texView)
[encoder setVertexTexture:static_cast<CCMTLTextureView*>(binding.texView)->getMTLTexture()
atIndex:binding.binding];
@@ -197,10 +196,6 @@ void CCMTLQueue::executeCommands(const CCMTLCommandPackage* commandPackage, id<M
if(binding.shaderStages & GFXShaderType::FRAGMENT)
{
if(binding.buffer)
[encoder setFragmentBuffer:static_cast<CCMTLBuffer*>(binding.buffer)->getMTLBuffer()
offset:0 atIndex:binding.binding];
if(binding.texView)
[encoder setFragmentTexture:static_cast<CCMTLTextureView*>(binding.texView)->getMTLTexture()
atIndex:binding.binding];
@@ -228,10 +223,7 @@ void CCMTLQueue::executeCommands(const CCMTLCommandPackage* commandPackage, id<M
{
auto index = std::get<0>(bindingInfo);
auto stream = std::get<1>(bindingInfo);
id<MTLBuffer> vertexBuffer = static_cast<CCMTLBuffer*>(inputAssembler->_vertexBuffers[stream])->getMTLBuffer();
[encoder setVertexBuffer:vertexBuffer
offset:0
atIndex:index];
static_cast<CCMTLBuffer*>(inputAssembler->_vertexBuffers[stream])->encodeBuffer(encoder, 0, index, GFXShaderType::VERTEX);
}
}
@@ -245,7 +237,7 @@ void CCMTLQueue::executeCommands(const CCMTLCommandPackage* commandPackage, id<M
auto indirectBuffer = inputAssembler->getIndirectBuffer();
if (!indirectBuffer)
{
if (mtlIndexBuffer && cmd->drawInfo.indexCount >= 0)
if (cmd->drawInfo.indexCount > 0)
{
NSUInteger offset = 0;
offset += cmd->drawInfo.firstIndex * inputAssembler->getIndexBuffer()->getStride();
+1 -2
View File
@@ -27,7 +27,7 @@ bool CCMTLShader::initialize(const GFXShaderInfo& info)
}
_status = GFXStatus::SUCCESS;
CC_LOG_INFO("%s compile succeed.", _name.c_str());
return true;
}
@@ -104,7 +104,6 @@ bool CCMTLShader::createMTLFunction(const GFXShaderStage& stage)
_fragMtlShader = mtlShader;
}
#endif
return true;
}
+10 -9
View File
@@ -198,14 +198,15 @@ void CCMTLTexture::update(uint8_t* const* datas, const GFXBufferTextureCopyList&
const auto& region = regions[i];
w = region.texExtent.width;
h = region.texExtent.height;
uint8_t* buffer = datas[n];
uint8_t* convertedData = convertData(buffer, w * h, _format);
MTLRegion mtlRegion = { {(uint)region.texOffset.x, (uint)region.texOffset.y, (uint)region.texOffset.z}, {w, h, 1} };
[_mtlTexture replaceRegion:mtlRegion
mipmapLevel:region.texSubres.mipLevel
withBytes:convertedData
bytesPerRow:GFX_FORMAT_INFOS[(uint)_convertedFormat].size * w];
mipmapLevel:region.texSubres.mipLevel
withBytes:convertedData
bytesPerRow:GFX_FORMAT_INFOS[(uint)_convertedFormat].size * w];
if (convertedData != datas[n])
CC_FREE(convertedData);
n++;
@@ -224,11 +225,11 @@ void CCMTLTexture::update(uint8_t* const* datas, const GFXBufferTextureCopyList&
uint8_t* convertedData = convertData(buffer, w * h, _format);
MTLRegion mtlRegion = { {(uint)region.texOffset.x, (uint)region.texOffset.y, (uint)region.texOffset.z}, {w, h, 1} };
[_mtlTexture replaceRegion:mtlRegion
mipmapLevel:region.texSubres.mipLevel
slice:layer
withBytes:convertedData
bytesPerRow:GFX_FORMAT_INFOS[(uint)_convertedFormat].size * w
bytesPerImage:0];
mipmapLevel:region.texSubres.mipLevel
slice:layer
withBytes:convertedData
bytesPerRow:GFX_FORMAT_INFOS[(uint)_convertedFormat].size * w
bytesPerImage:0];
if (convertedData != datas[n++])
CC_FREE(convertedData);
}
@@ -12,6 +12,7 @@ NS_CC_BEGIN
namespace mu
{
MTLResourceOptions toMTLResourseOption(GFXMemoryUsage usage);
MTLLoadAction toMTLLoadAction(GFXLoadOp op);
MTLStoreAction toMTLStoreAction(GFXStoreOp op);
MTLClearColor toMTLClearColor(const GFXColor& clearColor);
@@ -231,6 +231,20 @@ namespace {
namespace mu
{
MTLResourceOptions toMTLResourseOption(GFXMemoryUsage usage)
{
if (usage & GFXMemoryUsage::HOST && usage & GFXMemoryUsage::DEVICE)
return MTLResourceStorageModeShared;
else if(usage & GFXMemoryUsage::DEVICE)
return MTLResourceStorageModePrivate;
else
#if (CC_PLATFORM == CC_PLATFORM_MAC_IOS)
return MTLResourceStorageModeShared;
#else
return MTLResourceStorageModeManaged;
#endif
}
MTLLoadAction toMTLLoadAction(GFXLoadOp op)
{
switch (op) {