From 3ae4a0f76ba28e0cf3ef83ca8c464c4261318dc6 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Wed, 29 Jul 2020 18:51:21 -0300 Subject: [PATCH] metal: post merge compilation fixes --- src/modules/graphics/Buffer.h | 1 + src/modules/graphics/Shader.cpp | 2 +- src/modules/graphics/metal/Buffer.h | 3 +- src/modules/graphics/metal/Buffer.mm | 4 +- src/modules/graphics/metal/Graphics.h | 10 ++-- src/modules/graphics/metal/Graphics.mm | 47 +++++++++-------- src/modules/graphics/metal/Shader.h | 3 +- src/modules/graphics/metal/Shader.mm | 70 ++++++++++++++------------ src/modules/graphics/vertex.h | 1 + 9 files changed, 73 insertions(+), 68 deletions(-) diff --git a/src/modules/graphics/Buffer.h b/src/modules/graphics/Buffer.h index 36f684702..f489e69c2 100644 --- a/src/modules/graphics/Buffer.h +++ b/src/modules/graphics/Buffer.h @@ -60,6 +60,7 @@ public: TYPEFLAG_NONE = 0, TYPEFLAG_VERTEX = 1 << BUFFERTYPE_VERTEX, TYPEFLAG_INDEX = 1 << BUFFERTYPE_INDEX, + TYPEFLAG_UNIFORM = 1 << BUFFERTYPE_UNIFORM, TYPEFLAG_TEXEL = 1 << BUFFERTYPE_TEXEL, }; diff --git a/src/modules/graphics/Shader.cpp b/src/modules/graphics/Shader.cpp index 3b2167467..6220989f7 100644 --- a/src/modules/graphics/Shader.cpp +++ b/src/modules/graphics/Shader.cpp @@ -453,7 +453,7 @@ std::string Shader::createShaderStageCode(Graphics *gfx, ShaderStage::StageType if (info.language == LANGUAGE_GLSL4 && !features[Graphics::FEATURE_GLSL4]) throw love::Exception("GLSL 4 shaders are not supported on this system."); - bool gles = gfx->getRenderer() == Graphics::RENDERER_OPENGLES; + bool gles = gfx->usesGLSLES(); bool glsl1on3 = info.language == LANGUAGE_GLSL1 && features[Graphics::FEATURE_GLSL3]; Language lang = info.language; diff --git a/src/modules/graphics/metal/Buffer.h b/src/modules/graphics/metal/Buffer.h index 1447487e3..95ee76aa8 100644 --- a/src/modules/graphics/metal/Buffer.h +++ b/src/modules/graphics/metal/Buffer.h @@ -34,7 +34,7 @@ class Buffer final : public love::graphics::Buffer { public: - Buffer(id device, size_t size, const void *data, BufferType type, vertex::Usage usage, uint32 mapflags); + Buffer(love::graphics::Graphics *gfx, id device, const Settings &settings, const std::vector &format, const void *data, size_t size, size_t arraylength); virtual ~Buffer(); void *map() override; @@ -42,6 +42,7 @@ public: void setMappedRangeModified(size_t offset, size_t size) override; void fill(size_t offset, size_t size, const void *data) override; ptrdiff_t getHandle() const override { return (ptrdiff_t) buffer; } + ptrdiff_t getTexelBufferHandle() const override { return 0; } void copyTo(size_t offset, size_t size, love::graphics::Buffer *other, size_t otheroffset) override; diff --git a/src/modules/graphics/metal/Buffer.mm b/src/modules/graphics/metal/Buffer.mm index 6e81276b5..3a2b11999 100644 --- a/src/modules/graphics/metal/Buffer.mm +++ b/src/modules/graphics/metal/Buffer.mm @@ -27,8 +27,8 @@ namespace graphics namespace metal { -Buffer::Buffer(id device, size_t size, const void *data, BufferType type, vertex::Usage usage, uint32 mapflags) - : love::graphics::Buffer(size, type, usage, mapflags) +Buffer::Buffer(love::graphics::Graphics *gfx, id device, const Settings &settings, const std::vector &format, const void *data, size_t size, size_t arraylength) + : love::graphics::Buffer(gfx, settings, format, size, arraylength) , mappedRange() { @autoreleasepool { MTLResourceOptions opts = MTLResourceStorageModeManaged; diff --git a/src/modules/graphics/metal/Graphics.h b/src/modules/graphics/metal/Graphics.h index 76272a9ab..228fc6986 100644 --- a/src/modules/graphics/metal/Graphics.h +++ b/src/modules/graphics/metal/Graphics.h @@ -46,7 +46,7 @@ public: const char *getName() const override { return "love.graphics.metal"; } love::graphics::Texture *newTexture(const Texture::Settings &settings, const Texture::Slices *data = nullptr) override; - love::graphics::Buffer *newBuffer(size_t size, const void *data, BufferType type, vertex::Usage usage, uint32 mapflags) override; + love::graphics::Buffer *newBuffer(const Buffer::Settings &settings, const std::vector &format, const void *data, size_t size, size_t arraylength) override; void setViewportSize(int width, int height, int pixelwidth, int pixelheight) override; bool setMode(void *context, int width, int height, int pixelwidth, int pixelheight, bool windowhasstencil) override; @@ -56,7 +56,7 @@ public: void draw(const DrawCommand &cmd) override; void draw(const DrawIndexedCommand &cmd) override; - void drawQuads(int start, int count, const vertex::Attributes &attributes, const vertex::BufferBindings &buffers, love::graphics::Texture *texture) override; + void drawQuads(int start, int count, const VertexAttributes &attributes, const BufferBindings &buffers, love::graphics::Texture *texture) override; void clear(OptionalColorf color, OptionalInt stencil, OptionalDouble depth) override; void clear(const std::vector &colors, OptionalInt stencil, OptionalDouble depth) override; @@ -77,7 +77,7 @@ public: void setDepthMode(CompareMode compare, bool write) override; - void setFrontFaceWinding(vertex::Winding winding) override; + void setFrontFaceWinding(Winding winding) override; void setColorMask(ColorChannelMask mask) override; @@ -93,8 +93,6 @@ public: bool usesGLSLES() const override; RendererInfo getRendererInfo() const override; - Shader::Language getShaderLanguageTarget() const override; - id useCommandBuffer(); id getCommandBuffer() const { return commandBuffer; } void submitCommandBuffer(); @@ -170,7 +168,7 @@ private: void endPass(); id getCachedDepthStencilState(const DepthState &depth, const StencilState &stencil); - void applyRenderState(id renderEncoder, const vertex::Attributes &attributes); + void applyRenderState(id renderEncoder, const VertexAttributes &attributes); void applyShaderUniforms(id renderEncoder, Shader *shader); id commandQueue; diff --git a/src/modules/graphics/metal/Graphics.mm b/src/modules/graphics/metal/Graphics.mm index 94c6854a3..685a41c1f 100644 --- a/src/modules/graphics/metal/Graphics.mm +++ b/src/modules/graphics/metal/Graphics.mm @@ -155,10 +155,12 @@ Graphics::Graphics() initCapabilities(); - uniformBuffer = CreateStreamBuffer(device, BUFFER_UNIFORM, 1024 * 1024 * 1); + uniformBuffer = CreateStreamBuffer(device, BUFFERTYPE_UNIFORM, 1024 * 1024 * 1); float defaultAttributes[4] = {0.0f, 0.0f, 0.0f, 1.0f}; - defaultAttributesBuffer = newBuffer(sizeof(float) * 4, defaultAttributes, BUFFER_VERTEX, vertex::USAGE_STATIC, 0); + Buffer::Settings attribsettings(Buffer::TYPEFLAG_VERTEX, 0, BUFFERUSAGE_STATIC); + std::vector dataformat = {{"Default", DATAFORMAT_FLOAT_VEC4, 0}}; + defaultAttributesBuffer = newBuffer(attribsettings, dataformat, defaultAttributes, sizeof(float) * 4, 0); uint8 defaultpixel[] = {255, 255, 255, 255}; for (int i = 0; i < TEXTURE_MAX_ENUM; i++) @@ -234,9 +236,9 @@ love::graphics::Shader *Graphics::newShaderInternal(love::graphics::ShaderStage return new Shader(device, vertex, pixel); } -love::graphics::Buffer *Graphics::newBuffer(size_t size, const void *data, BufferType type, vertex::Usage usage, uint32 mapflags) +love::graphics::Buffer *Graphics::newBuffer(const Buffer::Settings &settings, const std::vector &format, const void *data, size_t size, size_t arraylength) { - return new Buffer(device, size, data, type, usage, mapflags); + return new Buffer(this, device, settings, format, data, size, arraylength); } void Graphics::setViewportSize(int width, int height, int pixelwidth, int pixelheight) @@ -277,9 +279,9 @@ bool Graphics::setMode(void *context, int width, int height, int pixelwidth, int { // Initial sizes that should be good enough for most cases. It will // resize to fit if needed, later. - batchedDrawState.vb[0] = CreateStreamBuffer(device, BUFFER_VERTEX, 1024 * 1024 * 1); - batchedDrawState.vb[1] = CreateStreamBuffer(device, BUFFER_VERTEX, 256 * 1024 * 1); - batchedDrawState.indexBuffer = CreateStreamBuffer(device, BUFFER_INDEX, sizeof(uint16) * LOVE_UINT16_MAX); + batchedDrawState.vb[0] = CreateStreamBuffer(device, BUFFERTYPE_VERTEX, 1024 * 1024 * 1); + batchedDrawState.vb[1] = CreateStreamBuffer(device, BUFFERTYPE_VERTEX, 256 * 1024 * 1); + batchedDrawState.indexBuffer = CreateStreamBuffer(device, BUFFERTYPE_INDEX, sizeof(uint16) * LOVE_UINT16_MAX); } createQuadIndexBuffer(); @@ -287,16 +289,16 @@ bool Graphics::setMode(void *context, int width, int height, int pixelwidth, int // Restore the graphics state. restoreState(states.back()); - int gammacorrect = isGammaCorrect() ? 1 : 0; - Shader::Language target = getShaderLanguageTarget(); - // We always need a default shader. for (int i = 0; i < Shader::STANDARD_MAX_ENUM; i++) { + auto stype = (Shader::StandardShader) i; if (!Shader::standardShaders[i]) { - const auto &code = defaultShaderCode[i][target][gammacorrect]; - Shader::standardShaders[i] = love::graphics::Graphics::newShader(code.source[ShaderStage::STAGE_VERTEX], code.source[ShaderStage::STAGE_PIXEL]); + std::vector stages; + stages.push_back(Shader::getDefaultCode(stype, ShaderStage::STAGE_VERTEX)); + stages.push_back(Shader::getDefaultCode(stype, ShaderStage::STAGE_PIXEL)); + Shader::standardShaders[i] = newShader(stages); } } @@ -486,7 +488,7 @@ id Graphics::getCachedDepthStencilState(const DepthState & return mtlstate; } -void Graphics::applyRenderState(id encoder, const vertex::Attributes &attributes) +void Graphics::applyRenderState(id encoder, const VertexAttributes &attributes) { const uint32 pipelineStateBits = STATEBIT_SHADER | STATEBIT_BLEND | STATEBIT_COLORMASK; @@ -539,7 +541,7 @@ void Graphics::applyRenderState(id encoder, const verte if (dirtyState & STATEBIT_FACEWINDING) { - auto winding = state.winding == vertex::WINDING_CCW ? MTLWindingCounterClockwise : MTLWindingClockwise; + auto winding = state.winding == WINDING_CCW ? MTLWindingCounterClockwise : MTLWindingClockwise; [encoder setFrontFacingWinding:winding]; } @@ -656,7 +658,7 @@ void Graphics::applyShaderUniforms(id renderEncoder, lo { size_t newsize = uniformBuffer->getSize() * 2; delete uniformBuffer; - uniformBuffer = CreateStreamBuffer(device, BUFFER_UNIFORM, newsize); + uniformBuffer = CreateStreamBuffer(device, BUFFERTYPE_UNIFORM, newsize); uniformBufferData = {}; uniformBufferOffset = 0; } @@ -763,7 +765,7 @@ void Graphics::draw(const DrawIndexedCommand &cmd) instanceCount:cmd.instanceCount]; }} -void Graphics::drawQuads(int start, int count, const vertex::Attributes &attributes, const vertex::BufferBindings &buffers, love::graphics::Texture *texture) +void Graphics::drawQuads(int start, int count, const VertexAttributes &attributes, const BufferBindings &buffers, love::graphics::Texture *texture) { @autoreleasepool { const int MAX_VERTICES_PER_DRAW = LOVE_UINT16_MAX; const int MAX_QUADS_PER_DRAW = MAX_VERTICES_PER_DRAW / 4; @@ -1209,7 +1211,7 @@ void Graphics::setDepthMode(CompareMode compare, bool write) } } -void Graphics::setFrontFaceWinding(vertex::Winding winding) +void Graphics::setFrontFaceWinding(Winding winding) { if (states.back().winding != winding) { @@ -1299,11 +1301,6 @@ Graphics::RendererInfo Graphics::getRendererInfo() const return info; } -Shader::Language Graphics::getShaderLanguageTarget() const -{ - return usesGLSLES() ? Shader::LANGUAGE_ESSL3 : Shader::LANGUAGE_GLSL3; -} - void Graphics::initCapabilities() { int msaa = 1; @@ -1327,7 +1324,8 @@ void Graphics::initCapabilities() capabilities.features[FEATURE_GLSL3] = true; capabilities.features[FEATURE_GLSL4] = true; capabilities.features[FEATURE_INSTANCING] = true; - static_assert(FEATURE_MAX_ENUM == 10, "Graphics::initCapabilities must be updated when adding a new graphics feature!"); + capabilities.features[FEATURE_TEXEL_BUFFER] = true; + static_assert(FEATURE_MAX_ENUM == 11, "Graphics::initCapabilities must be updated when adding a new graphics feature!"); // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf capabilities.limits[LIMIT_POINT_SIZE] = 511; @@ -1335,10 +1333,11 @@ void Graphics::initCapabilities() capabilities.limits[LIMIT_TEXTURE_LAYERS] = 2048; capabilities.limits[LIMIT_VOLUME_TEXTURE_SIZE] = 2048; capabilities.limits[LIMIT_CUBE_TEXTURE_SIZE] = 16384; // TODO + capabilities.limits[LIMIT_TEXEL_BUFFER_SIZE] = 128 * 1024 * 1024; // TODO capabilities.limits[LIMIT_RENDER_TARGETS] = 8; // TODO capabilities.limits[LIMIT_TEXTURE_MSAA] = msaa; capabilities.limits[LIMIT_ANISOTROPY] = 16.0f; - static_assert(LIMIT_MAX_ENUM == 8, "Graphics::initCapabilities must be updated when adding a new system limit!"); + static_assert(LIMIT_MAX_ENUM == 9, "Graphics::initCapabilities must be updated when adding a new system limit!"); for (int i = 0; i < TEXTURE_MAX_ENUM; i++) capabilities.textureTypes[i] = true; diff --git a/src/modules/graphics/metal/Shader.h b/src/modules/graphics/metal/Shader.h index ca2e96517..c7f10e6c6 100644 --- a/src/modules/graphics/metal/Shader.h +++ b/src/modules/graphics/metal/Shader.h @@ -49,7 +49,7 @@ public: struct RenderPipelineKey { - vertex::Attributes vertexAttributes; + VertexAttributes vertexAttributes; BlendState blend; uint64 colorRenderTargetFormats; uint32 depthStencilFormat; @@ -78,6 +78,7 @@ public: const UniformInfo *getUniformInfo(BuiltinUniform builtin) const override; void updateUniform(const UniformInfo *info, int count) override {} void sendTextures(const UniformInfo *info, love::graphics::Texture **textures, int count) override {} + void sendBuffers(const UniformInfo *info, love::graphics::Buffer **buffers, int count) override {} bool hasUniform(const std::string &name) const override { return false; } ptrdiff_t getHandle() const override { return 0; } void setVideoTextures(love::graphics::Texture *ytexture, love::graphics::Texture *cbtexture, love::graphics::Texture *crtexture) override {} diff --git a/src/modules/graphics/metal/Shader.mm b/src/modules/graphics/metal/Shader.mm index 2bf7ecc5d..260991604 100644 --- a/src/modules/graphics/metal/Shader.mm +++ b/src/modules/graphics/metal/Shader.mm @@ -39,41 +39,45 @@ namespace metal static_assert(MAX_COLOR_RENDER_TARGETS <= 8, "Metal pipeline cache key only stores 8 render target pixel formats."); -static MTLVertexFormat getMTLVertexFormat(vertex::DataType type, int components) +static MTLVertexFormat getMTLVertexFormat(DataFormat format) { - switch (type) + switch (format) { - case vertex::DATA_UNORM8: - if (components == 1) - return MTLVertexFormatUCharNormalized; - else if (components == 2) - return MTLVertexFormatUChar2Normalized; - else if (components == 3) - return MTLVertexFormatUChar3Normalized; - else - return MTLVertexFormatUChar4Normalized; - case vertex::DATA_UNORM16: - if (components == 1) - return MTLVertexFormatUShortNormalized; - else if (components == 2) - return MTLVertexFormatUShort2Normalized; - else if (components == 3) - return MTLVertexFormatUShort3Normalized; - else - return MTLVertexFormatUShort4Normalized; - case vertex::DATA_FLOAT: - if (components == 1) - return MTLVertexFormatFloat; - else if (components == 2) - return MTLVertexFormatFloat2; - else if (components == 3) - return MTLVertexFormatFloat3; - else - return MTLVertexFormatFloat4; - } + case DATAFORMAT_FLOAT: return MTLVertexFormatFloat; + case DATAFORMAT_FLOAT_VEC2: return MTLVertexFormatFloat2; + case DATAFORMAT_FLOAT_VEC3: return MTLVertexFormatFloat3; + case DATAFORMAT_FLOAT_VEC4: return MTLVertexFormatFloat4; - // TODO - return MTLVertexFormatFloat4; + case DATAFORMAT_INT32: return MTLVertexFormatInt; + case DATAFORMAT_INT32_VEC2: return MTLVertexFormatInt2; + case DATAFORMAT_INT32_VEC3: return MTLVertexFormatInt3; + case DATAFORMAT_INT32_VEC4: return MTLVertexFormatInt4; + + case DATAFORMAT_UINT32: return MTLVertexFormatUInt; + case DATAFORMAT_UINT32_VEC2: return MTLVertexFormatUInt2; + case DATAFORMAT_UINT32_VEC3: return MTLVertexFormatUInt3; + case DATAFORMAT_UINT32_VEC4: return MTLVertexFormatUInt4; + + case DATAFORMAT_SNORM8_VEC4: return MTLVertexFormatChar4Normalized; + case DATAFORMAT_UNORM8_VEC4: return MTLVertexFormatUChar4Normalized; + case DATAFORMAT_INT8_VEC4: return MTLVertexFormatChar4; + case DATAFORMAT_UINT8_VEC4: return MTLVertexFormatUChar4; + + case DATAFORMAT_SNORM16_VEC2: return MTLVertexFormatShort2Normalized; + case DATAFORMAT_SNORM16_VEC4: return MTLVertexFormatShort4Normalized; + + case DATAFORMAT_UNORM16_VEC2: return MTLVertexFormatUShort2Normalized; + case DATAFORMAT_UNORM16_VEC4: return MTLVertexFormatUShort4Normalized; + + case DATAFORMAT_INT16_VEC2: return MTLVertexFormatShort2; + case DATAFORMAT_INT16_VEC4: return MTLVertexFormatShort4; + + case DATAFORMAT_UINT16: return MTLVertexFormatUShort; + case DATAFORMAT_UINT16_VEC2: return MTLVertexFormatUShort2; + case DATAFORMAT_UINT16_VEC4: return MTLVertexFormatUShort4; + + default: return MTLVertexFormatInvalid; + } } static MTLBlendOperation getMTLBlendOperation(BlendOperation op) @@ -405,7 +409,7 @@ id Shader::getCachedRenderPipeline(const RenderPipelineK const auto &attrib = attributes.attribs[i]; int metalBufferIndex = attrib.bufferIndex + VERTEX_BUFFER_BINDING_START; - vertdesc.attributes[i].format = getMTLVertexFormat(attrib.type, attrib.components); + vertdesc.attributes[i].format = getMTLVertexFormat(attrib.format); vertdesc.attributes[i].offset = attrib.offsetFromVertex; vertdesc.attributes[i].bufferIndex = metalBufferIndex; diff --git a/src/modules/graphics/vertex.h b/src/modules/graphics/vertex.h index f1479ccf8..34dce00e5 100644 --- a/src/modules/graphics/vertex.h +++ b/src/modules/graphics/vertex.h @@ -58,6 +58,7 @@ enum BufferType { BUFFERTYPE_VERTEX = 0, BUFFERTYPE_INDEX, + BUFFERTYPE_UNIFORM, BUFFERTYPE_TEXEL, BUFFERTYPE_MAX_ENUM };