Skip to content

Commit

Permalink
Fix ownership (#41)
Browse files Browse the repository at this point in the history
* fix ownership

* fix cleanup
  • Loading branch information
skallweitNV authored Sep 17, 2024
1 parent 9de0d94 commit 22ff1f8
Show file tree
Hide file tree
Showing 43 changed files with 146 additions and 162 deletions.
4 changes: 2 additions & 2 deletions src/cpu/cpu-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace rhi::cpu {
class BufferImpl : public Buffer
{
public:
BufferImpl(Device* device, const BufferDesc& desc)
: Buffer(device, desc)
BufferImpl(const BufferDesc& desc)
: Buffer(desc)
{
}

Expand Down
8 changes: 3 additions & 5 deletions src/cpu/cpu-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ Result DeviceImpl::initialize(const Desc& desc)
Result DeviceImpl::createTexture(const TextureDesc& desc, const SubresourceData* initData, ITexture** outTexture)
{
TextureDesc srcDesc = fixupTextureDesc(desc);

RefPtr<TextureImpl> texture = new TextureImpl(this, srcDesc);

RefPtr<TextureImpl> texture = new TextureImpl(srcDesc);
SLANG_RETURN_ON_FAIL(texture->init(initData));

returnComPtr(outTexture, texture);
Expand All @@ -64,7 +62,7 @@ Result DeviceImpl::createTexture(const TextureDesc& desc, const SubresourceData*
Result DeviceImpl::createBuffer(const BufferDesc& descIn, const void* initData, IBuffer** outBuffer)
{
auto desc = fixupBufferDesc(descIn);
RefPtr<BufferImpl> buffer = new BufferImpl(this, desc);
RefPtr<BufferImpl> buffer = new BufferImpl(desc);
SLANG_RETURN_ON_FAIL(buffer->init());
if (initData)
{
Expand All @@ -76,7 +74,7 @@ Result DeviceImpl::createBuffer(const BufferDesc& descIn, const void* initData,

Result DeviceImpl::createTextureView(ITexture* texture, const TextureViewDesc& desc, ITextureView** outView)
{
RefPtr<TextureViewImpl> view = new TextureViewImpl(this, desc);
RefPtr<TextureViewImpl> view = new TextureViewImpl(desc);
view->m_texture = static_cast<TextureImpl*>(texture);
if (view->m_desc.format == Format::Unknown)
view->m_desc.format = view->m_texture->m_desc.format;
Expand Down
2 changes: 1 addition & 1 deletion src/cpu/cpu-shader-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Buffer* CPUShaderObjectData::getBufferResource(
{
BufferDesc desc = {};
desc.elementSize = (int)elementLayout->getSize();
m_buffer = new BufferImpl(device, desc);
m_buffer = new BufferImpl(desc);
}
m_buffer->m_desc.size = m_ordinaryData.size();
return m_buffer.get();
Expand Down
4 changes: 2 additions & 2 deletions src/cpu/cpu-texture-view.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace rhi::cpu {
class TextureViewImpl : public TextureView, public slang_prelude::IRWTexture
{
public:
TextureViewImpl(Device* device, const TextureViewDesc& desc)
: TextureView(device, desc)
TextureViewImpl(const TextureViewDesc& desc)
: TextureView(desc)
{
}

Expand Down
5 changes: 3 additions & 2 deletions src/cpu/cpu-texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ class TextureImpl : public Texture
};

public:
TextureImpl(Device* device, const TextureDesc& desc)
: Texture(device, desc)
TextureImpl(const TextureDesc& desc)
: Texture(desc)
{
}

~TextureImpl();

Result init(SubresourceData const* initData);
Expand Down
4 changes: 2 additions & 2 deletions src/cuda/cuda-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace rhi::cuda {
class BufferImpl : public Buffer
{
public:
BufferImpl(Device* device, const BufferDesc& desc)
: Buffer(device, desc)
BufferImpl(const BufferDesc& desc)
: Buffer(desc)
{
}

Expand Down
10 changes: 5 additions & 5 deletions src/cuda/cuda-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ Result DeviceImpl::createTexture(const TextureDesc& desc, const SubresourceData*
{
TextureDesc srcDesc = fixupTextureDesc(desc);

RefPtr<TextureImpl> tex = new TextureImpl(this, srcDesc);
RefPtr<TextureImpl> tex = new TextureImpl(srcDesc);

CUresourcetype resourceType;

Expand Down Expand Up @@ -725,7 +725,7 @@ Result DeviceImpl::createTexture(const TextureDesc& desc, const SubresourceData*
Result DeviceImpl::createBuffer(const BufferDesc& descIn, const void* initData, IBuffer** outBuffer)
{
auto desc = fixupBufferDesc(descIn);
RefPtr<BufferImpl> buffer = new BufferImpl(this, desc);
RefPtr<BufferImpl> buffer = new BufferImpl(desc);
SLANG_CUDA_RETURN_ON_FAIL(cuMemAllocManaged((CUdeviceptr*)(&buffer->m_cudaMemory), desc.size, CU_MEM_ATTACH_GLOBAL)
);
if (initData)
Expand All @@ -744,7 +744,7 @@ Result DeviceImpl::createBufferFromSharedHandle(NativeHandle handle, const Buffe
return SLANG_OK;
}

RefPtr<BufferImpl> buffer = new BufferImpl(this, desc);
RefPtr<BufferImpl> buffer = new BufferImpl(desc);

// CUDA manages sharing of buffers through the idea of an
// "external memory" object, which represents the relationship
Expand Down Expand Up @@ -808,7 +808,7 @@ Result DeviceImpl::createTextureFromSharedHandle(
return SLANG_OK;
}

RefPtr<TextureImpl> texture = new TextureImpl(this, desc);
RefPtr<TextureImpl> texture = new TextureImpl(desc);

// CUDA manages sharing of buffers through the idea of an
// "external memory" object, which represents the relationship
Expand Down Expand Up @@ -886,7 +886,7 @@ Result DeviceImpl::createTextureFromSharedHandle(

Result DeviceImpl::createTextureView(ITexture* texture, const TextureViewDesc& desc, ITextureView** outView)
{
RefPtr<TextureViewImpl> view = new TextureViewImpl(this, desc);
RefPtr<TextureViewImpl> view = new TextureViewImpl(desc);
view->m_texture = static_cast<TextureImpl*>(texture);
if (view->m_desc.format == Format::Unknown)
view->m_desc.format = view->m_texture->m_desc.format;
Expand Down
4 changes: 2 additions & 2 deletions src/cuda/cuda-shader-object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Result ShaderObjectData::setCount(Index count)
{
BufferDesc desc;
desc.size = count;
m_buffer = new BufferImpl(device, desc);
m_buffer = new BufferImpl(desc);
}
m_buffer->m_cpuBuffer = m_cpuBuffer.data();
m_buffer->m_desc.size = count;
Expand All @@ -25,7 +25,7 @@ Result ShaderObjectData::setCount(Index count)
{
BufferDesc desc;
desc.size = count;
m_buffer = new BufferImpl(device, desc);
m_buffer = new BufferImpl(desc);
if (count)
{
SLANG_CUDA_RETURN_ON_FAIL(cuMemAlloc((CUdeviceptr*)&m_buffer->m_cudaMemory, (size_t)count));
Expand Down
4 changes: 2 additions & 2 deletions src/cuda/cuda-texture-view.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace rhi::cuda {
class TextureViewImpl : public TextureView
{
public:
TextureViewImpl(Device* device, const TextureViewDesc& desc)
: TextureView(device, desc)
TextureViewImpl(const TextureViewDesc& desc)
: TextureView(desc)
{
}

Expand Down
5 changes: 3 additions & 2 deletions src/cuda/cuda-texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ namespace rhi::cuda {
class TextureImpl : public Texture
{
public:
TextureImpl(Device* device, const TextureDesc& desc)
: Texture(device, desc)
TextureImpl(const TextureDesc& desc)
: Texture(desc)
{
}

~TextureImpl();

uint64_t getBindlessHandle();
Expand Down
6 changes: 2 additions & 4 deletions src/d3d11/d3d11-buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ ID3D11ShaderResourceView* BufferImpl::getSRV(Format format, const BufferRange& r
srvDesc.Buffer.NumElements = UINT(range.size / (sizeInfo.blockSizeInBytes / sizeInfo.pixelsPerBlock));
}

ID3D11Device* device = static_cast<DeviceImpl*>(m_device.get())->m_device;
SLANG_RETURN_NULL_ON_FAIL(device->CreateShaderResourceView(m_buffer, &srvDesc, srv.writeRef()));
SLANG_RETURN_NULL_ON_FAIL(m_device->m_device->CreateShaderResourceView(m_buffer, &srvDesc, srv.writeRef()));

return srv.get();
}
Expand Down Expand Up @@ -94,8 +93,7 @@ ID3D11UnorderedAccessView* BufferImpl::getUAV(Format format, const BufferRange&
uavDesc.Buffer.NumElements = UINT(range.size / (sizeInfo.blockSizeInBytes / sizeInfo.pixelsPerBlock));
}

DeviceImpl* device = static_cast<DeviceImpl*>(m_device.get());
SLANG_RETURN_NULL_ON_FAIL(device->m_device->CreateUnorderedAccessView(m_buffer, &uavDesc, uav.writeRef()));
SLANG_RETURN_NULL_ON_FAIL(m_device->m_device->CreateUnorderedAccessView(m_buffer, &uavDesc, uav.writeRef()));

return uav.get();
}
Expand Down
6 changes: 4 additions & 2 deletions src/d3d11/d3d11-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ namespace rhi::d3d11 {
class BufferImpl : public Buffer
{
public:
BufferImpl(Device* device, const BufferDesc& desc)
: Buffer(device, desc)
BufferImpl(DeviceImpl* device, const BufferDesc& desc)
: Buffer(desc)
, m_device(device)
{
}

DeviceImpl* m_device;
MapFlavor m_mapFlavor;
D3D11_USAGE m_d3dUsage;
ComPtr<ID3D11Buffer> m_buffer;
Expand Down
4 changes: 2 additions & 2 deletions src/d3d11/d3d11-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,15 +736,15 @@ Result DeviceImpl::createSampler(SamplerDesc const& desc, ISampler** outSampler)
ComPtr<ID3D11SamplerState> sampler;
SLANG_RETURN_ON_FAIL(m_device->CreateSamplerState(&dxDesc, sampler.writeRef()));

RefPtr<SamplerImpl> samplerImpl = new SamplerImpl(this, desc);
RefPtr<SamplerImpl> samplerImpl = new SamplerImpl(desc);
samplerImpl->m_sampler = sampler;
returnComPtr(outSampler, samplerImpl);
return SLANG_OK;
}

Result DeviceImpl::createTextureView(ITexture* texture, const TextureViewDesc& desc, ITextureView** outView)
{
RefPtr<TextureViewImpl> view = new TextureViewImpl(this, desc);
RefPtr<TextureViewImpl> view = new TextureViewImpl(desc);
view->m_texture = static_cast<TextureImpl*>(texture);
if (view->m_desc.format == Format::Unknown)
view->m_desc.format = view->m_texture->m_desc.format;
Expand Down
4 changes: 2 additions & 2 deletions src/d3d11/d3d11-sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace rhi::d3d11 {
class SamplerImpl : public Sampler
{
public:
SamplerImpl(Device* device, const SamplerDesc& desc)
: Sampler(device, desc)
SamplerImpl(const SamplerDesc& desc)
: Sampler(desc)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/d3d11/d3d11-texture-view.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace rhi::d3d11 {
class TextureViewImpl : public TextureView
{
public:
TextureViewImpl(Device* device, const TextureViewDesc& desc)
: TextureView(device, desc)
TextureViewImpl(const TextureViewDesc& desc)
: TextureView(desc)
{
}

Expand Down
12 changes: 4 additions & 8 deletions src/d3d11/d3d11-texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ ID3D11RenderTargetView* TextureImpl::getRTV(Format format, const SubresourceRang
if (rtv)
return rtv;

DeviceImpl* device = static_cast<DeviceImpl*>(m_device.get());
SLANG_RETURN_NULL_ON_FAIL(device->m_device->CreateRenderTargetView(m_resource, nullptr, rtv.writeRef()));
SLANG_RETURN_NULL_ON_FAIL(m_device->m_device->CreateRenderTargetView(m_resource, nullptr, rtv.writeRef()));

return rtv;
}
Expand All @@ -28,8 +27,7 @@ ID3D11DepthStencilView* TextureImpl::getDSV(Format format, const SubresourceRang
if (dsv)
return dsv;

DeviceImpl* device = static_cast<DeviceImpl*>(m_device.get());
SLANG_RETURN_NULL_ON_FAIL(device->m_device->CreateDepthStencilView(m_resource, nullptr, dsv.writeRef()));
SLANG_RETURN_NULL_ON_FAIL(m_device->m_device->CreateDepthStencilView(m_resource, nullptr, dsv.writeRef()));

return dsv;
}
Expand All @@ -47,8 +45,7 @@ ID3D11ShaderResourceView* TextureImpl::getSRV(Format format, const SubresourceRa
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
initSrvDesc(m_desc, D3DUtil::getMapFormat(format), srvDesc);

DeviceImpl* device = static_cast<DeviceImpl*>(m_device.get());
SLANG_RETURN_NULL_ON_FAIL(device->m_device->CreateShaderResourceView(m_resource, &srvDesc, srv.writeRef()));
SLANG_RETURN_NULL_ON_FAIL(m_device->m_device->CreateShaderResourceView(m_resource, &srvDesc, srv.writeRef()));

return srv;
}
Expand All @@ -62,8 +59,7 @@ ID3D11UnorderedAccessView* TextureImpl::getUAV(Format format, const SubresourceR
if (uav)
return uav;

DeviceImpl* device = static_cast<DeviceImpl*>(m_device.get());
SLANG_RETURN_NULL_ON_FAIL(device->m_device->CreateUnorderedAccessView(m_resource, nullptr, uav.writeRef()));
SLANG_RETURN_NULL_ON_FAIL(m_device->m_device->CreateUnorderedAccessView(m_resource, nullptr, uav.writeRef()));

return uav;
}
Expand Down
6 changes: 4 additions & 2 deletions src/d3d11/d3d11-texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ namespace rhi::d3d11 {
class TextureImpl : public Texture
{
public:
TextureImpl(Device* device, const TextureDesc& desc)
: Texture(device, desc)
TextureImpl(DeviceImpl* device, const TextureDesc& desc)
: Texture(desc)
, m_device(device)
{
}

DeviceImpl* m_device;
ComPtr<ID3D11Resource> m_resource;

struct ViewKey
Expand Down
20 changes: 9 additions & 11 deletions src/d3d12/d3d12-buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@

namespace rhi::d3d12 {

BufferImpl::BufferImpl(Device* device, const BufferDesc& desc)
: Buffer(device, desc)
BufferImpl::BufferImpl(DeviceImpl* device, const BufferDesc& desc)
: Buffer(desc)
, m_device(device)
, m_defaultState(D3DUtil::getResourceState(desc.defaultState))
{
}

BufferImpl::~BufferImpl()
{
DeviceImpl* device = static_cast<DeviceImpl*>(m_device.get());
for (auto& srv : m_srvs)
{
if (srv.second)
{
device->m_cpuViewHeap->free(srv.second);
m_device->m_cpuViewHeap->free(srv.second);
}
}
for (auto& uav : m_uavs)
{
if (uav.second)
{
device->m_cpuViewHeap->free(uav.second);
m_device->m_cpuViewHeap->free(uav.second);
}
}

Expand Down Expand Up @@ -127,9 +127,8 @@ D3D12Descriptor BufferImpl::getSRV(Format format, uint32_t stride, const BufferR
viewDesc.Buffer.NumElements = UINT(range.size / sizeInfo.blockSizeInBytes);
}

DeviceImpl* device = static_cast<DeviceImpl*>(m_device.get());
device->m_cpuViewHeap->allocate(&descriptor);
device->m_device->CreateShaderResourceView(m_resource.getResource(), &viewDesc, descriptor.cpuHandle);
m_device->m_cpuViewHeap->allocate(&descriptor);
m_device->m_device->CreateShaderResourceView(m_resource.getResource(), &viewDesc, descriptor.cpuHandle);

return descriptor;
}
Expand Down Expand Up @@ -166,9 +165,8 @@ D3D12Descriptor BufferImpl::getUAV(Format format, uint32_t stride, const BufferR
viewDesc.Buffer.NumElements = UINT(range.size / sizeInfo.blockSizeInBytes);
}

DeviceImpl* device = static_cast<DeviceImpl*>(m_device.get());
device->m_cpuViewHeap->allocate(&descriptor);
device->m_device->CreateUnorderedAccessView(m_resource.getResource(), nullptr, &viewDesc, descriptor.cpuHandle);
m_device->m_cpuViewHeap->allocate(&descriptor);
m_device->m_device->CreateUnorderedAccessView(m_resource.getResource(), nullptr, &viewDesc, descriptor.cpuHandle);

return descriptor;
}
Expand Down
3 changes: 2 additions & 1 deletion src/d3d12/d3d12-buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ namespace rhi::d3d12 {
class BufferImpl : public Buffer
{
public:
BufferImpl(Device* device, const BufferDesc& desc);
BufferImpl(DeviceImpl* device, const BufferDesc& desc);

~BufferImpl();

DeviceImpl* m_device;
/// The resource in gpu memory, allocated on the correct heap relative to the cpu access flag
D3D12Resource m_resource;
D3D12_RESOURCE_STATES m_defaultState;
Expand Down
4 changes: 2 additions & 2 deletions src/d3d12/d3d12-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ Result DeviceImpl::createSampler(SamplerDesc const& desc, ISampler** outSampler)
// entries that we check before we go to the heap, and then
// when we are done with a sampler we simply add it to the free list.
//
RefPtr<SamplerImpl> samplerImpl = new SamplerImpl(this, desc);
RefPtr<SamplerImpl> samplerImpl = new SamplerImpl(desc);
samplerImpl->m_allocator = samplerHeap;
samplerImpl->m_descriptor = cpuDescriptor;
returnComPtr(outSampler, samplerImpl);
Expand All @@ -1343,7 +1343,7 @@ Result DeviceImpl::createSampler(SamplerDesc const& desc, ISampler** outSampler)

Result DeviceImpl::createTextureView(ITexture* texture, const TextureViewDesc& desc, ITextureView** outView)
{
RefPtr<TextureViewImpl> view = new TextureViewImpl(this, desc);
RefPtr<TextureViewImpl> view = new TextureViewImpl(desc);
view->m_texture = static_cast<TextureImpl*>(texture);
if (view->m_desc.format == Format::Unknown)
view->m_desc.format = view->m_texture->m_desc.format;
Expand Down
Loading

0 comments on commit 22ff1f8

Please sign in to comment.