Skip to content

Commit

Permalink
Add ComputeState implementation for Metal backend and RHI PIMPL class
Browse files Browse the repository at this point in the history
  • Loading branch information
egorodet committed Feb 18, 2023
1 parent 04d2ccc commit 6680506
Show file tree
Hide file tree
Showing 11 changed files with 417 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Modules/Graphics/RHI/Impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ list(APPEND HEADERS
${INCLUDE_DIR}/RenderContext.h
${INCLUDE_DIR}/RenderState.h
${INCLUDE_DIR}/ViewState.h
${INCLUDE_DIR}/ComputeState.h
${INCLUDE_DIR}/Buffer.h
${INCLUDE_DIR}/BufferSet.h
${INCLUDE_DIR}/Texture.h
Expand Down Expand Up @@ -48,6 +49,7 @@ list(APPEND SOURCES
${SOURCES_DIR}/RenderContext.cpp
${SOURCES_DIR}/RenderState.cpp
${SOURCES_DIR}/ViewState.cpp
${SOURCES_DIR}/ComputeState.cpp
${SOURCES_DIR}/Buffer.cpp
${SOURCES_DIR}/BufferSet.cpp
${SOURCES_DIR}/Texture.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/******************************************************************************
Copyright 2023 Evgeny Gorodetskiy
Licensed under the Apache License, Version 2.0 (the "License"),
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************
FILE: Methane/Graphics/RHI/ComputeState.h
Methane ComputeState PIMPL wrappers for direct calls to final implementation.
******************************************************************************/

#pragma once

#include <Methane/Pimpl.h>
#include "Program.h"

#include <Methane/Graphics/RHI/IComputeState.h>

namespace Methane::Graphics::META_GFX_NAME
{
class ComputeState;
}

namespace Methane::Graphics::Rhi
{

struct ComputeStateSettingsImpl
{
Program program;

META_PIMPL_API static ComputeStateSettings Convert(const ComputeStateSettingsImpl& settings);
};

class RenderContext;

class ComputeState // NOSONAR - constructors and assignment operators are required to use forward declared Impl and Ptr<Impl> in header
{
public:
using Settings = ComputeStateSettingsImpl;

META_PIMPL_DEFAULT_CONSTRUCT_METHODS_DECLARE(ComputeState);
META_PIMPL_METHODS_COMPARE_DECLARE(ComputeState);

META_PIMPL_API explicit ComputeState(const Ptr<IComputeState>& interface_ptr);
META_PIMPL_API explicit ComputeState(IComputeState& interface_ref);
META_PIMPL_API ComputeState(const RenderContext& context, const Settings& settings);

META_PIMPL_API bool IsInitialized() const META_PIMPL_NOEXCEPT;
META_PIMPL_API IComputeState& GetInterface() const META_PIMPL_NOEXCEPT;
META_PIMPL_API Ptr<IComputeState> GetInterfacePtr() const META_PIMPL_NOEXCEPT;

// IObject interface methods
META_PIMPL_API bool SetName(std::string_view name) const;
META_PIMPL_API std::string_view GetName() const META_PIMPL_NOEXCEPT;

// Data::IEmitter<IObjectCallback> interface methods
META_PIMPL_API void Connect(Data::Receiver<IObjectCallback>& receiver) const;
META_PIMPL_API void Disconnect(Data::Receiver<IObjectCallback>& receiver) const;

// IComputeState interface methods
[[nodiscard]] META_PIMPL_API const ComputeStateSettings& GetSettings() const META_PIMPL_NOEXCEPT;
META_PIMPL_API void Reset(const Settings& settings) const;
META_PIMPL_API void Reset(const IComputeState::Settings& settings) const;

META_PIMPL_API Program GetProgram() const;

private:
using Impl = Methane::Graphics::META_GFX_NAME::ComputeState;

Ptr<Impl> m_impl_ptr;
};

} // namespace Methane::Graphics::Rhi

#ifdef META_PIMPL_INLINE

#include <Methane/Graphics/RHI/ComputeState.cpp>

#endif // META_PIMPL_INLINE
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/******************************************************************************
Copyright 2023 Evgeny Gorodetskiy
Licensed under the Apache License, Version 2.0 (the "License"),
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************
FILE: Methane/Graphics/RHI/ComputeState.cpp
Methane ComputeState PIMPL wrappers for direct calls to final implementation.
******************************************************************************/

#include <Methane/Graphics/RHI/ComputeState.h>
#include <Methane/Graphics/RHI/RenderContext.h>

#include <Methane/Pimpl.hpp>

#ifdef META_GFX_METAL
#include <ComputeState.hh>
#else
#include <ComputeState.h>
#endif

namespace Methane::Graphics::Rhi
{

ComputeStateSettings ComputeStateSettingsImpl::Convert(const ComputeStateSettingsImpl& settings)
{
return ComputeStateSettings
{
settings.program.GetInterfacePtr()
};
}

META_PIMPL_DEFAULT_CONSTRUCT_METHODS_IMPLEMENT(ComputeState);
META_PIMPL_METHODS_COMPARE_IMPLEMENT(ComputeState);

ComputeState::ComputeState(const Ptr<IComputeState>& interface_ptr)
: m_impl_ptr(std::dynamic_pointer_cast<Impl>(interface_ptr))
{
}

ComputeState::ComputeState(IComputeState& interface_ref)
: ComputeState(interface_ref.GetDerivedPtr<IComputeState>())
{
}

ComputeState::ComputeState(const RenderContext& context, const Settings& settings)
: ComputeState(IComputeState::Create(context.GetInterface(), ComputeStateSettingsImpl::Convert(settings)))
{
}

bool ComputeState::IsInitialized() const META_PIMPL_NOEXCEPT
{
return static_cast<bool>(m_impl_ptr);
}

IComputeState& ComputeState::GetInterface() const META_PIMPL_NOEXCEPT
{
return *m_impl_ptr;
}

Ptr<IComputeState> ComputeState::GetInterfacePtr() const META_PIMPL_NOEXCEPT
{
return m_impl_ptr;
}

bool ComputeState::SetName(std::string_view name) const
{
return GetImpl(m_impl_ptr).SetName(name);
}

std::string_view ComputeState::GetName() const META_PIMPL_NOEXCEPT
{
return GetImpl(m_impl_ptr).GetName();
}

void ComputeState::Connect(Data::Receiver<IObjectCallback>& receiver) const
{
GetImpl(m_impl_ptr).Data::Emitter<IObjectCallback>::Connect(receiver);
}

void ComputeState::Disconnect(Data::Receiver<IObjectCallback>& receiver) const
{
GetImpl(m_impl_ptr).Data::Emitter<IObjectCallback>::Disconnect(receiver);
}

const ComputeStateSettings& ComputeState::GetSettings() const META_PIMPL_NOEXCEPT
{
return GetImpl(m_impl_ptr).GetSettings();
}

void ComputeState::Reset(const Settings& settings) const
{
return GetImpl(m_impl_ptr).Reset(ComputeStateSettingsImpl::Convert(settings));
}

void ComputeState::Reset(const IComputeState::Settings& settings) const
{
return GetImpl(m_impl_ptr).Reset(settings);
}

Program ComputeState::GetProgram() const
{
return Program(GetSettings().program_ptr);
}

} // namespace Methane::Graphics::Rhi
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Methane render state interface: specifies configuration of the graphics pipeline

#include <Methane/Graphics/RHI/IRenderState.h>
#include <Methane/Graphics/RHI/IRenderContext.h>
#include <Methane/Graphics/RHI/IProgram.h>

#include <Methane/Data/EnumMaskUtil.hpp>
#include <Methane/Instrumentation.h>
Expand Down
2 changes: 2 additions & 0 deletions Modules/Graphics/RHI/Metal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ list(APPEND HEADERS
${INCLUDE_DIR}/ProgramBindings.hh
${INCLUDE_DIR}/RenderState.hh
${INCLUDE_DIR}/ViewState.hh
${INCLUDE_DIR}/ComputeState.hh
${INCLUDE_DIR}/Resource.hh
${INCLUDE_DIR}/ResourceBarriers.hh
${INCLUDE_DIR}/DescriptorManager.h
Expand Down Expand Up @@ -53,6 +54,7 @@ list(APPEND SOURCES
${SOURCES_DIR}/ProgramBindings.mm
${SOURCES_DIR}/RenderState.mm
${SOURCES_DIR}/ViewState.mm
${SOURCES_DIR}/ComputeState.mm
${SOURCES_DIR}/Resource.mm
${SOURCES_DIR}/Buffer.mm
${SOURCES_DIR}/BufferSet.mm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class CommandList : public CommandListBaseT
}

const MTLCommandEncoderId& GetNativeCommandEncoder() const noexcept { return m_mtl_cmd_encoder; }
const id<MTLCommandBuffer>& GetNativeCommandBuffer() const noexcept { return m_mtl_cmd_buffer; }
const id<MTLCommandBuffer>& GetNativeCommandBuffer() const noexcept { return m_mtl_cmd_buffer; }

CommandQueue& GetMetalCommandQueue() noexcept
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ Metal implementation of the compute command list interface.

#include "CommandList.hpp"

#include <Methane/Graphics/RHI/IComputeCommandList.h>
#include <Methane/Graphics/Base/CommandList.h>
#include <Methane/Graphics/Base/ComputeCommandList.h>

#import <Metal/Metal.h>

Expand All @@ -36,8 +35,7 @@ namespace Methane::Graphics::Metal
class CommandQueue;

class ComputeCommandList final
: public CommandList<id<MTLComputeCommandEncoder>, Base::CommandList>
, public Rhi::IComputeCommandList
: public CommandList<id<MTLComputeCommandEncoder>, Base::ComputeCommandList>
{
public:
ComputeCommandList(Base::CommandQueue& command_queue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/******************************************************************************
Copyright 2023 Evgeny Gorodetskiy
Licensed under the Apache License, Version 2.0 (the "License"),
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*******************************************************************************
FILE: Methane/Graphics/Metal/ComputeState.hh
Metal implementation of the render state interface.
******************************************************************************/

#pragma once

#include <Methane/Graphics/Base/ComputeState.h>

#import <Metal/Metal.h>

#include <vector>

namespace Methane::Graphics::Metal
{

class Device;

class ComputeState final
: public Base::ComputeState
{
public:
ComputeState(const Rhi::IContext& context, const Settings& settings);

// IComputeState interface
void Reset(const Settings& settings) override;

// Base::ComputeState interface
void Apply(Base::ComputeCommandList& command_list) override;

// IObject interface
bool SetName(std::string_view name) override;

void InitializeNativeStates();
void InitializeNativePipelineState();
void InitializeNativeDepthStencilState();

id<MTLComputePipelineState> GetNativePipelineState();

private:
void ResetNativeState();

const Device& m_device;
MTLComputePipelineDescriptor* m_mtl_pipeline_state_desc = nil;
id<MTLComputePipelineState> m_mtl_pipeline_state = nil;
};

} // namespace Methane::Graphics::Metal
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
{

ComputeCommandList::ComputeCommandList(Base::CommandQueue& command_queue)
: CommandList<id<MTLComputeCommandEncoder>, Base::CommandList>(true, command_queue, Rhi::CommandListType::Compute)
: CommandList(true, command_queue)
{ }

void ComputeCommandList::Reset(Rhi::ICommandListDebugGroup* debug_group_ptr)
Expand Down
Loading

9 comments on commit 6680506

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ubuntu_VK_SonarScan Tests Code Coverage

Code Coverage

Package Line Rate Branch Rate Health
Default 25% 100%
Summary 25% (4527 / 18101) 100% (0 / 0)

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacOS_MTL_SonarScan Tests Code Coverage

Code Coverage

Package Line Rate Branch Rate Health
Default 36% 19%
Summary 36% (8156 / 22711) 19% (2965 / 15802)

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win64_DX_SonarScan Tests Code Coverage

Code Coverage

Package Line Rate Branch Rate Health
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneDataEventsTest.exe 95% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneDataRangeSetTest.exe 91% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneDataTypesTest.exe 98% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneGraphicsCameraTest.exe 61% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneGraphicsTypesTest.exe 98% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethanePlatformInputTest.exe 43% 100%
D:\a\MethaneKit\MethaneKit\Build\Output\Ninja-Win-DX-Scan\Install\Tests\MethaneUserInterfaceTypesTest.exe 10% 100%
Summary 29% (2108 / 7223) 100% (0 / 0)

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ubuntu_VK_Release Test Results

  • ✅ 2967 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 12375 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MacOS_MTL_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 79 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win64_DX_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 152 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win32_VK_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 152 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win64_VK_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 165 ms. run duration

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Win32_DX_Release Test Results

  • ✅ 2966 tests passed
  • ❌ 0 tests failed
  • ⚠️ 0 tests skipped
  • ⏱️ 146 ms. run duration

Please sign in to comment.