Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/RavEngine/RGL into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Feb 21, 2024
2 parents fff05b6 + 3c89b1a commit c1bfc29
Show file tree
Hide file tree
Showing 24 changed files with 212 additions and 38 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ jobs:
cd build
cmake --build . --config Release --target RGL -- -quiet
build-android:
name: Android Compile Check
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
arch: [x86, x86_64, arm64-v8a, armeabi-v7a]
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
submodules: recursive
- name: Install deps
run: |
sudo apt update
sudo apt install ninja-build -y
- name: Configure
run: |
mkdir build
cd build
cmake -G "Ninja" -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=33 -DCMAKE_ANDROID_ARCH_ABI="${{ matrix.arch }}" -DCMAKE_ANDROID_ARM_NEON=ON ..
- name: Build
run: cmake --build build --parallel


build-web:
name: Web Compile Check
runs-on: ubuntu-latest
Expand Down
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ option(RGL_DISABLE_DX "Force-disable the D3D12 backend" OFF)
option(RGL_DISABLE_WEBGPU "Force-disable the WebGPU backend" OFF)
option(RGL_ENABLE_WEBGPU "Force-enable the WebGPU backend" OFF)
option(RGL_RUNTIME_COMPILATION "Enable runtime shader compilation" OFF)
option(RGL_SKIP_BACKEND_CHECK "Skip the SDK check" OFF)
option(RGL_IDE_ROOT "IDE parent folder for targets" "RGL")
if (NOT RGL_IDE_ROOT)
set(RGL_IDE_ROOT "RGL") # CMake sets this to "OFF" for some insane reason
Expand Down Expand Up @@ -260,8 +261,9 @@ if (RGL_DX12_AVAILABLE)
endif()



if (RGL_VK_AVAILABLE OR RGL_DX12_AVAILABLE OR RGL_MTL_AVAILABLE OR RGL_WEBGPU_AVAILABLE)
else()
message(FATAL_ERROR "No backends are enabled! Check that all required SDKs are installed.")
if (NOT RGL_SKIP_BACKEND_CHECK)
if (RGL_VK_AVAILABLE OR RGL_DX12_AVAILABLE OR RGL_MTL_AVAILABLE OR RGL_WEBGPU_AVAILABLE)
else()
message(FATAL_ERROR "No backends are enabled! Check that all required SDKs are installed.")
endif()
endif()
2 changes: 2 additions & 0 deletions include/RGL/CommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ struct TextureView;

struct TextureCopyConfig{
TextureView texture;
uint32_t mip = 0;
uint32_t layer = 0;
};
virtual void CopyTextureToTexture(const TextureCopyConfig& from, const TextureCopyConfig& to) = 0;

Expand Down
16 changes: 15 additions & 1 deletion include/RGL/Texture.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include "Types.hpp"
#include <cstdint>
#include <limits>
#include <string_view>
#include "TextureFormat.hpp"
#include "SubresourceRange.hpp"

Expand Down Expand Up @@ -118,6 +120,7 @@ namespace RGL {
TextureView() {}
};


struct TextureConfig {
TextureUsage usage;
TextureAspect aspect;
Expand All @@ -128,7 +131,17 @@ namespace RGL {
ResourceLayout initialLayout = ResourceLayout::Undefined;
bool isCubemap = false;
bool readbackEnabled = false;
const char* debugName = nullptr;
std::string_view debugName;
};

class ICustomTextureView {

virtual TextureView GetView() const = 0;
};

struct CustomTextureViewConfig {
uint32_t mip = 0;
uint32_t layer = 0;
};

class ITexture {
Expand All @@ -139,6 +152,7 @@ namespace RGL {
virtual Dimension GetSize() const = 0;
virtual TextureView GetDefaultView() const = 0;
virtual TextureView GetViewForMip(uint32_t mip) const = 0;
virtual RGLCustomTextureViewPtr MakeCustomTextureView(const CustomTextureViewConfig& config) const = 0;
};

}
10 changes: 10 additions & 0 deletions include/RGL/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace RGL {
struct ISwapchain;
struct IRenderPass;
struct IComputePipeline;
struct ICustomTextureView;
}

#if RGL_SINGLE_BACKEND
Expand All @@ -52,6 +53,7 @@ namespace RGL {
struct CommandBufferMTL;
struct RenderPassMTL;
struct ComputePipelineMTL;
struct CustomTextureViewMTL;
}

using RGLDevicePtr = std::shared_ptr<RGL::DeviceMTL>;
Expand All @@ -69,6 +71,8 @@ using RGLCommandQueuePtr = std::shared_ptr<RGL::CommandQueueMTL>;
using RGLCommandBufferPtr = std::shared_ptr<RGL::CommandBufferMTL>;
using RGLRenderPassPtr = std::shared_ptr<RGL::RenderPassMTL>;
using RGLComputePipelinePtr = std::shared_ptr<RGL::ComputePipelineMTL>;
using RGLCustomTextureViewPtr = std::shared_ptr<RGL::CustomTextureViewMTL>;


#elif RGL_VK_AVAILABLE
namespace RGL {
Expand All @@ -89,6 +93,7 @@ namespace RGL {
struct CommandQueueVk;
struct CommandBufferVk;
struct ComputePipelineVk;
struct CustomTextureViewVk;
}

using RGLDevicePtr = std::shared_ptr<RGL::DeviceVk>;
Expand All @@ -105,6 +110,8 @@ using RGLSamplerPtr = std::shared_ptr<RGL::SamplerVk>;
using RGLCommandQueuePtr = std::shared_ptr<RGL::CommandQueueVk>;
using RGLCommandBufferPtr = std::shared_ptr<RGL::CommandBufferVk>;
using RGLComputePipelinePtr = std::shared_ptr<RGL::ComputePipelineVk>;
using RGLCustomTextureViewPtr = std::shared_ptr<RGL::CustomTextureViewVk>;

#elif RGL_DX12_AVAILABLE
namespace RGL {
struct SwapchainD3D12;
Expand All @@ -124,6 +131,7 @@ namespace RGL {
struct CommandQueueD3D12;
struct CommandBufferD3D12;
struct ComputePipelineD3D12;
struct CustomTextureViewD3D12;
}

using RGLDevicePtr = std::shared_ptr<RGL::DeviceD3D12>;
Expand All @@ -141,6 +149,7 @@ using RGLCommandQueuePtr = std::shared_ptr<RGL::CommandQueueD3D12>;
using RGLCommandBufferPtr = std::shared_ptr<RGL::CommandBufferD3D12>;
using RGLRenderPassPtr = std::shared_ptr<RGL::RenderPassD3D12>;
using RGLComputePipelinePtr = std::shared_ptr<RGL::ComputePipelineD3D12>;
using RGLCustomTextureViewPtr = std::shared_ptr<RGL::CustomTextureViewD3D12>;
#endif

#else
Expand All @@ -153,6 +162,7 @@ using RGLComputePipelinePtr = std::shared_ptr<RGL::ComputePipelineD3D12>;
using RGLBufferPtr = std::shared_ptr<RGL::IBuffer>;
using RGLFencePtr = std::shared_ptr<RGL::IFence>;
using RGLTexturePtr = std::shared_ptr<RGL::ITexture>;
using RGLCustomTextureViewPtr = std::shared_ptr<RGL::ICustomTextureView>;
using RGLSamplerPtr = std::shared_ptr<RGL::ISampler>;
using RGLCommandQueuePtr = std::shared_ptr<RGL::ICommandQueue>;
using RGLCommandBufferPtr = std::shared_ptr<RGL::ICommandBuffer>;
Expand Down
4 changes: 2 additions & 2 deletions src/D3D12CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,13 @@ namespace RGL {
D3D12_TEXTURE_COPY_LOCATION srcLocation = {};
srcLocation.pResource = from.texture.texture.dx.parentResource->texture.Get();
srcLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
srcLocation.SubresourceIndex = 0;
srcLocation.SubresourceIndex = from.texture.texture.dx.parentResource->SubresourceIndexForMipLayer(from.mip, from.layer);

// Create a destination texture location
D3D12_TEXTURE_COPY_LOCATION dstLocation = {};
dstLocation.pResource = to.texture.texture.dx.parentResource->texture.Get();
dstLocation.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
dstLocation.SubresourceIndex = 0;
dstLocation.SubresourceIndex = to.texture.texture.dx.parentResource->SubresourceIndexForMipLayer(to.mip, to.layer);

// Create a box that specifies the region to copy
D3D12_BOX box = {};
Expand Down
14 changes: 11 additions & 3 deletions src/D3D12Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ namespace RGL {
&allocation, IID_PPV_ARGS(&texture));

std::wstring wide;
wide.resize(config.debugName == nullptr? 0 : strlen(config.debugName));
MultiByteToWideChar(CP_UTF8, 0, config.debugName, -1, wide.data(), wide.size());
wide.resize(config.debugName.data() == nullptr ? 0 : config.debugName.length());
MultiByteToWideChar(CP_UTF8, 0, config.debugName.data(), -1, wide.data(), wide.size());
texture->SetName(wide.data());

if (config.debugName != nullptr) {
if (config.debugName.data() != nullptr) {
debugName = config.debugName;
}

Expand Down Expand Up @@ -269,6 +269,10 @@ namespace RGL {
.coveredLayers = ALL_LAYERS,
} };
}
RGLCustomTextureViewPtr TextureD3D12::MakeCustomTextureView(const CustomTextureViewConfig& config) const
{
return RGLCustomTextureViewPtr();
}
Dimension TextureD3D12::GetSize() const
{
return size;
Expand Down Expand Up @@ -302,5 +306,9 @@ namespace RGL {
owningDevice->CBV_SRV_UAVHeap->DeallocateSingle(handle);
}
}
TextureView CustomTextureViewD3D12::GetView() const
{
return TextureView();
}
}
#endif
6 changes: 6 additions & 0 deletions src/D3D12Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace RGL {
TextureView GetDefaultView() const final;
TextureView GetViewForMip(uint32_t mip) const final;

RGLCustomTextureViewPtr MakeCustomTextureView(const CustomTextureViewConfig& config) const;

Dimension GetSize() const final;
virtual ~TextureD3D12();
Expand All @@ -65,4 +66,9 @@ namespace RGL {

std::string debugName;
};

struct CustomTextureViewD3D12 : public ICustomTextureView {

TextureView GetView() const;
};
}
2 changes: 1 addition & 1 deletion src/MTLCommandBuffer.mm
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ uint32_t bytesPerPixel(MTLPixelFormat format){
auto blitEncoder = [currentCommandBuffer blitCommandEncoder];
auto fromTexture = TextureMTL::ViewToTexture(from.texture);
auto toTexture = TextureMTL::ViewToTexture(to.texture);
[blitEncoder copyFromTexture:fromTexture toTexture:toTexture];
[blitEncoder copyFromTexture:fromTexture sourceSlice:from.layer sourceLevel:from.mip toTexture:toTexture destinationSlice:to.layer destinationLevel:to.mip sliceCount:1 levelCount:1];
[blitEncoder endEncoding];
}

Expand Down
7 changes: 7 additions & 0 deletions src/MTLTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ struct TextureMTL : public ITexture{

TextureView GetDefaultView() const final;
TextureView GetViewForMip(uint32_t mip) const final;

RGLCustomTextureViewPtr MakeCustomTextureView(const CustomTextureViewConfig& config) const;

std::vector<OBJC_ID(MTLTexture)> mipTextures;
};

struct CustomTextureViewMTL : public ICustomTextureView {

TextureView GetView() const;
};

}
16 changes: 15 additions & 1 deletion src/MTLTexture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@
desc.mipmapLevelCount = config.mipLevels;
auto usage = rgl2mtlTextureUsage(config.usage);
desc.usage = usage;
desc.storageMode = (config.aspect.HasDepth || config.aspect.HasStencil) ? MTLStorageModePrivate :
desc.storageMode = (config.aspect.HasDepth || config.aspect.HasStencil || config.usage.ColorAttachment) ? MTLStorageModePrivate :
#if TARGET_OS_IPHONE
MTLStorageModeShared;
#else
MTLStorageModeManaged;
#endif
texture = [owningDevice->device newTextureWithDescriptor:desc];

if (config.debugName.data() != nullptr){
[texture setLabel:[NSString stringWithUTF8String:config.debugName.data()]];
}

mipTextures.reserve(config.mipLevels - 1);
for(int i = 1; i < config.mipLevels; i++){
auto tex = [texture newTextureViewWithPixelFormat:format textureType:MTLTextureType2D levels:NSMakeRange(i, 1) slices:NSMakeRange(0, 1)];
Expand Down Expand Up @@ -77,6 +81,11 @@
return TextureView::NativeHandles::mtl_t{this, mip};
}

RGLCustomTextureViewPtr TextureMTL::MakeCustomTextureView(const CustomTextureViewConfig& config) const
{
return RGLCustomTextureViewPtr();
}

id<MTLTexture> TextureMTL::ViewToTexture(const TextureView& view){
if (view.texture.mtl.mip == TextureView::NativeHandles::mtl_t::ALL_MIPS){
return view.texture.mtl.texture->texture;
Expand All @@ -86,5 +95,10 @@
}
}

TextureView RGL::CustomTextureViewMTL::GetView() const
{
return TextureView();
}

}
#endif
2 changes: 1 addition & 1 deletion src/VkBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace RGL {
allocation = createBuffer(owningDevice.get(), config.nElements * config.stride, usage, memprop, buffer);

if (config.options.debugName) {
owningDevice->SetDebugNameForResource(buffer, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, config.options.debugName);
owningDevice->SetDebugNameForResource((void*)buffer, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, config.options.debugName);
#ifndef NDEBUG
debugName = config.options.debugName;
#endif
Expand Down
31 changes: 24 additions & 7 deletions src/VkCommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "VkComputePipeline.hpp"
#include <cstring>
#include <iostream>
#include <utility>

namespace RGL {
VkAttachmentLoadOp RGL2LoadOp(LoadAccessOperation op) {
Expand Down Expand Up @@ -276,9 +277,24 @@ namespace RGL {
}
void CommandBufferVk::CopyTextureToTexture(const TextureCopyConfig& from, const TextureCopyConfig& to)
{
RecordTextureBinding(from.texture, {}, true);
RecordTextureBinding(to.texture, {}, true);
EncodeCommand(CmdCopyTextureToTexture{ from,to });
auto fromCpy = from.texture;
fromCpy.texture.vk.coveredLayers = MakeMipMaskForIndex(from.layer);
fromCpy.texture.vk.coveredMips = MakeMipMaskForIndex(from.mip);

auto toCpy = to.texture;
toCpy.texture.vk.coveredLayers = MakeMipMaskForIndex(to.layer);
toCpy.texture.vk.coveredMips = MakeMipMaskForIndex(to.mip);

RecordTextureBinding(fromCpy, {VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL}, false);
RecordTextureBinding(toCpy, { VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL }, false);

auto fromcnfcpy = from;
fromcnfcpy.texture = fromCpy;

auto tocnfcpy = to;
tocnfcpy.texture = toCpy;

EncodeCommand(CmdCopyTextureToTexture{ fromcnfcpy, tocnfcpy, from.mip, from.layer, to.mip, to.layer });
}
void CommandBufferVk::SetViewport(const Viewport& viewport)
{
Expand Down Expand Up @@ -803,22 +819,23 @@ namespace RGL {
auto dst = static_cast<const TextureVk*>(arg.to.texture.parent);

auto& srcLayout = activeTextures.at(TextureLastUseKey{src, arg.from.texture.texture.vk.coveredMips, arg.from.texture.texture.vk.coveredLayers });
auto& dstLayout = activeTextures.at(TextureLastUseKey{ src,arg.to.texture.texture.vk.coveredMips, arg.to.texture.texture.vk.coveredLayers });
auto& dstLayout = activeTextures.at(TextureLastUseKey{ dst, arg.to.texture.texture.vk.coveredMips, arg.to.texture.texture.vk.coveredLayers });

auto dim = src->GetSize();
VkImageCopy2 region{
.sType = VK_STRUCTURE_TYPE_IMAGE_COPY_2,
.pNext = nullptr,
.srcSubresource = {
.aspectMask = src->createdAspectVk,
.mipLevel = 0,
.baseArrayLayer = 0,
.mipLevel = arg.fromMip,
.baseArrayLayer = arg.fromLayer,
.layerCount = 1
},
.srcOffset = {0,0,0},
.dstSubresource = {
.aspectMask = dst->createdAspectVk,
.baseArrayLayer = 0,
.mipLevel = arg.toMip,
.baseArrayLayer = arg.toLayer,
.layerCount = 1
},
.dstOffset = {0,0,0},
Expand Down
Loading

0 comments on commit c1bfc29

Please sign in to comment.