Path: blob/master/drivers/metal/metal_objects_shared.h
12197 views
/**************************************************************************/1/* metal_objects_shared.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#import "metal_device_properties.h"33#import "metal_utils.h"3435using RDC = RenderingDeviceCommons;3637// These types can be used in Vector and other containers that use38// pointer operations not supported by ARC.39namespace MTL {40#define MTL_CLASS(name) \41class name { \42public: \43name(id<MTL##name> obj = nil) : m_obj(obj) {} \44operator id<MTL##name>() const { \45return m_obj; \46} \47id<MTL##name> m_obj; \48};4950MTL_CLASS(Texture)5152} //namespace MTL5354enum ShaderStageUsage : uint32_t {55None = 0,56Vertex = RDD::SHADER_STAGE_VERTEX_BIT,57Fragment = RDD::SHADER_STAGE_FRAGMENT_BIT,58TesselationControl = RDD::SHADER_STAGE_TESSELATION_CONTROL_BIT,59TesselationEvaluation = RDD::SHADER_STAGE_TESSELATION_EVALUATION_BIT,60Compute = RDD::SHADER_STAGE_COMPUTE_BIT,61};6263_FORCE_INLINE_ ShaderStageUsage &operator|=(ShaderStageUsage &p_a, int p_b) {64p_a = ShaderStageUsage(uint32_t(p_a) | uint32_t(p_b));65return p_a;66}6768struct ClearAttKey {69const static uint32_t COLOR_COUNT = MAX_COLOR_ATTACHMENT_COUNT;70const static uint32_t DEPTH_INDEX = COLOR_COUNT;71const static uint32_t STENCIL_INDEX = DEPTH_INDEX + 1;72const static uint32_t ATTACHMENT_COUNT = STENCIL_INDEX + 1;7374enum Flags : uint16_t {75CLEAR_FLAGS_NONE = 0,76CLEAR_FLAGS_LAYERED = 1 << 0,77};7879Flags flags = CLEAR_FLAGS_NONE;80uint16_t sample_count = 0;81uint16_t pixel_formats[ATTACHMENT_COUNT] = { 0 };8283_FORCE_INLINE_ void set_color_format(uint32_t p_idx, MTLPixelFormat p_fmt) { pixel_formats[p_idx] = p_fmt; }84_FORCE_INLINE_ void set_depth_format(MTLPixelFormat p_fmt) { pixel_formats[DEPTH_INDEX] = p_fmt; }85_FORCE_INLINE_ void set_stencil_format(MTLPixelFormat p_fmt) { pixel_formats[STENCIL_INDEX] = p_fmt; }86_FORCE_INLINE_ MTLPixelFormat depth_format() const { return (MTLPixelFormat)pixel_formats[DEPTH_INDEX]; }87_FORCE_INLINE_ MTLPixelFormat stencil_format() const { return (MTLPixelFormat)pixel_formats[STENCIL_INDEX]; }88_FORCE_INLINE_ void enable_layered_rendering() { flags::set(flags, CLEAR_FLAGS_LAYERED); }8990_FORCE_INLINE_ bool is_enabled(uint32_t p_idx) const { return pixel_formats[p_idx] != 0; }91_FORCE_INLINE_ bool is_depth_enabled() const { return pixel_formats[DEPTH_INDEX] != 0; }92_FORCE_INLINE_ bool is_stencil_enabled() const { return pixel_formats[STENCIL_INDEX] != 0; }93_FORCE_INLINE_ bool is_layered_rendering_enabled() const { return flags::any(flags, CLEAR_FLAGS_LAYERED); }9495_FORCE_INLINE_ bool operator==(const ClearAttKey &p_rhs) const {96return memcmp(this, &p_rhs, sizeof(ClearAttKey)) == 0;97}9899uint32_t hash() const {100uint32_t h = hash_murmur3_one_32(flags);101h = hash_murmur3_one_32(sample_count, h);102h = hash_murmur3_buffer(pixel_formats, ATTACHMENT_COUNT * sizeof(pixel_formats[0]), h);103return hash_fmix32(h);104}105};106107/**108* Returns an index that can be used to map a shader stage to an index in a fixed-size array that is used for109* a single pipeline type.110*/111_FORCE_INLINE_ static uint32_t to_index(RDD::ShaderStage p_s) {112switch (p_s) {113case RenderingDeviceCommons::SHADER_STAGE_VERTEX:114case RenderingDeviceCommons::SHADER_STAGE_TESSELATION_CONTROL:115case RenderingDeviceCommons::SHADER_STAGE_TESSELATION_EVALUATION:116case RenderingDeviceCommons::SHADER_STAGE_COMPUTE:117default:118return 0;119case RenderingDeviceCommons::SHADER_STAGE_FRAGMENT:120return 1;121}122}123124class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) MDFrameBuffer {125Vector<MTL::Texture> textures;126127public:128Size2i size;129MDFrameBuffer(Vector<MTL::Texture> p_textures, Size2i p_size) :130textures(p_textures), size(p_size) {}131MDFrameBuffer() {}132133/// Returns the texture at the given index.134_ALWAYS_INLINE_ MTL::Texture get_texture(uint32_t p_idx) const {135return textures[p_idx];136}137138/// Returns true if the texture at the given index is not nil.139_ALWAYS_INLINE_ bool has_texture(uint32_t p_idx) const {140return textures[p_idx] != nil;141}142143/// Set the texture at the given index.144_ALWAYS_INLINE_ void set_texture(uint32_t p_idx, MTL::Texture p_texture) {145textures.write[p_idx] = p_texture;146}147148/// Unset or nil the texture at the given index.149_ALWAYS_INLINE_ void unset_texture(uint32_t p_idx) {150textures.write[p_idx] = nil;151}152153/// Resizes buffers to the specified size.154_ALWAYS_INLINE_ void set_texture_count(uint32_t p_size) {155textures.resize(p_size);156}157158virtual ~MDFrameBuffer() = default;159};160161// These functions are used to convert between Objective-C objects and162// the RIDs used by Godot, respecting automatic reference counting.163namespace rid {164165// Converts an Objective-C object to a pointer, and incrementing the166// reference count.167_FORCE_INLINE_ void *owned(id p_id) {168return (__bridge_retained void *)p_id;169}170171#define MAKE_ID(FROM, TO) \172_FORCE_INLINE_ TO make(FROM p_obj) { \173return TO(owned(p_obj)); \174}175176// These are shared for Metal and Metal 4 drivers177178MAKE_ID(id<MTLTexture>, RDD::TextureID)179MAKE_ID(id<MTLBuffer>, RDD::BufferID)180MAKE_ID(id<MTLSamplerState>, RDD::SamplerID)181MAKE_ID(MTLVertexDescriptor *, RDD::VertexFormatID)182183#undef MAKE_ID184185// Converts a pointer to an Objective-C object without changing the reference count.186_FORCE_INLINE_ auto get(RDD::ID p_id) {187return (p_id.id) ? (__bridge ::id)(void *)p_id.id : nil;188}189190// Converts a pointer to an Objective-C object, and decrements the reference count.191_FORCE_INLINE_ auto release(RDD::ID p_id) {192return (__bridge_transfer ::id)(void *)p_id.id;193}194195} // namespace rid196197198