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_level_9_1.h"
38 #include "tri_ps_4_0_level_9_1.h"
41 static IDXGISwapChain* g_pSwapChain = NULL;
42 static ID3D10Device1 * g_pDevice = NULL;
45 int main(int argc, char *argv[]){
48 HINSTANCE hInstance = GetModuleHandle(NULL);
66 const int WindowWidth = 250;
67 const int WindowHeight = 250;
70 DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
72 RECT rect = {0, 0, WindowWidth, WindowHeight};
73 AdjustWindowRect(&rect, dwStyle, FALSE);
75 HWND hWnd = CreateWindow(wc.lpszClassName,
76 "Simple example using DirectX10",
78 CW_USEDEFAULT, CW_USEDEFAULT,
79 rect.right - rect.left,
80 rect.bottom - rect.top,
89 ShowWindow(hWnd, SW_SHOW);
92 if (LoadLibraryA("d3d10sdklayers")) {
93 Flags |= D3D10_CREATE_DEVICE_DEBUG;
96 DXGI_SWAP_CHAIN_DESC SwapChainDesc;
97 ZeroMemory(&SwapChainDesc, sizeof SwapChainDesc);
98 SwapChainDesc.BufferDesc.Width = WindowWidth;
99 SwapChainDesc.BufferDesc.Height = WindowHeight;
100 SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;;
101 SwapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
102 SwapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
103 SwapChainDesc.SampleDesc.Quality = 0;
104 SwapChainDesc.SampleDesc.Count = 1;
105 SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
106 SwapChainDesc.BufferCount = 2;
107 SwapChainDesc.OutputWindow = hWnd;
108 SwapChainDesc.Windowed = true;
110 hr = D3D10CreateDeviceAndSwapChain1(NULL,
111 D3D10_DRIVER_TYPE_HARDWARE,
114 D3D10_FEATURE_LEVEL_9_1,
123 ID3D10RenderTargetView *pRenderTargetView = NULL;
124 ID3D10Texture2D* pBackBuffer;
125 hr = g_pSwapChain->GetBuffer(0, IID_ID3D10Texture2D, (void **)&pBackBuffer);
129 hr = g_pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
133 pBackBuffer->Release();
135 g_pDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);
137 const float clearColor[4] = { 0.3f, 0.1f, 0.3f, 1.0f };
138 g_pDevice->ClearRenderTargetView(pRenderTargetView, clearColor);
140 ID3D10VertexShader * pVertexShader;
141 hr = g_pDevice->CreateVertexShader(g_VS, sizeof g_VS, &pVertexShader);
151 static const D3D10_INPUT_ELEMENT_DESC InputElementDescs[] = {
152 { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, position), D3D10_INPUT_PER_VERTEX_DATA, 0 },
153 { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(Vertex, color), D3D10_INPUT_PER_VERTEX_DATA, 0 }
156 ID3D10InputLayout *pVertexLayout = NULL;
157 hr = g_pDevice->CreateInputLayout(InputElementDescs,
166 g_pDevice->IASetInputLayout(pVertexLayout);
168 ID3D10PixelShader * pPixelShader;
169 hr = g_pDevice->CreatePixelShader(g_PS, sizeof g_PS, &pPixelShader);
174 g_pDevice->VSSetShader(pVertexShader);
175 g_pDevice->PSSetShader(pPixelShader);
177 static const Vertex vertices[] = {
178 { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
179 { { 0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
180 { { 0.0f, 0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
183 D3D10_BUFFER_DESC BufferDesc;
184 ZeroMemory(&BufferDesc, sizeof BufferDesc);
185 BufferDesc.Usage = D3D10_USAGE_DYNAMIC;
186 BufferDesc.ByteWidth = sizeof vertices;
187 BufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
188 BufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
189 BufferDesc.MiscFlags = 0;
191 ID3D10Buffer *pVertexBuffer;
192 hr = g_pDevice->CreateBuffer(&BufferDesc, NULL, &pVertexBuffer);
198 pVertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &pMap);
199 memcpy(pMap, vertices, sizeof vertices);
200 pVertexBuffer->Unmap();
202 UINT Stride = sizeof(Vertex);
204 g_pDevice->IASetVertexBuffers(0, 1, &pVertexBuffer, &Stride, &Offset);
206 D3D10_VIEWPORT ViewPort;
207 ViewPort.TopLeftX = 0;
208 ViewPort.TopLeftY = 0;
209 ViewPort.Width = WindowWidth;
210 ViewPort.Height = WindowHeight;
211 ViewPort.MinDepth = 0.0f;
212 ViewPort.MaxDepth = 1.0f;
213 g_pDevice->RSSetViewports(1, &ViewPort);
215 D3D10_RASTERIZER_DESC RasterizerDesc;
216 ZeroMemory(&RasterizerDesc, sizeof RasterizerDesc);
217 RasterizerDesc.CullMode = D3D10_CULL_NONE;
218 RasterizerDesc.FillMode = D3D10_FILL_SOLID;
219 RasterizerDesc.FrontCounterClockwise = true;
220 RasterizerDesc.DepthClipEnable = true;
221 ID3D10RasterizerState* pRasterizerState = NULL;
222 hr = g_pDevice->CreateRasterizerState(&RasterizerDesc, &pRasterizerState);
226 g_pDevice->RSSetState(pRasterizerState);
228 g_pDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
229 g_pDevice->Draw(3, 0);
231 g_pSwapChain->Present(0, 0);
234 ID3D10Buffer *pNullBuffer = NULL;
237 g_pDevice->IASetVertexBuffers(0, 1, &pNullBuffer, &NullStride, &NullOffset);
238 pVertexBuffer->Release();
240 g_pDevice->OMSetRenderTargets(0, NULL, NULL);
241 pRenderTargetView->Release();
243 g_pDevice->IASetInputLayout(NULL);
244 pVertexLayout->Release();
246 g_pDevice->VSSetShader(NULL);
247 pVertexShader->Release();
249 g_pDevice->PSSetShader(NULL);
250 pPixelShader->Release();
252 g_pDevice->RSSetState(NULL);
253 pRasterizerState->Release();
255 g_pSwapChain->Release();
258 g_pDevice->Release();