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 IDXGIFactory1 *g_pFactory = NULL;
42 static IDXGIAdapter *g_pAdapter = NULL;
43 static IDXGISwapChain* g_pSwapChain = NULL;
44 static ID3D10Device1 * g_pDevice = NULL;
48 main(int argc, char *argv[])
52 HINSTANCE hInstance = GetModuleHandle(NULL);
70 const int WindowWidth = 250;
71 const int WindowHeight = 250;
74 DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
76 RECT rect = {0, 0, WindowWidth, WindowHeight};
77 AdjustWindowRect(&rect, dwStyle, FALSE);
79 HWND hWnd = CreateWindow(wc.lpszClassName,
80 "Simple example using DirectX10",
82 CW_USEDEFAULT, CW_USEDEFAULT,
83 rect.right - rect.left,
84 rect.bottom - rect.top,
93 ShowWindow(hWnd, SW_SHOW);
96 if (LoadLibraryA("d3d10sdklayers")) {
97 Flags |= D3D10_CREATE_DEVICE_DEBUG;
100 hr = CreateDXGIFactory1(IID_IDXGIFactory1, (void**)(&g_pFactory) );
105 hr = g_pFactory->EnumAdapters(0, &g_pAdapter);
110 hr = D3D10CreateDevice1(g_pAdapter,
111 D3D10_DRIVER_TYPE_HARDWARE,
114 D3D10_FEATURE_LEVEL_10_0,
121 DXGI_SWAP_CHAIN_DESC SwapChainDesc;
122 ZeroMemory(&SwapChainDesc, sizeof SwapChainDesc);
123 SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;;
124 SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
125 SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
126 SwapChainDesc.SampleDesc.Quality = 0;
127 SwapChainDesc.SampleDesc.Count = 1;
128 SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
129 SwapChainDesc.BufferCount = 2;
130 SwapChainDesc.OutputWindow = hWnd;
131 SwapChainDesc.Windowed = true;
132 SwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
134 hr = g_pFactory->CreateSwapChain(g_pDevice, &SwapChainDesc, &g_pSwapChain);
139 ID3D10RenderTargetView *pRenderTargetView = NULL;
140 ID3D10Texture2D* pBackBuffer;
141 hr = g_pSwapChain->GetBuffer(0, IID_ID3D10Texture2D, (void **)&pBackBuffer);
145 D3D10_RENDER_TARGET_VIEW_DESC RenderTargetViewDesc;
146 ZeroMemory(&RenderTargetViewDesc, sizeof RenderTargetViewDesc);
147 RenderTargetViewDesc.Format = SwapChainDesc.BufferDesc.Format;
148 RenderTargetViewDesc.ViewDimension = D3D10_RTV_DIMENSION_TEXTURE2D;
149 RenderTargetViewDesc.Texture2D.MipSlice = 0;
150 hr = g_pDevice->CreateRenderTargetView(pBackBuffer, &RenderTargetViewDesc, &pRenderTargetView);
154 pBackBuffer->Release();
156 g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
158 const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
159 g_pDevice->ClearRenderTargetView(pRenderTargetView, clearColor);
161 ID3D10VertexShader * pVertexShader;
162 hr = g_pDevice->CreateVertexShader(g_VS, sizeof g_VS, &pVertexShader);
172 static const D3D10_INPUT_ELEMENT_DESC InputElementDescs[] = {
173 { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, position), D3D10_INPUT_PER_VERTEX_DATA, 0 },
174 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, color), D3D10_INPUT_PER_VERTEX_DATA, 0 }
177 ID3D10InputLayout *pVertexLayout = NULL;
178 hr = g_pDevice->CreateInputLayout(InputElementDescs,
187 g_pDevice->IASetInputLayout(pVertexLayout);
189 ID3D10PixelShader * pPixelShader;
190 hr = g_pDevice->CreatePixelShader(g_PS, sizeof g_PS, &pPixelShader);
195 g_pDevice->VSSetShader(pVertexShader);
196 g_pDevice->PSSetShader(pPixelShader);
198 static const Vertex vertices[] = {
199 { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
200 { { 0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
201 { { 0.0f, 0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
204 D3D10_BUFFER_DESC BufferDesc;
205 ZeroMemory(&BufferDesc, sizeof BufferDesc);
206 BufferDesc.Usage = D3D10_USAGE_DYNAMIC;
207 BufferDesc.ByteWidth = sizeof vertices;
208 BufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
209 BufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
210 BufferDesc.MiscFlags = 0;
212 ID3D10Buffer *pVertexBuffer;
213 hr = g_pDevice->CreateBuffer(&BufferDesc, NULL, &pVertexBuffer);
219 pVertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pMap);
220 memcpy(pMap, vertices, sizeof vertices);
221 pVertexBuffer->Unmap();
223 UINT Stride = sizeof(Vertex);
225 g_pDevice->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
227 D3D10_VIEWPORT ViewPort;
228 ViewPort.TopLeftX = 0;
229 ViewPort.TopLeftY = 0;
230 ViewPort.Width = WindowWidth;
231 ViewPort.Height = WindowHeight;
232 ViewPort.MinDepth = 0.0f;
233 ViewPort.MaxDepth = 1.0f;
234 g_pDevice->RSSetViewports(1, &ViewPort);
236 D3D10_RASTERIZER_DESC RasterizerDesc;
237 ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
238 RasterizerDesc.CullMode = D3D10_CULL_NONE;
239 RasterizerDesc.FillMode = D3D10_FILL_SOLID;
240 RasterizerDesc.FrontCounterClockwise = true;
241 RasterizerDesc.DepthClipEnable = true;
242 ID3D10RasterizerState* pRasterizerState = NULL;
243 hr = g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
247 g_pDevice->RSSetState(pRasterizerState);
249 g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
250 g_pDevice->Draw(3, 0);
252 g_pSwapChain->Present(0, 0);
255 ID3D10Buffer *pNullBuffer = NULL;
258 g_pDevice->IASetVertexBuffers(0, 1, &pNullBuffer, &NullStride, &NullOffset);
259 pVertexBuffer->Release();
261 g_pDevice->OMSetRenderTargets(0, NULL, NULL);
262 pRenderTargetView->Release();
264 g_pDevice->IASetInputLayout(NULL);
265 pVertexLayout->Release();
267 g_pDevice->VSSetShader(NULL);
268 pVertexShader->Release();
270 g_pDevice->PSSetShader(NULL);
271 pPixelShader->Release();
273 g_pDevice->RSSetState(NULL);
274 pRasterizerState->Release();
276 g_pSwapChain->Release();
279 g_pDevice->Release();