33 HMODULE GHOST_ContextD3D::s_d3d_lib =
NULL;
34 PFN_D3D11_CREATE_DEVICE GHOST_ContextD3D::s_D3D11CreateDeviceFn =
NULL;
44 m_device_ctx->ClearState();
45 m_device_ctx->Release();
65 if (s_d3d_lib ==
NULL) {
66 s_d3d_lib = LoadLibraryA(
"d3d11.dll");
68 WIN32_CHK(s_d3d_lib !=
NULL);
70 if (s_d3d_lib ==
NULL) {
71 fprintf(stderr,
"LoadLibrary(\"d3d11.dll\") failed!\n");
76 if (s_D3D11CreateDeviceFn ==
NULL) {
77 s_D3D11CreateDeviceFn = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(s_d3d_lib,
80 WIN32_CHK(s_D3D11CreateDeviceFn !=
NULL);
82 if (s_D3D11CreateDeviceFn ==
NULL) {
83 fprintf(stderr,
"GetProcAddress(s_d3d_lib, \"D3D11CreateDevice\") failed!\n");
97 HRESULT hres = s_D3D11CreateDeviceFn(
99 D3D_DRIVER_TYPE_HARDWARE,
113 WIN32_CHK(hres == S_OK);
127 HANDLE render_buf{
nullptr};
132 ID3D11DeviceContext *device_ctx,
135 ID3D11RenderTargetView *render_target =
nullptr)
136 : m_device(device), m_device_ctx(device_ctx), m_cur_width(
width), m_cur_height(
height)
138 ID3D11Resource *backbuffer_res;
140 if (!render_target) {
141 D3D11_TEXTURE2D_DESC texDesc{};
142 D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc{};
143 ID3D11Texture2D *
tex;
145 texDesc.Width =
width;
148 texDesc.SampleDesc.Count = 1;
149 texDesc.ArraySize = 1;
150 texDesc.MipLevels = 1;
151 texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
153 device->CreateTexture2D(&texDesc,
NULL, &
tex);
157 fprintf(stderr,
"Error creating texture for shared DirectX-OpenGL resource\n");
161 renderTargetViewDesc.Format = texDesc.Format;
162 renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
163 renderTargetViewDesc.Texture2D.MipSlice = 0;
165 device->CreateRenderTargetView(
tex, &renderTargetViewDesc, &render_target);
174 if (backbuffer_res) {
176 backbuffer_res->Release();
180 fprintf(stderr,
"Error creating render target for shared DirectX-OpenGL resource\n");
194 if (m_is_initialized) {
195 if (m_shared.render_buf) {
196 wglDXUnregisterObjectNV(m_shared.device, m_shared.render_buf);
198 if (m_shared.device) {
199 wglDXCloseDeviceNV(m_shared.device);
201 glDeleteFramebuffers(1, &m_shared.fbo);
202 glDeleteRenderbuffers(1, &m_gl_render_buf);
208 if (m_shared.render_buf) {
209 wglDXUnregisterObjectNV(m_shared.device, m_shared.render_buf);
216 m_shared.render_buf = wglDXRegisterObjectNV(m_shared.device,
220 WGL_ACCESS_READ_WRITE_NV);
222 if (!m_shared.render_buf) {
223 fprintf(stderr,
"Error registering shared object using wglDXRegisterObjectNV()\n");
230 m_shared.device = wglDXOpenDeviceNV(m_device);
231 if (m_shared.device ==
NULL) {
232 fprintf(stderr,
"Error opening shared device using wglDXOpenDeviceNV()\n");
237 glGenRenderbuffers(1, &m_gl_render_buf);
238 glBindRenderbuffer(GL_RENDERBUFFER, m_gl_render_buf);
243 glGenFramebuffers(1, &m_shared.fbo);
244 glBindFramebuffer(GL_FRAMEBUFFER, m_shared.fbo);
245 glFramebufferRenderbuffer(
246 GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, m_gl_render_buf);
247 m_is_initialized =
true;
254 if (m_is_initialized ==
false) {
258 if ((m_cur_width !=
width) || (m_cur_height !=
height)) {
268 glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &fbo);
277 const float clear_col[] = {0.8f, 0.5f, 1.0f, 1.0f};
284 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_shared.fbo);
285 GLenum
err = glCheckFramebufferStatus(GL_FRAMEBUFFER);
286 if (
err != GL_FRAMEBUFFER_COMPLETE) {
288 stderr,
"Error: Framebuffer for shared DirectX-OpenGL resource incomplete %u\n",
err);
293 glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
296 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
309 wglDXLockObjectsNV(m_shared.device, 1, &m_shared.render_buf);
313 wglDXUnlockObjectsNV(m_shared.device, 1, &m_shared.render_buf);
316 ID3D11Device *m_device;
317 ID3D11DeviceContext *m_device_ctx;
318 GLuint m_gl_render_buf;
319 unsigned int m_cur_width, m_cur_height;
320 bool m_is_initialized{
false};
324 unsigned int width,
unsigned int height, ID3D11RenderTargetView *render_target)
326 if (!(WGL_NV_DX_interop && WGL_NV_DX_interop2)) {
328 "Error: Can't render OpenGL framebuffer using Direct3D. NV_DX_interop extension not "
333 m_device, m_device_ctx,
width,
height, render_target);
@ DXGI_FORMAT_R8G8B8A8_UNORM
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei height
GHOST_TSuccess activateDrawingContext()
GHOST_TSuccess releaseNativeHandles()
class GHOST_SharedOpenGLResource * createSharedOpenGLResource(unsigned int width, unsigned int height, ID3D11RenderTargetView *render_target)
GHOST_TSuccess swapBuffers()
GHOST_ContextD3D(bool stereoVisual, HWND hWnd)
GHOST_TSuccess blitFromOpenGLContext(class GHOST_SharedOpenGLResource *shared_res, unsigned int width, unsigned int height)
ID3D11Texture2D * getSharedTexture2D(class GHOST_SharedOpenGLResource *shared_res)
GHOST_TSuccess initializeDrawingContext()
void disposeSharedOpenGLResource(class GHOST_SharedOpenGLResource *shared_res)
GHOST_TSuccess releaseDrawingContext()
void ensureUpdated(unsigned int width, unsigned int height)
void reregisterSharedObject()
GHOST_TSuccess blit(unsigned int width, unsigned int height)
GHOST_TSuccess initialize()
GHOST_SharedOpenGLResource(ID3D11Device *device, ID3D11DeviceContext *device_ctx, unsigned int width, unsigned int height, ID3D11RenderTargetView *render_target=nullptr)
~GHOST_SharedOpenGLResource()
ID3D11RenderTargetView * m_render_target
ID3D11Texture2D * m_render_target_tex