-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainglfwdx11.cpp
212 lines (168 loc) · 5.98 KB
/
mainglfwdx11.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "GLFW/glfw3.h"
#define GLFW_EXPOSE_NATIVE_WIN32
#include "GLFW/glfw3native.h"
#include <d3d11.h>
#include <d3dcompiler.h>
#pragma comment(lib, "d3d11")
#pragma comment(lib, "d3dcompiler")
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
IDXGISwapChain* g_swapchain;
ID3D11Device* g_dev;
ID3D11DeviceContext* g_devcon;
ID3D11RenderTargetView* g_backbuffer;
ID3D11VertexShader* g_VS;
ID3D11PixelShader* g_PS;
ID3D11InputLayout* g_layout;
ID3D11Buffer* g_vertexbuffer;
struct VERTEX
{
float X, Y, Z;
float R, G, B, A;
};
void InitD3D(HWND hWnd);
void RenderFrame();
void CleanD3D();
void InitPipeline();
void InitGraphics();
static const char* g_shader = R"(
struct VSOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VSOut VSmain(float4 position : POSITION, float4 color : COLOR)
{
VSOut output;
output.position = position;
output.color = color;
return output;
}
float4 PSmain(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
return color;
}
)";
int main()
{
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "GLFW + DX11", nullptr, nullptr);
InitD3D(glfwGetWin32Window(window));
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
RenderFrame();
}
CleanD3D();
glfwTerminate();
return EXIT_SUCCESS;
}
void InitD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd;
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
scd.BufferCount = 1; // one back buffer
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
scd.BufferDesc.Width = SCREEN_WIDTH; // set the back buffer width
scd.BufferDesc.Height = SCREEN_HEIGHT; // set the back buffer height
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
scd.OutputWindow = hWnd; // the window to be used
scd.SampleDesc.Count = 4; // how many multisamples
scd.Windowed = TRUE; // windowed/full-screen mode
scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow full-screen switching
D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&g_swapchain,
&g_dev,
NULL,
&g_devcon);
ID3D11Texture2D* pBackBuffer;
g_swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
g_dev->CreateRenderTargetView(pBackBuffer, NULL, &g_backbuffer);
g_devcon->OMSetRenderTargets(1, &g_backbuffer, NULL);
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = SCREEN_WIDTH;
viewport.Height = SCREEN_HEIGHT;
g_devcon->RSSetViewports(1, &viewport);
InitPipeline();
InitGraphics();
}
void CleanD3D()
{
g_swapchain->SetFullscreenState(FALSE, NULL); // switch to windowed mode
g_VS->Release();
g_PS->Release();
g_vertexbuffer->Release();
g_layout->Release();
g_swapchain->Release();
g_dev->Release();
g_devcon->Release();
g_backbuffer->Release();
}
void RenderFrame()
{
float color[4] = {0.0f, 0.2f, 0.4f, 1.0f};
g_devcon->ClearRenderTargetView(g_backbuffer, color);
{
UINT stride = sizeof(VERTEX);
UINT offset = 0;
g_devcon->IASetVertexBuffers(0, 1, &g_vertexbuffer, &stride, &offset);
g_devcon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
g_devcon->Draw(3, 0);
}
g_swapchain->Present(1, 0);
}
void InitPipeline()
{
ID3DBlob *pVSblob, *pPSblob;
ID3DBlob* pErrorBlob;
D3DCompile(g_shader, strlen(g_shader), nullptr, nullptr, nullptr, "VSmain", "vs_5_0", 0, 0, &pVSblob, &pErrorBlob);
D3DCompile(g_shader, strlen(g_shader), nullptr, nullptr, nullptr, "PSmain", "ps_5_0", 0, 0, &pPSblob, &pErrorBlob);
g_dev->CreateVertexShader(pVSblob->GetBufferPointer(), pVSblob->GetBufferSize(), NULL, &g_VS);
g_dev->CreatePixelShader(pPSblob->GetBufferPointer(), pPSblob->GetBufferSize(), NULL, &g_PS);
g_devcon->VSSetShader(g_VS, NULL, 0);
g_devcon->PSSetShader(g_PS, NULL, 0);
D3D11_INPUT_ELEMENT_DESC ied[] =
{
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
};
g_dev->CreateInputLayout(ied, 2, pVSblob->GetBufferPointer(), pVSblob->GetBufferSize(), &g_layout);
g_devcon->IASetInputLayout(g_layout);
pVSblob->Release();
pPSblob->Release();
}
void InitGraphics()
{
VERTEX OurVertices[] =
{
{0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f},
{0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f},
{-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f},
};
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(D3D11_BUFFER_DESC));
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.ByteWidth = sizeof(VERTEX) * 3;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
g_dev->CreateBuffer(&bd, NULL, &g_vertexbuffer);
D3D11_MAPPED_SUBRESOURCE ms;
g_devcon->Map(g_vertexbuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);
memcpy(ms.pData, OurVertices, sizeof(OurVertices));
g_devcon->Unmap(g_vertexbuffer, NULL);
}