-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathcube-d3d11.c
171 lines (155 loc) · 5.68 KB
/
cube-d3d11.c
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
//------------------------------------------------------------------------------
// cube-d3d11.c
// Demonstrate uniform block updates.
//------------------------------------------------------------------------------
#include "d3d11entry.h"
#define SOKOL_IMPL
#define SOKOL_D3D11
#include "sokol_gfx.h"
#include "sokol_log.h"
#define HANDMADE_MATH_IMPLEMENTATION
#define HANDMADE_MATH_NO_SSE
#include "HandmadeMath.h"
// a uniform block with a model-view-projection matrix
typedef struct {
hmm_mat4 mvp;
} vs_params_t;
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
(void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
// setup d3d11 app wrapper and sokol_gfx
const int width = 800;
const int height = 600;
d3d11_init(&(d3d11_desc_t){ .width = width, .height = height, .sample_count = 4, .title = L"Sokol Cube D3D11" });
sg_setup(&(sg_desc){
.environment = d3d11_environment(),
.logger.func = slog_func,
});
// cube vertex buffer
float vertices[] = {
// positions colors
-1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
-1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
-1.0, -1.0, 1.0, 0.0, 1.0, 0.0, 1.0,
1.0, -1.0, 1.0, 0.0, 1.0, 0.0, 1.0,
1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0,
-1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0,
-1.0, -1.0, -1.0, 0.0, 0.0, 1.0, 1.0,
-1.0, 1.0, -1.0, 0.0, 0.0, 1.0, 1.0,
-1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0,
-1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0,
1.0, -1.0, -1.0, 1.0, 0.5, 0.0, 1.0,
1.0, 1.0, -1.0, 1.0, 0.5, 0.0, 1.0,
1.0, 1.0, 1.0, 1.0, 0.5, 0.0, 1.0,
1.0, -1.0, 1.0, 1.0, 0.5, 0.0, 1.0,
-1.0, -1.0, -1.0, 0.0, 0.5, 1.0, 1.0,
-1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 1.0,
1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 1.0,
1.0, -1.0, -1.0, 0.0, 0.5, 1.0, 1.0,
-1.0, 1.0, -1.0, 1.0, 0.0, 0.5, 1.0,
-1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 1.0,
1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 1.0,
1.0, 1.0, -1.0, 1.0, 0.0, 0.5, 1.0
};
sg_buffer vbuf = sg_make_buffer(&(sg_buffer_desc){
.data = SG_RANGE(vertices)
});
// cube indices
uint16_t indices[] = {
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
};
sg_buffer ibuf = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_INDEXBUFFER,
.data = SG_RANGE(indices)
});
// define the resource bindings
sg_bindings bind = {
.vertex_buffers[0] = vbuf,
.index_buffer = ibuf
};
// create shader
sg_shader shd = sg_make_shader(&(sg_shader_desc){
.vertex_func.source =
"cbuffer params: register(b0) {\n"
" float4x4 mvp;\n"
"};\n"
"struct vs_in {\n"
" float4 pos: POSITION;\n"
" float4 color: COLOR1;\n"
"};\n"
"struct vs_out {\n"
" float4 color: COLOR0;\n"
" float4 pos: SV_Position;\n"
"};\n"
"vs_out main(vs_in inp) {\n"
" vs_out outp;\n"
" outp.pos = mul(mvp, inp.pos);\n"
" outp.color = inp.color;\n"
" return outp;\n"
"};\n",
.fragment_func.source =
"float4 main(float4 color: COLOR0): SV_Target0 {\n"
" return color;\n"
"}\n",
.attrs = {
[0] = { .hlsl_sem_name = "POSITION" },
[1] = { .hlsl_sem_name = "COLOR", .hlsl_sem_index = 1 },
},
.uniform_blocks[0] = {
.stage = SG_SHADERSTAGE_VERTEX,
.size = sizeof(vs_params_t),
.hlsl_register_b_n = 0,
},
});
// a pipeline object
sg_pipeline pip = sg_make_pipeline(&(sg_pipeline_desc){
// test to provide buffer stride, but no attribute offsets
.layout = {
.buffers[0].stride = 28,
.attrs = {
[0].format = SG_VERTEXFORMAT_FLOAT3,
[1].format = SG_VERTEXFORMAT_FLOAT4
}
},
.shader = shd,
.index_type = SG_INDEXTYPE_UINT16,
.depth = {
.compare = SG_COMPAREFUNC_LESS_EQUAL,
.write_enabled = true
},
.cull_mode = SG_CULLMODE_BACK,
});
// default pass action (clear to gray)
sg_pass_action pass_action = { 0 };
// view-projection matrix
hmm_mat4 proj = HMM_Perspective(60.0f, (float)width/(float)height, 0.01f, 10.0f);
hmm_mat4 view = HMM_LookAt(HMM_Vec3(0.0f, 1.5f, 6.0f), HMM_Vec3(0.0f, 0.0f, 0.0f), HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 view_proj = HMM_MultiplyMat4(proj, view);
float rx = 0.0f, ry = 0.0f;
while (d3d11_process_events()) {
// model-view-proj matrix for vertex shader
vs_params_t vs_params;
rx += 1.0f; ry += 2.0f;
hmm_mat4 rxm = HMM_Rotate(rx, HMM_Vec3(1.0f, 0.0f, 0.0f));
hmm_mat4 rym = HMM_Rotate(ry, HMM_Vec3(0.0f, 1.0f, 0.0f));
hmm_mat4 model = HMM_MultiplyMat4(rxm, rym);
vs_params.mvp = HMM_MultiplyMat4(view_proj, model);
sg_begin_pass(&(sg_pass){ .action = pass_action, .swapchain = d3d11_swapchain() });
sg_apply_pipeline(pip);
sg_apply_bindings(&bind);
sg_apply_uniforms(0, &SG_RANGE(vs_params));
sg_draw(0, 36, 1);
sg_end_pass();
sg_commit();
d3d11_present();
}
sg_shutdown();
d3d11_shutdown();
return 0;
}