Path: blob/master/servers/rendering/renderer_rd/effects/metal_fx.cpp
20971 views
/**************************************************************************/1/* metal_fx.cpp */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#ifdef METAL_ENABLED3132#include "metal_fx.h"3334#include "../storage_rd/render_scene_buffers_rd.h"35#include "drivers/metal/pixel_formats.h"36#include "drivers/metal/rendering_device_driver_metal3.h"3738#include <MetalFX/MetalFX.hpp>3940using namespace RendererRD;4142#pragma mark - Spatial Scaler4344MFXSpatialContext::~MFXSpatialContext() {45if (scaler) {46scaler->release();47}48}4950MFXSpatialEffect::MFXSpatialEffect() {51}5253MFXSpatialEffect::~MFXSpatialEffect() {54}5556void MFXSpatialEffect::callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata) {57MDCommandBufferBase *obj = (MDCommandBufferBase *)(p_command_buffer.id);58obj->end();5960MTL::Texture *src_texture = reinterpret_cast<MTL::Texture *>(p_userdata->src.id);61MTL::Texture *dst_texture = reinterpret_cast<MTL::Texture *>(p_userdata->dst.id);6263MTLFX::SpatialScalerBase *scaler = p_userdata->scaler;64scaler->setColorTexture(src_texture);65scaler->setOutputTexture(dst_texture);66MTLFX::SpatialScaler *s = static_cast<MTLFX::SpatialScaler *>(scaler);67MTL3::MDCommandBuffer *cmd = (MTL3::MDCommandBuffer *)(p_command_buffer.id);68s->encodeToCommandBuffer(cmd->get_command_buffer());69obj->retain_resource(scaler);7071CallbackArgs::free(&p_userdata);72}7374void MFXSpatialEffect::ensure_context(Ref<RenderSceneBuffersRD> p_render_buffers) {75p_render_buffers->ensure_mfx(this);76}7778void MFXSpatialEffect::process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_src, RID p_dst) {79MFXSpatialContext *ctx = p_render_buffers->get_mfx_spatial_context();80DEV_ASSERT(ctx); // this should have been done by the caller via ensure_context8182CallbackArgs *userdata = args_allocator.alloc(83this,84RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_src)),85RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_dst)),86*ctx);87RD::CallbackResource res[2] = {88{ .rid = p_src, .usage = RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE },89{ .rid = p_dst, .usage = RD::CALLBACK_RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE }90};91RD::get_singleton()->driver_callback_add((RDD::DriverCallback)MFXSpatialEffect::callback, userdata, VectorView<RD::CallbackResource>(res, 2));92}9394MFXSpatialContext *MFXSpatialEffect::create_context(CreateParams p_params) const {95DEV_ASSERT(RD::get_singleton()->has_feature(RD::SUPPORTS_METALFX_SPATIAL));9697RenderingDeviceDriverMetal *rdd = (RenderingDeviceDriverMetal *)RD::get_singleton()->get_device_driver();98PixelFormats &pf = rdd->get_pixel_formats();99MTL::Device *dev = rdd->get_device();100101NS::SharedPtr<MTLFX::SpatialScalerDescriptor> desc = NS::TransferPtr(MTLFX::SpatialScalerDescriptor::alloc()->init());102desc->setInputWidth((NS::UInteger)p_params.input_size.width);103desc->setInputHeight((NS::UInteger)p_params.input_size.height);104105desc->setOutputWidth((NS::UInteger)p_params.output_size.width);106desc->setOutputHeight((NS::UInteger)p_params.output_size.height);107108desc->setColorTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.input_format));109desc->setOutputTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.output_format));110desc->setColorProcessingMode(MTLFX::SpatialScalerColorProcessingModeLinear);111112MFXSpatialContext *context = memnew(MFXSpatialContext);113context->scaler = desc->newSpatialScaler(dev);114115return context;116}117118#ifdef METAL_MFXTEMPORAL_ENABLED119120#pragma mark - Temporal Scaler121122MFXTemporalContext::~MFXTemporalContext() {123if (scaler) {124scaler->release();125}126}127128MFXTemporalEffect::MFXTemporalEffect() {}129MFXTemporalEffect::~MFXTemporalEffect() {}130131MFXTemporalContext *MFXTemporalEffect::create_context(CreateParams p_params) const {132DEV_ASSERT(RD::get_singleton()->has_feature(RD::SUPPORTS_METALFX_TEMPORAL));133134RenderingDeviceDriverMetal *rdd = (RenderingDeviceDriverMetal *)RD::get_singleton()->get_device_driver();135PixelFormats &pf = rdd->get_pixel_formats();136MTL::Device *dev = rdd->get_device();137138NS::SharedPtr<MTLFX::TemporalScalerDescriptor> desc = NS::TransferPtr(MTLFX::TemporalScalerDescriptor::alloc()->init());139desc->setInputWidth((NS::UInteger)p_params.input_size.width);140desc->setInputHeight((NS::UInteger)p_params.input_size.height);141142desc->setOutputWidth((NS::UInteger)p_params.output_size.width);143desc->setOutputHeight((NS::UInteger)p_params.output_size.height);144145desc->setColorTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.input_format));146desc->setDepthTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.depth_format));147desc->setMotionTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.motion_format));148desc->setAutoExposureEnabled(false);149150desc->setOutputTextureFormat((MTL::PixelFormat)pf.getMTLPixelFormat(p_params.output_format));151152MFXTemporalContext *context = memnew(MFXTemporalContext);153context->scaler = desc->newTemporalScaler(dev);154context->scaler->setMotionVectorScaleX(p_params.motion_vector_scale.x);155context->scaler->setMotionVectorScaleY(p_params.motion_vector_scale.y);156context->scaler->setDepthReversed(true); // Godot uses reverse Z per https://github.com/godotengine/godot/pull/88328157158return context;159}160161void MFXTemporalEffect::process(RendererRD::MFXTemporalContext *p_ctx, RendererRD::MFXTemporalEffect::Params p_params) {162CallbackArgs *userdata = args_allocator.alloc(163this,164RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.src)),165RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.depth)),166RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.motion)),167p_params.exposure.is_valid() ? RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.exposure)) : RDD::TextureID(),168p_params.jitter_offset,169RDD::TextureID(RD::get_singleton()->get_driver_resource(RDC::DRIVER_RESOURCE_TEXTURE, p_params.dst)),170*p_ctx,171p_params.reset);172RD::CallbackResource res[3] = {173{ .rid = p_params.src, .usage = RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE },174{ .rid = p_params.depth, .usage = RD::CALLBACK_RESOURCE_USAGE_TEXTURE_SAMPLE },175{ .rid = p_params.dst, .usage = RD::CALLBACK_RESOURCE_USAGE_STORAGE_IMAGE_READ_WRITE },176};177RD::get_singleton()->driver_callback_add((RDD::DriverCallback)MFXTemporalEffect::callback, userdata, VectorView<RD::CallbackResource>(res, 3));178}179180void MFXTemporalEffect::callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata) {181MDCommandBufferBase *obj = (MDCommandBufferBase *)(p_command_buffer.id);182obj->end();183184MTL::Texture *src_texture = reinterpret_cast<MTL::Texture *>(p_userdata->src.id);185MTL::Texture *depth = reinterpret_cast<MTL::Texture *>(p_userdata->depth.id);186MTL::Texture *motion = reinterpret_cast<MTL::Texture *>(p_userdata->motion.id);187MTL::Texture *exposure = reinterpret_cast<MTL::Texture *>(p_userdata->exposure.id);188189MTL::Texture *dst_texture = reinterpret_cast<MTL::Texture *>(p_userdata->dst.id);190191MTLFX::TemporalScalerBase *scaler = p_userdata->scaler;192scaler->setReset(p_userdata->reset);193scaler->setColorTexture(src_texture);194scaler->setDepthTexture(depth);195scaler->setMotionTexture(motion);196scaler->setExposureTexture(exposure);197scaler->setJitterOffsetX(p_userdata->jitter_offset.x);198scaler->setJitterOffsetY(p_userdata->jitter_offset.y);199scaler->setOutputTexture(dst_texture);200MTLFX::TemporalScaler *s = static_cast<MTLFX::TemporalScaler *>(scaler);201MTL3::MDCommandBuffer *cmd = (MTL3::MDCommandBuffer *)(p_command_buffer.id);202s->encodeToCommandBuffer(cmd->get_command_buffer());203obj->retain_resource(scaler);204205CallbackArgs::free(&p_userdata);206}207208#endif209210#endif211212213