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 **************************************************************************/
37 #include "tri_vs_4_0.h"
38 #include "tri_ps_4_0.h"
41 static IDXGISwapChain* g_pSwapChain = NULL;
42 static ID3D11Device * g_pDevice = NULL;
43 static ID3D11DeviceContext * g_pDeviceContext = NULL;
46 int main(int argc, char *argv[]){
49 HINSTANCE hInstance = GetModuleHandle(NULL);
67 const int WindowWidth = 250;
68 const int WindowHeight = 250;
71 DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
73 RECT rect = {0, 0, WindowWidth, WindowHeight};
74 AdjustWindowRect(&rect, dwStyle, FALSE);
76 HWND hWnd = CreateWindow(wc.lpszClassName,
77 "Simple example using DirectX10",
79 CW_USEDEFAULT, CW_USEDEFAULT,
80 rect.right - rect.left,
81 rect.bottom - rect.top,
90 ShowWindow(hWnd, SW_SHOW);
93 if (LoadLibraryA("d3d11sdklayers")) {
94 Flags |= D3D11_CREATE_DEVICE_DEBUG;
97 DXGI_SWAP_CHAIN_DESC SwapChainDesc;
98 ZeroMemory(&SwapChainDesc, sizeof SwapChainDesc);
99 SwapChainDesc.BufferDesc.Width = WindowWidth;
100 SwapChainDesc.BufferDesc.Height = WindowHeight;
101 SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;
102 SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
103 SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
104 SwapChainDesc.SampleDesc.Quality = 0;
105 SwapChainDesc.SampleDesc.Count = 1;
106 SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
107 SwapChainDesc.BufferCount = 2;
108 SwapChainDesc.OutputWindow = hWnd;
109 SwapChainDesc.Windowed = true;
111 static const D3D_FEATURE_LEVEL FeatureLevels[] = {
112 D3D_FEATURE_LEVEL_11_0,
113 D3D_FEATURE_LEVEL_10_1,
114 D3D_FEATURE_LEVEL_10_0
117 hr = D3D11CreateDeviceAndSwapChain(NULL, /* pAdapter */
118 D3D_DRIVER_TYPE_HARDWARE,
122 sizeof FeatureLevels / sizeof FeatureLevels[0],
127 NULL, /* pFeatureLevel */
128 &g_pDeviceContext); /* ppImmediateContext */
133 ID3D11RenderTargetView *pRenderTargetView = NULL;
134 ID3D11Texture2D* pBackBuffer;
135 hr = g_pSwapChain->GetBuffer(0, IID_ID3D11Texture2D, (void **)&pBackBuffer);
139 hr = g_pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
143 pBackBuffer->Release();
145 g_pDeviceContext->OMSetRenderTargets(1, &pRenderTargetView, NULL);
147 const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
148 g_pDeviceContext->ClearRenderTargetView(pRenderTargetView, clearColor);
150 ID3D11VertexShader * pVertexShader;
151 hr = g_pDevice->CreateVertexShader(g_VS, sizeof g_VS, NULL, &pVertexShader);
161 static const D3D11_INPUT_ELEMENT_DESC InputElementDescs[] = {
162 { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, position), D3D11_INPUT_PER_VERTEX_DATA, 0 },
163 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, color), D3D11_INPUT_PER_VERTEX_DATA, 0 }
166 ID3D11InputLayout *pVertexLayout = NULL;
167 hr = g_pDevice->CreateInputLayout(InputElementDescs,
176 g_pDeviceContext->IASetInputLayout(pVertexLayout);
178 ID3D11PixelShader * pPixelShader;
179 hr = g_pDevice->CreatePixelShader(g_PS, sizeof g_PS, NULL, &pPixelShader);
184 g_pDeviceContext->VSSetShader(pVertexShader, NULL, 0);
185 g_pDeviceContext->PSSetShader(pPixelShader, NULL, 0);
187 static const Vertex vertices[] = {
188 { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
189 { { 0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
190 { { 0.0f, 0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
193 D3D11_BUFFER_DESC BufferDesc;
194 ZeroMemory(&BufferDesc, sizeof BufferDesc);
195 BufferDesc.Usage = D3D11_USAGE_DYNAMIC;
196 BufferDesc.ByteWidth = sizeof vertices;
197 BufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
198 BufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
199 BufferDesc.MiscFlags = 0;
201 D3D11_SUBRESOURCE_DATA BufferData;
202 BufferData.pSysMem = vertices;
203 BufferData.SysMemPitch = 0;
204 BufferData.SysMemSlicePitch = 0;
206 ID3D11Buffer *pVertexBuffer;
207 hr = g_pDevice->CreateBuffer(&BufferDesc, &BufferData, &pVertexBuffer);
212 UINT Stride = sizeof(Vertex);
214 g_pDeviceContext->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
216 D3D11_VIEWPORT ViewPort;
217 ViewPort.TopLeftX = 0;
218 ViewPort.TopLeftY = 0;
219 ViewPort.Width = WindowWidth;
220 ViewPort.Height = WindowHeight;
221 ViewPort.MinDepth = 0.0f;
222 ViewPort.MaxDepth = 1.0f;
223 g_pDeviceContext->RSSetViewports(1, &ViewPort);
225 D3D11_RASTERIZER_DESC RasterizerDesc;
226 ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
227 RasterizerDesc.CullMode = D3D11_CULL_NONE;
228 RasterizerDesc.FillMode = D3D11_FILL_SOLID;
229 RasterizerDesc.FrontCounterClockwise = true;
230 RasterizerDesc.DepthClipEnable = true;
231 ID3D11RasterizerState* pRasterizerState = NULL;
232 hr = g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
236 g_pDeviceContext->RSSetState(pRasterizerState);
238 g_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
239 g_pDeviceContext->Draw(3, 0);
241 g_pSwapChain->Present(0, 0);
244 ID3D11Buffer *pNullBuffer = NULL;
247 g_pDeviceContext->IASetVertexBuffers(0, 1, &pNullBuffer, &NullStride, &NullOffset);
248 pVertexBuffer->Release();
250 g_pDeviceContext->OMSetRenderTargets(0, NULL, NULL);
251 pRenderTargetView->Release();
253 g_pDeviceContext->IASetInputLayout(NULL);
254 pVertexLayout->Release();
256 g_pDeviceContext->VSSetShader(NULL, NULL, 0);
257 pVertexShader->Release();
259 g_pDeviceContext->PSSetShader(NULL, NULL, 0);
260 pPixelShader->Release();
262 g_pDeviceContext->RSSetState(NULL);
263 pRasterizerState->Release();
265 g_pSwapChain->Release();
268 g_pDeviceContext->Release();
269 g_pDeviceContext = NULL;
271 g_pDevice->Release();