Skip to content

Commit 27a9029

Browse files
committed
Add minimal Direct3D 11.1 renderer (proof-of-concept)
Implements basic D3D11.1 rendering backend with device management, DXGI 1.2 flip model swap chains, and clear/present operations.
1 parent bbd6641 commit 27a9029

File tree

6 files changed

+790
-0
lines changed

6 files changed

+790
-0
lines changed

include/cinder/app/RendererD3d11.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright (c) 2025, The Cinder Project
3+
4+
This code is intended to be used with the Cinder C++ library, http://libcinder.org
5+
6+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
7+
the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this list of conditions and
10+
the following disclaimer.
11+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12+
the following disclaimer in the documentation and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
15+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16+
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
18+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21+
POSSIBILITY OF SUCH DAMAGE.
22+
*/
23+
24+
#pragma once
25+
26+
#include "cinder/app/Renderer.h"
27+
28+
namespace cinder { namespace app {
29+
30+
typedef std::shared_ptr<class RendererD3d11> RendererD3d11Ref;
31+
32+
//! Direct3D 11 renderer for Windows Desktop applications
33+
class RendererD3d11 : public Renderer {
34+
public:
35+
RendererD3d11();
36+
~RendererD3d11();
37+
38+
static RendererD3d11Ref create() { return RendererD3d11Ref( new RendererD3d11() ); }
39+
virtual RendererRef clone() const override { return RendererD3d11Ref( new RendererD3d11( *this ) ); }
40+
41+
//! Initializes the renderer with a WindowImplMsw
42+
virtual void setup( WindowImplMsw *windowImpl, RendererRef sharedRenderer ) override;
43+
44+
//! Returns the HWND associated with this renderer
45+
virtual HWND getHwnd() const override { return mWnd; }
46+
47+
//! Returns the HDC (not used for D3D11, returns nullptr)
48+
virtual HDC getDc() const override { return nullptr; }
49+
50+
//! Cleans up D3D11 resources
51+
virtual void kill() override;
52+
53+
//! Called before toggling fullscreen
54+
virtual void prepareToggleFullScreen() override;
55+
56+
//! Called after toggling fullscreen
57+
virtual void finishToggleFullScreen() override;
58+
59+
//! Called at the start of each frame
60+
virtual void startDraw() override;
61+
62+
//! Called at the end of each frame (presents the back buffer)
63+
virtual void finishDraw() override;
64+
65+
//! Called when the window is resized
66+
virtual void defaultResize() override;
67+
68+
//! Copies the current window surface to a Cinder Surface
69+
virtual Surface8u copyWindowSurface( const Area &area, int32_t windowHeightPixels ) override;
70+
71+
//! Makes this context current (for D3D11, sets render targets)
72+
virtual void makeCurrentContext( bool force = false ) override;
73+
74+
//! Clears the back buffer to the specified color
75+
void clear( const ColorA &color );
76+
77+
//! Returns the D3D11 device (for advanced users)
78+
void* getDevice();
79+
80+
//! Returns the D3D11 device context (for advanced users)
81+
void* getDeviceContext();
82+
83+
protected:
84+
RendererD3d11( const RendererD3d11 &renderer );
85+
86+
class RendererImplD3d11 *mImpl;
87+
HWND mWnd;
88+
};
89+
90+
} } // namespace cinder::app
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright (c) 2025, The Cinder Project
3+
4+
This code is intended to be used with the Cinder C++ library, http://libcinder.org
5+
6+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
7+
the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this list of conditions and
10+
the following disclaimer.
11+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12+
the following disclaimer in the documentation and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
15+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16+
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
18+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21+
POSSIBILITY OF SUCH DAMAGE.
22+
*/
23+
24+
#pragma once
25+
26+
#include "cinder/app/AppBase.h"
27+
#include "cinder/app/msw/RendererImplMsw.h"
28+
#include "cinder/app/RendererD3d11.h"
29+
#include <d3d11_1.h>
30+
#include <dxgi1_2.h>
31+
32+
namespace cinder { namespace app {
33+
34+
//! Forward declaration
35+
class WindowImplMsw;
36+
37+
//! Implementation details for RendererD3d11
38+
class RendererImplD3d11 : public RendererImplMsw {
39+
public:
40+
RendererImplD3d11( WindowImplMsw *windowImpl, RendererD3d11 *renderer );
41+
~RendererImplD3d11();
42+
43+
//! Initializes the D3D11 device, swap chain, and render targets
44+
virtual bool initialize( WindowImplMsw *windowImpl, RendererRef sharedRenderer );
45+
46+
//! Cleans up D3D11 resources
47+
virtual void kill() override;
48+
49+
//! Prepares for fullscreen toggle
50+
virtual void prepareToggleFullScreen() override;
51+
52+
//! Finishes fullscreen toggle
53+
virtual void finishToggleFullScreen() override;
54+
55+
//! Called when window is resized
56+
virtual void defaultResize() const override;
57+
58+
//! Presents the back buffer
59+
virtual void swapBuffers() const override;
60+
61+
//! Sets the render target
62+
virtual void makeCurrentContext( bool force = false ) override;
63+
64+
//! Clears the back buffer to a color
65+
void clear( const ColorA &color );
66+
67+
//! Copies window surface to a Cinder Surface
68+
Surface8u copyWindowSurface( const Area &area );
69+
70+
//! D3D11 device
71+
ID3D11Device1 *mDevice;
72+
73+
//! D3D11 device context
74+
ID3D11DeviceContext1 *mDeviceContext;
75+
76+
//! DXGI swap chain
77+
IDXGISwapChain1 *mSwapChain;
78+
79+
//! Render target view for the back buffer
80+
ID3D11RenderTargetView *mRenderTargetView;
81+
82+
//! Depth stencil texture
83+
ID3D11Texture2D *mDepthStencilTexture;
84+
85+
//! Depth stencil view
86+
ID3D11DepthStencilView *mDepthStencilView;
87+
88+
protected:
89+
RendererD3d11 *mRenderer;
90+
HWND mWnd;
91+
bool mFullScreen;
92+
93+
//! Creates the D3D11 device
94+
bool createDevice();
95+
96+
//! Creates the swap chain
97+
bool createSwapChain();
98+
99+
//! Creates the render target view
100+
bool createRenderTarget();
101+
102+
//! Creates the depth stencil
103+
bool createDepthStencil();
104+
105+
//! Releases framebuffer resources (render target, depth stencil)
106+
void releaseFramebufferResources();
107+
};
108+
109+
} } // namespace cinder::app

proj/vc2019/cinder.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,9 @@
637637
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
638638
</ClCompile>
639639
<ClCompile Include="..\..\src\cinder\app\msw\RendererImplGlMsw.cpp" />
640+
<ClCompile Include="..\..\src\cinder\app\msw\RendererImplD3d11.cpp" />
640641
<ClCompile Include="..\..\src\cinder\app\Platform.cpp" />
642+
<ClCompile Include="..\..\src\cinder\app\RendererD3d11.cpp" />
641643
<ClCompile Include="..\..\src\cinder\app\RendererGl.cpp" />
642644
<ClCompile Include="..\..\src\cinder\app\Window.cpp" />
643645
<ClCompile Include="..\..\src\cinder\app\winrt\AppImplWinRT.cpp">
@@ -1024,12 +1026,14 @@
10241026
<ClInclude Include="..\..\include\cinder\app\msw\AppMsw.h" />
10251027
<ClInclude Include="..\..\include\cinder\app\msw\PlatformMsw.h" />
10261028
<ClInclude Include="..\..\include\cinder\app\msw\RendererImpl2dGdi.h" />
1029+
<ClInclude Include="..\..\include\cinder\app\msw\RendererImplD3d11.h" />
10271030
<ClInclude Include="..\..\include\cinder\app\msw\RendererImplDx.h" />
10281031
<ClInclude Include="..\..\include\cinder\app\msw\RendererImplGlAngle.h" />
10291032
<ClInclude Include="..\..\include\cinder\app\msw\RendererImplGlMsw.h" />
10301033
<ClInclude Include="..\..\include\cinder\app\msw\RendererImplMsw.h" />
10311034
<ClInclude Include="..\..\include\cinder\app\Platform.h" />
10321035
<ClInclude Include="..\..\include\cinder\app\Renderer.h" />
1036+
<ClInclude Include="..\..\include\cinder\app\RendererD3d11.h" />
10331037
<ClInclude Include="..\..\include\cinder\app\RendererDx.h" />
10341038
<ClInclude Include="..\..\include\cinder\app\RendererGl.h" />
10351039
<ClInclude Include="..\..\include\cinder\app\TouchEvent.h" />

proj/vc2019/cinder.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,12 @@
10831083
<ClCompile Include="..\..\src\cinder\CanvasUi.cpp">
10841084
<Filter>Source Files</Filter>
10851085
</ClCompile>
1086+
<ClCompile Include="..\..\src\cinder\app\RendererD3d11.cpp">
1087+
<Filter>Source Files\app</Filter>
1088+
</ClCompile>
1089+
<ClCompile Include="..\..\src\cinder\app\msw\RendererImplD3d11.cpp">
1090+
<Filter>Source Files\app\msw</Filter>
1091+
</ClCompile>
10861092
</ItemGroup>
10871093
<ItemGroup>
10881094
<ClInclude Include="..\..\src\AntTweakBar\AntPerfTimer.h">
@@ -2279,6 +2285,12 @@
22792285
<ClInclude Include="..\..\include\cinder\CanvasUi.h">
22802286
<Filter>Header Files</Filter>
22812287
</ClInclude>
2288+
<ClInclude Include="..\..\include\cinder\app\RendererD3d11.h">
2289+
<Filter>Header Files\app</Filter>
2290+
</ClInclude>
2291+
<ClInclude Include="..\..\include\cinder\app\msw\RendererImplD3d11.h">
2292+
<Filter>Header Files\app\msw</Filter>
2293+
</ClInclude>
22822294
</ItemGroup>
22832295
<ItemGroup>
22842296
<Natvis Include="nlohmann_json.natvis" />

src/cinder/app/RendererD3d11.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
Copyright (c) 2025, The Cinder Project
3+
4+
This code is intended to be used with the Cinder C++ library, http://libcinder.org
5+
6+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
7+
the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this list of conditions and
10+
the following disclaimer.
11+
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12+
the following disclaimer in the documentation and/or other materials provided with the distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
15+
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16+
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
18+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21+
POSSIBILITY OF SUCH DAMAGE.
22+
*/
23+
24+
#include "cinder/app/RendererD3d11.h"
25+
#include "cinder/app/msw/RendererImplD3d11.h"
26+
#include "cinder/app/msw/AppImplMsw.h"
27+
#include "cinder/Exception.h"
28+
29+
namespace cinder { namespace app {
30+
31+
RendererD3d11::RendererD3d11()
32+
: Renderer(), mImpl( nullptr ), mWnd( nullptr )
33+
{
34+
}
35+
36+
RendererD3d11::RendererD3d11( const RendererD3d11 &renderer )
37+
: mImpl( nullptr ), mWnd( renderer.mWnd )
38+
{
39+
}
40+
41+
RendererD3d11::~RendererD3d11()
42+
{
43+
delete mImpl;
44+
}
45+
46+
void RendererD3d11::setup( WindowImplMsw *windowImpl, RendererRef sharedRenderer )
47+
{
48+
mWnd = windowImpl->getHwnd();
49+
50+
if( ! mImpl )
51+
mImpl = new RendererImplD3d11( windowImpl, this );
52+
53+
mImpl->initialize( windowImpl, sharedRenderer );
54+
}
55+
56+
void RendererD3d11::kill()
57+
{
58+
if( mImpl )
59+
mImpl->kill();
60+
}
61+
62+
void RendererD3d11::prepareToggleFullScreen()
63+
{
64+
if( mImpl )
65+
mImpl->prepareToggleFullScreen();
66+
}
67+
68+
void RendererD3d11::finishToggleFullScreen()
69+
{
70+
if( mImpl )
71+
mImpl->finishToggleFullScreen();
72+
}
73+
74+
void RendererD3d11::startDraw()
75+
{
76+
if( mImpl )
77+
mImpl->makeCurrentContext();
78+
}
79+
80+
void RendererD3d11::finishDraw()
81+
{
82+
if( mImpl )
83+
mImpl->swapBuffers();
84+
}
85+
86+
void RendererD3d11::defaultResize()
87+
{
88+
if( mImpl )
89+
mImpl->defaultResize();
90+
}
91+
92+
Surface8u RendererD3d11::copyWindowSurface( const Area &area, int32_t windowHeightPixels )
93+
{
94+
if( mImpl )
95+
return mImpl->copyWindowSurface( area );
96+
97+
return Surface8u();
98+
}
99+
100+
void RendererD3d11::makeCurrentContext( bool force )
101+
{
102+
if( mImpl )
103+
mImpl->makeCurrentContext( force );
104+
}
105+
106+
void RendererD3d11::clear( const ColorA &color )
107+
{
108+
if( mImpl )
109+
mImpl->clear( color );
110+
}
111+
112+
void* RendererD3d11::getDevice()
113+
{
114+
if( mImpl )
115+
return mImpl->mDevice;
116+
return nullptr;
117+
}
118+
119+
void* RendererD3d11::getDeviceContext()
120+
{
121+
if( mImpl )
122+
return mImpl->mDeviceContext;
123+
return nullptr;
124+
}
125+
126+
} } // namespace cinder::app

0 commit comments

Comments
 (0)