Skip to content

Commit 969d0c6

Browse files
committed
♻️🚧 Split Render API source files in header+source files.
For RenderAPI DX11 I add methods to create and copy texture see #2
1 parent 59d2482 commit 969d0c6

File tree

9 files changed

+187
-891
lines changed

9 files changed

+187
-891
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
#include "renderAPI.h"
3+
#include "framework.h"
4+
#include "log.h"
5+
// Direct3D 11 implementation of RenderAPI.
6+
7+
#if SUPPORT_D3D11
8+
9+
#include "d3d11.h"
10+
#include "IUnityGraphicsD3D11.h"
11+
#include <assert.h>
12+
13+
class RenderAPI_D3D11 : public RenderAPI
14+
{
15+
public:
16+
RenderAPI_D3D11();
17+
virtual ~RenderAPI_D3D11();
18+
virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type,
19+
IUnityInterfaces *interfaces);
20+
21+
22+
23+
/// <summary>
24+
/// Create a 2D texture with the given paramaters
25+
/// return -1, if device has not been set, -2 if dx11
26+
/// failed to create the texture, else 0
27+
/// </summary>
28+
int createTexture2D(int textureWidth, int textureHeight, int textureDepth,
29+
ID3D11Texture2D **textureHandle);
30+
31+
/// <summary>
32+
/// Copy the content of texture 2D src to texture 2D dest
33+
/// This method is UNSAFE, because for efficiency reason it
34+
/// doesn't check if src and dest textures are compatible
35+
/// Therefore, make sure to have compatible textures. See
36+
/// https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-id3d11devicecontext-copyresource
37+
/// for more details
38+
/// </summary>
39+
void copyTextures2D(ID3D11Texture2D* dest, ID3D11Texture2D* src);
40+
41+
42+
private:
43+
ID3D11Device *_device;
44+
ID3D11DeviceContext* _context{};
45+
};
46+
47+
RenderAPI *CreateRenderAPI_D3D11();
48+
49+
#endif // #if SUPPORT_D3D11
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
#include "framework.h"
3+
#include "renderAPI.h"
4+
5+
#include <cmath>
6+
7+
// Direct3D 12 implementation of RenderAPI.
8+
9+
#if SUPPORT_D3D12
10+
11+
#include <assert.h>
12+
#include <d3d12.h>
13+
#include "IUnityGraphicsD3D12.h"
14+
15+
class RenderAPI_D3D12 : public RenderAPI
16+
{
17+
public:
18+
RenderAPI_D3D12();
19+
virtual ~RenderAPI_D3D12();
20+
21+
virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type,
22+
IUnityInterfaces *interfaces);
23+
24+
private:
25+
void CreateResources();
26+
void ReleaseResources();
27+
28+
private:
29+
IUnityGraphicsD3D12v2 *s_D3D12;
30+
ID3D12CommandAllocator *s_D3D12CmdAlloc;
31+
ID3D12GraphicsCommandList *s_D3D12CmdList;
32+
UINT64 s_D3D12FenceValue = 0;
33+
HANDLE s_D3D12Event = NULL;
34+
};
35+
36+
RenderAPI *CreateRenderAPI_D3D12();
37+
38+
const UINT kNodeMask = 0;
39+
40+
#endif // #if SUPPORT_D3D12
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
#include "openGL_include.h"
3+
#include "renderAPI.h"
4+
#include "framework.h"
5+
6+
// OpenGL Core profile (desktop) or OpenGL ES (mobile) implementation of RenderAPI.
7+
// Supports several flavors: Core, ES2, ES3
8+
9+
10+
#if SUPPORT_OPENGL_UNIFIED
11+
12+
class RenderAPI_OpenGLCoreES : public RenderAPI
13+
{
14+
public:
15+
RenderAPI_OpenGLCoreES(UnityGfxRenderer apiType);
16+
virtual ~RenderAPI_OpenGLCoreES();
17+
18+
19+
virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces);
20+
private:
21+
void CreateResources();
22+
23+
private:
24+
UnityGfxRenderer m_APIType;
25+
};
26+
27+
28+
RenderAPI* CreateRenderAPI_OpenGLCoreES(UnityGfxRenderer apiType);
29+
30+
31+
#endif // #if SUPPORT_OPENGL_UNIFIED

Plugin/PluginInteropUnityCUDA/src/RenderAPI/renderAPI.cpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,6 @@ RenderAPI* CreateRenderAPI(UnityGfxRenderer apiType)
3030
}
3131
# endif // if SUPPORT_OPENGL_UNIFIED
3232

33-
# if SUPPORT_METAL
34-
if (apiType == kUnityGfxRendererMetal)
35-
{
36-
extern RenderAPI* CreateRenderAPI_Metal();
37-
return CreateRenderAPI_Metal();
38-
}
39-
# endif // if SUPPORT_METAL
40-
41-
# if SUPPORT_VULKAN
42-
if (apiType == kUnityGfxRendererVulkan)
43-
{
44-
extern RenderAPI* CreateRenderAPI_Vulkan();
45-
return CreateRenderAPI_Vulkan();
46-
}
47-
# endif // if SUPPORT_VULKAN
48-
4933
// Unknown or unsupported graphics API
5034
return NULL;
5135
}

Plugin/PluginInteropUnityCUDA/src/RenderAPI/renderAPI_D3D11.cpp

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,80 @@
1-
#include "framework.h"
2-
#include "renderAPI.h"
3-
#include "log.h"
4-
// Direct3D 11 implementation of RenderAPI.
1+
#include "RenderAPI_D3D11.h"
52

63
#if SUPPORT_D3D11
74

8-
#include <assert.h>
9-
#include <d3d11.h>
10-
#include "IUnityGraphicsD3D11.h"
11-
12-
class RenderAPI_D3D11 : public RenderAPI
5+
RenderAPI *CreateRenderAPI_D3D11()
136
{
14-
public:
15-
RenderAPI_D3D11();
16-
virtual ~RenderAPI_D3D11()
17-
{
18-
}
7+
return new RenderAPI_D3D11();
8+
}
199

20-
virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type,
21-
IUnityInterfaces *interfaces);
10+
RenderAPI_D3D11::RenderAPI_D3D11() : _device(NULL)
11+
{
12+
}
2213

23-
private:
24-
ID3D11Device *m_Device;
25-
};
14+
RenderAPI_D3D11::~RenderAPI_D3D11()
15+
{
16+
}
2617

27-
RenderAPI *CreateRenderAPI_D3D11()
18+
int RenderAPI_D3D11::createTexture2D(int textureWidth, int textureHeight,
19+
int textureDepth,
20+
ID3D11Texture2D **textureHandle)
2821
{
29-
return new RenderAPI_D3D11();
22+
D3D11_TEXTURE2D_DESC texDesc;
23+
ZeroMemory(&texDesc, sizeof(texDesc));
24+
texDesc.Width = textureWidth;
25+
texDesc.Height = textureHeight;
26+
texDesc.MipLevels = 1;
27+
texDesc.ArraySize = textureDepth;
28+
texDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
29+
texDesc.SampleDesc.Count = 1;
30+
texDesc.Usage = D3D11_USAGE_DEFAULT;
31+
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
32+
texDesc.CPUAccessFlags = 0;
33+
texDesc.MiscFlags = 0;
34+
35+
if (_device == NULL)
36+
{
37+
Log::log().debugLogError(
38+
"m_Device has not been initialized in RenderAPI_D3D11, please make "
39+
"sure event kUnityGfxDeviceEventInitialize has been called "
40+
"before.");
41+
return -1;
42+
}
43+
44+
HRESULT hr = _device->CreateTexture2D(&texDesc, nullptr, textureHandle);
45+
if (FAILED(hr))
46+
{
47+
Log::log().debugLogError("Error " + std::to_string(hr) +
48+
" when creating Texture in DX11.");
49+
return -2;
50+
}
51+
return 0;
3052
}
3153

32-
RenderAPI_D3D11::RenderAPI_D3D11() : m_Device(NULL)
54+
void RenderAPI_D3D11::copyTextures2D(ID3D11Texture2D *dest,
55+
ID3D11Texture2D *src)
3356
{
57+
_device->GetImmediateContext(&_context);
58+
_context->CopyResource(dest, src);
3459
}
3560

61+
3662
void RenderAPI_D3D11::ProcessDeviceEvent(UnityGfxDeviceEventType type,
3763
IUnityInterfaces *interfaces)
3864
{
3965
switch (type)
4066
{
4167
case kUnityGfxDeviceEventInitialize: {
42-
Log::log().debugLog("init dx11");
4368
IUnityGraphicsD3D11 *d3d = interfaces->Get<IUnityGraphicsD3D11>();
44-
m_Device = d3d->GetDevice();
45-
46-
ID3D11DeviceContext *ctx = NULL;
47-
m_Device->GetImmediateContext(&ctx);
48-
Log::log().debugLog("end dx11");
69+
_device = d3d->GetDevice();
4970
break;
5071
}
5172
case kUnityGfxDeviceEventShutdown:
5273
break;
74+
case kUnityGfxDeviceEventBeforeReset:
75+
break;
76+
case kUnityGfxDeviceEventAfterReset:
77+
break;
5378
}
5479
}
5580

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,13 @@
1-
#include "renderAPI.h"
2-
#include "framework.h"
3-
4-
#include <cmath>
5-
6-
// Direct3D 12 implementation of RenderAPI.
7-
1+
#include "renderAPI_D3D12.h"
82

93
#if SUPPORT_D3D12
104

11-
#include <assert.h>
12-
#include <d3d12.h>
13-
#include "IUnityGraphicsD3D12.h"
14-
15-
16-
class RenderAPI_D3D12 : public RenderAPI
17-
{
18-
public:
19-
RenderAPI_D3D12();
20-
virtual ~RenderAPI_D3D12() { }
21-
22-
virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces);
23-
24-
private:
25-
void CreateResources();
26-
void ReleaseResources();
27-
28-
private:
29-
IUnityGraphicsD3D12v2* s_D3D12;
30-
ID3D12CommandAllocator* s_D3D12CmdAlloc;
31-
ID3D12GraphicsCommandList* s_D3D12CmdList;
32-
UINT64 s_D3D12FenceValue = 0;
33-
HANDLE s_D3D12Event = NULL;
34-
};
35-
36-
375
RenderAPI* CreateRenderAPI_D3D12()
386
{
397
return new RenderAPI_D3D12();
408
}
419

4210

43-
const UINT kNodeMask = 0;
44-
4511

4612
RenderAPI_D3D12::RenderAPI_D3D12()
4713
: s_D3D12(NULL)
@@ -52,6 +18,10 @@ RenderAPI_D3D12::RenderAPI_D3D12()
5218
{
5319
}
5420

21+
RenderAPI_D3D12::~RenderAPI_D3D12()
22+
{
23+
24+
}
5525

5626
void RenderAPI_D3D12::CreateResources()
5727
{
@@ -92,7 +62,10 @@ void RenderAPI_D3D12::ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInt
9262
case kUnityGfxDeviceEventShutdown:
9363
ReleaseResources();
9464
break;
95-
}
65+
case kUnityGfxDeviceEventBeforeReset:
66+
case kUnityGfxDeviceEventAfterReset:
67+
break;
68+
}
9669
}
9770

9871
#endif // #if SUPPORT_D3D12

Plugin/PluginInteropUnityCUDA/src/RenderAPI/renderAPI_OpenGLCoreES.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,7 @@
1-
#include "openGL_include.h"
2-
#include "renderAPI.h"
3-
#include "framework.h"
4-
5-
// OpenGL Core profile (desktop) or OpenGL ES (mobile) implementation of RenderAPI.
6-
// Supports several flavors: Core, ES2, ES3
7-
1+
#include "renderAPI_OpenGLCoreES.h"
82

93
#if SUPPORT_OPENGL_UNIFIED
104

11-
class RenderAPI_OpenGLCoreES : public RenderAPI
12-
{
13-
public:
14-
RenderAPI_OpenGLCoreES(UnityGfxRenderer apiType);
15-
virtual ~RenderAPI_OpenGLCoreES() { }
16-
17-
virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces);
18-
private:
19-
void CreateResources();
20-
21-
private:
22-
UnityGfxRenderer m_APIType;
23-
};
24-
25-
265
RenderAPI* CreateRenderAPI_OpenGLCoreES(UnityGfxRenderer apiType)
276
{
287
return new RenderAPI_OpenGLCoreES(apiType);
@@ -37,12 +16,15 @@ void RenderAPI_OpenGLCoreES::CreateResources()
3716

3817

3918

40-
4119
RenderAPI_OpenGLCoreES::RenderAPI_OpenGLCoreES(UnityGfxRenderer apiType)
4220
: m_APIType(apiType)
4321
{
4422
}
4523

24+
RenderAPI_OpenGLCoreES::~RenderAPI_OpenGLCoreES()
25+
{
26+
27+
}
4628

4729
void RenderAPI_OpenGLCoreES::ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces)
4830
{

0 commit comments

Comments
 (0)