1 /**************************************************************************
3 * Copyright 2012 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
41 static IDXGISwapChain* g_pSwapChain = NULL;
42 static ID3D11Device * g_pDevice = NULL;
43 static ID3D11DeviceContext * g_pDeviceContext = NULL;
44 static ID3D11Device1 * g_pDevice1 = NULL;
45 static ID3D11DeviceContext1 * g_pDeviceContext1 = NULL;
48 int main(int argc, char *argv[]){
51 HINSTANCE hInstance = GetModuleHandle(NULL);
69 const int WindowWidth = 250;
70 const int WindowHeight = 250;
73 DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
75 RECT rect = {0, 0, WindowWidth, WindowHeight};
76 AdjustWindowRect(&rect, dwStyle, FALSE);
78 HWND hWnd = CreateWindow(wc.lpszClassName,
79 "Simple example using DirectX10",
81 CW_USEDEFAULT, CW_USEDEFAULT,
82 rect.right - rect.left,
83 rect.bottom - rect.top,
92 ShowWindow(hWnd, SW_SHOW);
94 DXGI_SWAP_CHAIN_DESC SwapChainDesc;
95 ZeroMemory(&SwapChainDesc, sizeof SwapChainDesc);
96 SwapChainDesc.BufferDesc.Width = WindowWidth;
97 SwapChainDesc.BufferDesc.Height = WindowHeight;
98 SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;
99 SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
100 SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
101 SwapChainDesc.SampleDesc.Quality = 0;
102 SwapChainDesc.SampleDesc.Count = 1;
103 SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
104 SwapChainDesc.BufferCount = 2;
105 SwapChainDesc.OutputWindow = hWnd;
106 SwapChainDesc.Windowed = true;
108 D3D_FEATURE_LEVEL FeatureLevel = D3D_FEATURE_LEVEL_11_1;
110 hr = D3D11CreateDeviceAndSwapChain(NULL, /* pAdapter */
111 D3D_DRIVER_TYPE_HARDWARE,
113 D3D11_CREATE_DEVICE_DEBUG,
125 NULL, /* pFeatureLevel */
126 &g_pDeviceContext); /* ppImmediateContext */
131 hr = g_pDevice->QueryInterface(IID_ID3D11Device1, (void **)&g_pDevice1);
136 hr = g_pDeviceContext->QueryInterface(IID_ID3D11DeviceContext1, (void **)&g_pDeviceContext1);
141 ID3D11RenderTargetView *pRenderTargetView = NULL;
142 ID3D11Texture2D* pBackBuffer;
143 hr = g_pSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void **)&pBackBuffer);
147 hr = g_pDevice1->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
151 pBackBuffer->Release();
153 g_pDeviceContext1->OMSetRenderTargets(1, &pRenderTargetView, NULL);
155 const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
156 g_pDeviceContext1->ClearRenderTargetView(pRenderTargetView, clearColor);
158 ID3D11VertexShader * pVertexShader;
159 hr = g_pDevice1->CreateVertexShader(g_VS, sizeof g_VS, NULL, &pVertexShader);
169 static const D3D11_INPUT_ELEMENT_DESC InputElementDescs[] = {
170 { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, position), D3D11_INPUT_PER_VERTEX_DATA, 0 },
171 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, color), D3D11_INPUT_PER_VERTEX_DATA, 0 }
174 ID3D11InputLayout *pVertexLayout = NULL;
175 hr = g_pDevice1->CreateInputLayout(InputElementDescs,
184 g_pDeviceContext1->IASetInputLayout(pVertexLayout);
186 ID3D11PixelShader * pPixelShader;
187 hr = g_pDevice1->CreatePixelShader(g_PS, sizeof g_PS, NULL, &pPixelShader);
192 g_pDeviceContext1->VSSetShader(pVertexShader, NULL, 0);
193 g_pDeviceContext1->PSSetShader(pPixelShader, NULL, 0);
195 static const Vertex vertices[] = {
196 { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
197 { { 0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
198 { { 0.0f, 0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
201 D3D11_BUFFER_DESC BufferDesc;
202 ZeroMemory(&BufferDesc, sizeof BufferDesc);
203 BufferDesc.Usage = D3D11_USAGE_DYNAMIC;
204 BufferDesc.ByteWidth = sizeof vertices;
205 BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
206 BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
207 BufferDesc.MiscFlags = 0;
209 D3D11_SUBRESOURCE_DATA BufferData;
210 BufferData.pSysMem = vertices;
211 BufferData.SysMemPitch = 0;
212 BufferData.SysMemSlicePitch = 0;
214 ID3D11Buffer *pVertexBuffer;
215 hr = g_pDevice1->CreateBuffer(&BufferDesc, &BufferData, &pVertexBuffer);
220 UINT Stride = sizeof(Vertex);
222 g_pDeviceContext1->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
224 D3D11_VIEWPORT ViewPort;
225 ViewPort.TopLeftX = 0;
226 ViewPort.TopLeftY = 0;
227 ViewPort.Width = WindowWidth;
228 ViewPort.Height = WindowHeight;
229 ViewPort.MinDepth = 0.0f;
230 ViewPort.MaxDepth = 1.0f;
231 g_pDeviceContext1->RSSetViewports(1, &ViewPort);
233 D3D11_RASTERIZER_DESC RasterizerDesc;
234 ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
235 RasterizerDesc.CullMode = D3D11_CULL_NONE;
236 RasterizerDesc.FillMode = D3D11_FILL_SOLID;
237 RasterizerDesc.FrontCounterClockwise = true;
238 ID3D11RasterizerState* pRasterizerState = NULL;
239 g_pDevice1->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
240 g_pDeviceContext1->RSSetState(pRasterizerState);
242 g_pDeviceContext1->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
243 g_pDeviceContext1->Draw(3, 0);
245 g_pSwapChain->Present(0, 0);
248 ID3D11Buffer *pNullBuffer = NULL;
251 g_pDeviceContext1->IASetVertexBuffers(0, 1, &pNullBuffer, &NullStride, &NullOffset);
252 pVertexBuffer->Release();
254 g_pDeviceContext1->OMSetRenderTargets(0, NULL, NULL);
255 pRenderTargetView->Release();
257 g_pDeviceContext1->IASetInputLayout(NULL);
258 pVertexLayout->Release();
260 g_pDeviceContext1->VSSetShader(NULL, NULL, 0);
261 pVertexShader->Release();
263 g_pDeviceContext1->PSSetShader(NULL, NULL, 0);
264 pPixelShader->Release();
266 g_pDeviceContext1->RSSetState(NULL);
267 pRasterizerState->Release();
269 g_pSwapChain->Release();
272 g_pDeviceContext1->Release();
273 g_pDeviceContext1 = NULL;
275 g_pDevice1->Release();
278 g_pDeviceContext->Release();
279 g_pDeviceContext = NULL;
281 g_pDevice->Release();