Skip to content

Commit 126f7b7

Browse files
committed
✨🚧 Add a first version of dx11 support
1 parent aaf7293 commit 126f7b7

File tree

6 files changed

+210
-117
lines changed

6 files changed

+210
-117
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
// Direct3D 11 implementation of Vertex Buffer API
3+
#include "vertex_buffer.h"
4+
5+
6+
#if SUPPORT_D3D11
7+
8+
#include <d3d11.h>
9+
#include <cuda_d3d11_interop.h>
10+
#include "IUnityGraphicsD3D11.h"
11+
12+
class VertexBuffer_D3D11 : public VertexBuffer
13+
{
14+
public:
15+
VertexBuffer_D3D11(void* bufferHandle, int size);
16+
~VertexBuffer_D3D11();
17+
virtual void registerBufferInCUDA();
18+
virtual void unRegisterBufferInCUDA();
19+
20+
};
21+
22+
#endif
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
// Direct3D 11 implementation of Texture API
3+
#include "texture.h"
4+
5+
6+
#if SUPPORT_D3D11
7+
8+
#include <d3d11.h>
9+
#include <cuda_d3d11_interop.h>
10+
#include "IUnityGraphicsD3D11.h"
11+
#include <assert.h>
12+
13+
class Texture_D3D11 : public Texture
14+
{
15+
public:
16+
Texture_D3D11(void* textureHandle, int textureWidth, int textureHeight, int textureDepth);
17+
~Texture_D3D11();
18+
virtual void registerTextureInCUDA();
19+
virtual void unRegisterTextureInCUDA();
20+
21+
};
22+
23+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
#include "vertex_buffer_D3D11.h"
3+
4+
5+
6+
#if SUPPORT_D3D11
7+
8+
VertexBuffer_D3D11::VertexBuffer_D3D11(void* bufferHandle, int size)
9+
: VertexBuffer(bufferHandle, size)
10+
{}
11+
12+
VertexBuffer_D3D11::~VertexBuffer_D3D11()
13+
{
14+
// final check to be sure there was no mistake
15+
CUDA_CHECK(cudaGetLastError());
16+
};
17+
18+
/// <summary>
19+
/// Register the buffer from OpenGL to CUDA
20+
/// </summary>
21+
void VertexBuffer_D3D11::registerBufferInCUDA()
22+
{
23+
Log::log().debugLog("try register");
24+
// register the texture to cuda : it initialize the _pGraphicsResource
25+
CUDA_CHECK(cudaGraphicsD3D11RegisterResource(&_pGraphicsResource, (ID3D11Resource*)_bufferHandle, cudaGraphicsRegisterFlagsWriteDiscard));
26+
Log::log().debugLog("register");}
27+
28+
void VertexBuffer_D3D11::unRegisterBufferInCUDA()
29+
{
30+
CUDA_CHECK(cudaGraphicsUnregisterResource(_pGraphicsResource));
31+
}
32+
33+
34+
#endif // #if SUPPORT_D3D11
Lines changed: 31 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "renderAPI.h"
21
#include "framework.h"
3-
2+
#include "renderAPI.h"
3+
#include "log.h"
44
// Direct3D 11 implementation of RenderAPI.
55

66
#if SUPPORT_D3D11
@@ -9,91 +9,48 @@
99
#include <d3d11.h>
1010
#include "IUnityGraphicsD3D11.h"
1111

12-
1312
class RenderAPI_D3D11 : public RenderAPI
1413
{
15-
public:
16-
RenderAPI_D3D11();
17-
virtual ~RenderAPI_D3D11() { }
18-
19-
virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces);
14+
public:
15+
RenderAPI_D3D11();
16+
virtual ~RenderAPI_D3D11()
17+
{
18+
}
2019

21-
private:
22-
void CreateResources();
23-
void ReleaseResources();
20+
virtual void ProcessDeviceEvent(UnityGfxDeviceEventType type,
21+
IUnityInterfaces *interfaces);
2422

25-
private:
26-
ID3D11Device* m_Device;
27-
ID3D11Buffer* m_VB; // vertex buffer
28-
ID3D11Buffer* m_CB; // constant buffer
29-
ID3D11VertexShader* m_VertexShader;
30-
ID3D11PixelShader* m_PixelShader;
31-
ID3D11InputLayout* m_InputLayout;
32-
ID3D11RasterizerState* m_RasterState;
33-
ID3D11BlendState* m_BlendState;
34-
ID3D11DepthStencilState* m_DepthState;
23+
private:
24+
ID3D11Device *m_Device;
3525
};
3626

37-
38-
RenderAPI* CreateRenderAPI_D3D11()
39-
{
40-
return new RenderAPI_D3D11();
41-
}
42-
43-
44-
45-
RenderAPI_D3D11::RenderAPI_D3D11()
46-
: m_Device(NULL)
47-
, m_VB(NULL)
48-
, m_CB(NULL)
49-
, m_VertexShader(NULL)
50-
, m_PixelShader(NULL)
51-
, m_InputLayout(NULL)
52-
, m_RasterState(NULL)
53-
, m_BlendState(NULL)
54-
, m_DepthState(NULL)
27+
RenderAPI *CreateRenderAPI_D3D11()
5528
{
29+
return new RenderAPI_D3D11();
5630
}
5731

58-
59-
void RenderAPI_D3D11::ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces* interfaces)
32+
RenderAPI_D3D11::RenderAPI_D3D11() : m_Device(NULL)
6033
{
61-
switch (type)
62-
{
63-
case kUnityGfxDeviceEventInitialize:
64-
{
65-
IUnityGraphicsD3D11* d3d = interfaces->Get<IUnityGraphicsD3D11>();
66-
m_Device = d3d->GetDevice();
67-
CreateResources();
68-
break;
69-
}
70-
case kUnityGfxDeviceEventShutdown:
71-
ReleaseResources();
72-
break;
73-
}
7434
}
7535

76-
77-
void RenderAPI_D3D11::CreateResources()
36+
void RenderAPI_D3D11::ProcessDeviceEvent(UnityGfxDeviceEventType type,
37+
IUnityInterfaces *interfaces)
7838
{
79-
80-
39+
switch (type)
40+
{
41+
case kUnityGfxDeviceEventInitialize: {
42+
Log::log().debugLog("init dx11");
43+
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");
49+
break;
50+
}
51+
case kUnityGfxDeviceEventShutdown:
52+
break;
53+
}
8154
}
8255

83-
84-
void RenderAPI_D3D11::ReleaseResources()
85-
{
86-
SAFE_RELEASE(m_VB);
87-
SAFE_RELEASE(m_CB);
88-
SAFE_RELEASE(m_VertexShader);
89-
SAFE_RELEASE(m_PixelShader);
90-
SAFE_RELEASE(m_InputLayout);
91-
SAFE_RELEASE(m_RasterState);
92-
SAFE_RELEASE(m_BlendState);
93-
SAFE_RELEASE(m_DepthState);
94-
}
95-
96-
97-
98-
9956
#endif // #if SUPPORT_D3D11
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
#include "texture_D3D11.h"
3+
4+
#if SUPPORT_D3D11
5+
6+
Texture_D3D11::Texture_D3D11(void *textureHandle, int textureWidth,
7+
int textureHeight, int textureDepth)
8+
: Texture(textureHandle, textureWidth, textureHeight, textureDepth)
9+
{
10+
}
11+
12+
Texture_D3D11::~Texture_D3D11()
13+
{
14+
CUDA_CHECK(cudaGetLastError());
15+
};
16+
17+
/// <summary>
18+
/// Has to be call after the first issue plugin event
19+
/// see. https://docs.unity3d.com/ScriptReference/GL.IssuePluginEvent.html
20+
/// register a graphics resources defined from the texture openGL
21+
/// </summary>
22+
void Texture_D3D11::registerTextureInCUDA()
23+
{
24+
25+
Log::log().debugLog("try register");
26+
ID3D11Texture2D *d3dtex = (ID3D11Texture2D *)_textureHandle;
27+
assert(d3dtex);
28+
// ID3D11DeviceContext* ctx = NULL;
29+
// m_Device->GetImmediateContext(&ctx);
30+
// ID3D11Resource* tex = (ID3D11Resource*)(_textureHandle);
31+
// HRESULT result = tex->GetDevice()->GetDeviceRemovedReason();
32+
// register the texture to cuda : it initialize the _pGraphicsResource
33+
CUDA_CHECK(cudaGraphicsD3D11RegisterResource(
34+
&_pGraphicsResource, d3dtex, cudaGraphicsRegisterFlagsWriteDiscard));
35+
Log::log().debugLog("register");
36+
}
37+
38+
void Texture_D3D11::unRegisterTextureInCUDA()
39+
{
40+
CUDA_CHECK(cudaGraphicsUnregisterResource(_pGraphicsResource));
41+
}
42+
43+
#endif // #if SUPPORT_D3D11
Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,68 @@
1-
#include "vertex_buffer_OpenGLCoreES.h"
21
#include "texture_OpenGLCoreES.h"
2+
#include "vertex_buffer_OpenGLCoreES.h"
3+
#include "texture_D3D11.h"
4+
#include "vertex_buffer_D3D11.h"
35

46
namespace Factory
57
{
6-
VertexBuffer* createBuffer(void* bufferHandle, int size, UnityGfxRenderer apiType)
7-
{
8-
VertexBuffer* buffer = NULL;
9-
// if SUPPORT_OPENGL_UNIFIED
10-
#if SUPPORT_OPENGL_UNIFIED
11-
if (apiType == kUnityGfxRendererOpenGLCore || apiType == kUnityGfxRendererOpenGLES20 || apiType == kUnityGfxRendererOpenGLES30)
12-
{
13-
buffer = new VertexBuffer_OpenGLCoreES(bufferHandle, size);
14-
}
15-
#endif
8+
VertexBuffer *createBuffer(void *bufferHandle, int size,
9+
UnityGfxRenderer apiType)
10+
{
11+
VertexBuffer *buffer = NULL;
1612

17-
// Unknown or unsupported graphics API
18-
return buffer;
19-
}
13+
#if SUPPORT_D3D11
14+
if (apiType == kUnityGfxRendererD3D11)
15+
{
16+
buffer = new VertexBuffer_D3D11(bufferHandle, size);
17+
}
18+
#endif // if SUPPORT_D3D11
19+
// if SUPPORT_OPENGL_UNIFIED
20+
#if SUPPORT_OPENGL_UNIFIED
21+
if (apiType == kUnityGfxRendererOpenGLCore ||
22+
apiType == kUnityGfxRendererOpenGLES20 ||
23+
apiType == kUnityGfxRendererOpenGLES30)
24+
{
25+
buffer = new VertexBuffer_OpenGLCoreES(bufferHandle, size);
26+
}
27+
#endif
2028

29+
// Unknown or unsupported graphics API
30+
return buffer;
31+
}
2132

22-
Texture* createTexture(void* textureHandle, int textureWidth, int textureHeight, int textureDepth, UnityGfxRenderer apiType)
23-
{
24-
Texture* texture = NULL;
25-
//# if SUPPORT_D3D11
26-
// if (apiType == kUnityGfxRendererD3D11)
27-
// {
28-
// extern Texture* CreateRenderAPI_D3D11();
29-
// return CreateRenderAPI_D3D11();
30-
// }
31-
//# endif // if SUPPORT_D3D11
32-
//
33-
//# if SUPPORT_D3D12
34-
// if (apiType == kUnityGfxRendererD3D12)
35-
// {
36-
// extern RenderAPI* CreateRenderAPI_D3D12();
37-
// return CreateRenderAPI_D3D12();
38-
// }
39-
//# endif // if SUPPORT_D3D12
33+
Texture *createTexture(void *textureHandle, int textureWidth, int textureHeight,
34+
int textureDepth, UnityGfxRenderer apiType)
35+
{
36+
Texture *texture = NULL;
37+
#if SUPPORT_D3D11
38+
if (apiType == kUnityGfxRendererD3D11)
39+
{
40+
texture = new Texture_D3D11(textureHandle, textureWidth,
41+
textureHeight, textureDepth);
42+
}
43+
#endif // if SUPPORT_D3D11
44+
//
45+
// # if SUPPORT_D3D12
46+
// if (apiType == kUnityGfxRendererD3D12)
47+
// {
48+
// extern RenderAPI* CreateRenderAPI_D3D12();
49+
// return CreateRenderAPI_D3D12();
50+
// }
51+
// # endif // if SUPPORT_D3D12
4052

41-
// if SUPPORT_OPENGL_UNIFIED
53+
// if SUPPORT_OPENGL_UNIFIED
4254
#if SUPPORT_OPENGL_UNIFIED
43-
if (apiType == kUnityGfxRendererOpenGLCore || apiType == kUnityGfxRendererOpenGLES20 || apiType == kUnityGfxRendererOpenGLES30)
44-
{
45-
texture = new Texture_OpenGLCoreES(textureHandle, textureWidth, textureHeight, textureDepth);
46-
}
47-
#endif
48-
49-
// will be NULL is unknown or unsupported graphics API
50-
return texture;
51-
}
55+
if (apiType == kUnityGfxRendererOpenGLCore ||
56+
apiType == kUnityGfxRendererOpenGLES20 ||
57+
apiType == kUnityGfxRendererOpenGLES30)
58+
{
59+
texture = new Texture_OpenGLCoreES(textureHandle, textureWidth,
60+
textureHeight, textureDepth);
61+
}
62+
#endif
5263

64+
// will be NULL is unknown or unsupported graphics API
65+
return texture;
66+
}
5367

54-
}
68+
} // namespace Factory

0 commit comments

Comments
 (0)