Metal: Cleanup.

This commit is contained in:
Branimir Karadžić 2015-10-01 20:26:27 -07:00
parent 2e1d82b7d9
commit 2537efad37
3 changed files with 66 additions and 31 deletions

View file

@ -315,6 +315,7 @@ function exampleProject(_name)
linkoptions {
"-framework CoreFoundation",
"-framework Foundation",
"-framework Metal",
"-framework OpenGLES",
"-framework UIKit",
"-framework QuartzCore",

View file

@ -46,14 +46,37 @@ namespace bgfx { namespace mtl
id<MTLRenderCommandEncoder> renderCommandEncoderWithDescriptor( MTLRenderPassDescriptor *_renderPassDescriptor){
return [m_obj renderCommandEncoderWithDescriptor:_renderPassDescriptor];
}
id<MTLComputeCommandEncoder> computeCommandEncoder() { return [m_obj computeCommandEncoder]; }
id<MTLComputeCommandEncoder> computeCommandEncoder()
{
return [m_obj computeCommandEncoder];
}
// Scheduling and Executing Commands
void enqueue() { [m_obj enqueue]; }
void commit() { [m_obj commit]; }
void addCompletedHandler(mtlCallback _cb, void* _data) { [m_obj addCompletedHandler:^(id <MTLCommandBuffer> cmdb){ _cb(_data); }]; }
void presentDrawable(id<MTLDrawable> _drawable) { [m_obj presentDrawable:_drawable]; }
void waitUntilCompleted() { [m_obj waitUntilCompleted]; }
void enqueue()
{
[m_obj enqueue];
}
void commit()
{
[m_obj commit];
}
void addCompletedHandler(mtlCallback _cb, void* _data)
{
[m_obj addCompletedHandler:^(id <MTLCommandBuffer>){ _cb(_data); }];
}
void presentDrawable(id<MTLDrawable> _drawable)
{
[m_obj presentDrawable:_drawable];
}
void waitUntilCompleted()
{
[m_obj waitUntilCompleted];
}
MTL_CLASS_END
MTL_CLASS(CommandQueue)
@ -365,14 +388,14 @@ namespace bgfx { namespace mtl
ProgramMtl()
: m_vsh(NULL)
, m_fsh(NULL)
, m_processedUniforms(false)
, m_vshConstantBuffer(NULL)
, m_fshConstantBuffer(NULL)
, m_numPredefined(0)
, m_vshConstantBufferSize(0)
, m_vshConstantBufferAlignmentMask(0)
, m_fshConstantBufferSize(0)
, m_fshConstantBufferAlignmentMask(0)
, m_numPredefined(0)
, m_processedUniforms(false)
{
}
@ -383,22 +406,22 @@ namespace bgfx { namespace mtl
StateCacheT<RenderPipelineState> m_renderPipelineStateCache;
uint8_t m_used[Attrib::Count+1]; // dense
uint8_t m_used[Attrib::Count+1]; // dense
uint32_t m_attributes[Attrib::Count]; // sparse
uint32_t m_instanceData[BGFX_CONFIG_MAX_INSTANCE_DATA_COUNT+1];
const ShaderMtl* m_vsh;
const ShaderMtl* m_fsh;
UniformBuffer* m_vshConstantBuffer;
UniformBuffer* m_fshConstantBuffer;
bool m_processedUniforms;
uint32_t m_vshConstantBufferSize;
uint32_t m_vshConstantBufferAlignmentMask;
uint32_t m_fshConstantBufferSize;
uint32_t m_fshConstantBufferAlignmentMask;
UniformBuffer* m_fshConstantBuffer;
UniformBuffer* m_vshConstantBuffer;
PredefinedUniform m_predefined[PredefinedUniform::Count*2];
uint8_t m_numPredefined;
bool m_processedUniforms;
};
struct TextureMtl
@ -407,8 +430,8 @@ namespace bgfx { namespace mtl
: m_ptr(NULL)
, m_ptrStencil(NULL)
, m_sampler(NULL)
, m_numMips(0)
, m_flags(0)
, m_numMips(0)
{
}
@ -433,10 +456,9 @@ namespace bgfx { namespace mtl
struct FrameBufferMtl
{
FrameBufferMtl()
: //m_swapChain(NULL) //TODO: swapchain
m_denseIdx(UINT16_MAX)
, m_num(0)
: m_denseIdx(UINT16_MAX)
, m_pixelFormatHash(0)
, m_num(0)
{
m_depthHandle.idx = invalidHandle;
}
@ -455,7 +477,7 @@ namespace bgfx { namespace mtl
TextureHandle m_colorHandle[BGFX_CONFIG_MAX_FRAME_BUFFER_ATTACHMENTS-1];
TextureHandle m_depthHandle;
uint8_t m_num; // number of color handles
uint8_t m_num; // number of color handles
};

View file

@ -327,12 +327,12 @@ namespace bgfx { namespace mtl
struct RendererContextMtl : public RendererContextI
{
RendererContextMtl()
: m_numWindows(1),
m_metalLayer(NULL),
m_drawable(NULL),
m_maxAnisotropy(1),
m_rtMsaa(false),
m_backBufferPixelFormatHash(0)
: m_metalLayer(NULL)
, m_backBufferPixelFormatHash(0)
, m_maxAnisotropy(1)
, m_numWindows(1)
, m_rtMsaa(false)
, m_drawable(NULL)
{
m_fbh.idx = invalidHandle;
}
@ -686,7 +686,7 @@ namespace bgfx { namespace mtl
uint32_t length = width*height*4;
uint8_t* data = (uint8_t*)BX_ALLOC(g_allocator, length);
MTLRegion region = { 0, 0, 0, width, height, 1};
MTLRegion region = { { 0, 0, 0 }, { width, height, 1 } };
backBuffer.getBytes(data, 4*width, 0, region, 0, 0);
@ -1715,15 +1715,23 @@ namespace bgfx { namespace mtl
void BufferMtl::create(uint32_t _size, void* _data, uint16_t _flags, uint16_t _stride, bool _vertex)
{
BX_UNUSED(_flags, _stride, _vertex);
m_size = _size;
if ( NULL == _data )
if (NULL == _data)
{
m_buffer = s_renderMtl->m_device.newBufferWithLength(_size, 0);
}
else
{
m_buffer = s_renderMtl->m_device.newBufferWithBytes(_data, _size, 0);
}
}
void BufferMtl::update(uint32_t _offset, uint32_t _size, void* _data, bool _discard)
{
BX_UNUSED(_discard);
memcpy( (uint8_t*)m_buffer.contents() + _offset, _data, _size);
}
@ -1797,11 +1805,11 @@ namespace bgfx { namespace mtl
);
const bool bufferOnly = 0 != (_flags&BGFX_TEXTURE_RT_BUFFER_ONLY);
// const bool bufferOnly = 0 != (_flags&BGFX_TEXTURE_RT_BUFFER_ONLY);
const bool computeWrite = 0 != (_flags&BGFX_TEXTURE_COMPUTE_WRITE);
const bool renderTarget = 0 != (_flags&BGFX_TEXTURE_RT_MASK);
// const bool renderTarget = 0 != (_flags&BGFX_TEXTURE_RT_MASK);
const bool srgb = 0 != (_flags&BGFX_TEXTURE_SRGB) || imageContainer.m_srgb;
const uint32_t msaaQuality = bx::uint32_satsub( (_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT, 1);
// const uint32_t msaaQuality = bx::uint32_satsub( (_flags&BGFX_TEXTURE_RT_MSAA_MASK)>>BGFX_TEXTURE_RT_MSAA_SHIFT, 1);
// const DXGI_SAMPLE_DESC& msaa = s_msaa[msaaQuality];
MTLPixelFormat format = MTLPixelFormatInvalid;
@ -1868,7 +1876,7 @@ namespace bgfx { namespace mtl
data = temp;
}
MTLRegion region = { 0, 0, 0, width, height, depth};
MTLRegion region = { { 0, 0, 0 }, { width, height, depth } };
uint32_t bytesPerRow;
uint32_t bytesPerImage;
@ -1914,7 +1922,7 @@ namespace bgfx { namespace mtl
void TextureMtl::update(uint8_t _side, uint8_t _mip, const Rect& _rect, uint16_t _z, uint16_t _depth, uint16_t _pitch, const Memory* _mem)
{
MTLRegion region = { _rect.m_x, _rect.m_y, _z, _rect.m_width, _rect.m_height, _depth};
MTLRegion region = { { _rect.m_x, _rect.m_y, _z }, { _rect.m_width, _rect.m_height, _depth } };
const uint32_t bpp = getBitsPerPixel(TextureFormat::Enum(m_textureFormat) );
const uint32_t rectpitch = _rect.m_width*bpp/8;
@ -1990,6 +1998,8 @@ namespace bgfx { namespace mtl
void FrameBufferMtl::create(uint16_t _denseIdx, void* _nwh, uint32_t _width, uint32_t _height, TextureFormat::Enum _depthFormat)
{
BX_UNUSED(_denseIdx, _nwh, _width, _height, _depthFormat);
BX_WARN(false, "FrameBufferMtl::create not yet implemented");
}
@ -2012,6 +2022,8 @@ namespace bgfx { namespace mtl
void RendererContextMtl::submit(Frame* _render, ClearQuad& _clearQuad, TextVideoMemBlitter& _textVideoMemBlitter) BX_OVERRIDE
{
BX_UNUSED(_clearQuad);
m_commandBuffer = m_commandQueue.commandBuffer();
retain(m_commandBuffer); // keep alive to be useable at 'flip'
@ -2638,7 +2650,7 @@ namespace bgfx { namespace mtl
static uint32_t maxGpuLatency = 0;
static double maxGpuElapsed = 0.0f;
double elapsedGpuMs = 0.0;
// double elapsedGpuMs = 0.0;
//TODO: gputimer
/* m_gpuTimer.end();