-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawExample.cpp
254 lines (201 loc) · 5.9 KB
/
drawExample.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*!
* @file
* @brief This file contains example structure of drawTriangle function
*
* @author Tomáš Milet, imilet@fit.vutbr.cz
*/
#include <student/fwd.hpp>
///\cond HIDDEN_SYMBOLS
//![gpu_execute_commands]
void clear(GPUMemory&mem,ClearCommand cmd){
if(cmd.clearColor){
float red = cmd.color.r;
mem.framebuffer.color[0] = (uint8_t)red*255.f;
//...
}
}
void gpu_execute(GPUMemory&mem,CommandBuffer const&cb){
for(uint32_t i=0;i<cb.nofCommands;++i){
CommandType type = cb.commands[i].type;
CommandData data = cb.commands[i].data;
if(type == CommandType::CLEAR){clear(mem,data.clearCommand);
}
}
//![gpu_execute_commands]
//![gpu_execute_before_vs]
void runVertexAssembly(){
computeVertexID();
readAttributes();
}
void draw(GPUMemory&mem,DrawCommand cmd){
Program prg = mem.programs[cmd.programID];
for(every vertex v < cmd.nofVertices){
InVertex inVertex;
OutVertex outVertex;
runVertexAssembly(inVertex,cmd.vao,v);
ShaderInterface si;
prg.vertexShader(outVertex,inVertex,si);
}
}
void gpu_execute(GPUMemory&mem,CommandBuffer const&cb){
for(... commands ...){
if (commandType == DRAW )
draw(mem, drawCommand);
}
}
//![gpu_execute_before_vs]
//![gpu_execute_after_vs]
void runVertexAssembly(){
computeVertexID();
readAttributes();
}
void runPrimitiveAssembly(primitive,VertexArray vao,t,Program prg){
for(every vertex v in triangle){
InVertex inVertex;
runVertexAssembly(inVertex,vao,t+v);
prg.vertexShader(primitive.vertex,inVertex,prg.uniforms);
}
}
void rasterizeTriangle(frame,primitive,prg){
for(pixels in frame){
if(pixels in primitive){
InFragment inFragment;
createFragment(inFragment,primitive,barycentrics,pixelCoord,prg);
OutFragment outFragment;
prg.fragmentShader(outFragment,inFragment,uniforms);
}
}
}
void draw(GPUMemory&mem,DrawCommand const&cmd){
for(every triangle t){
Primitive primitive;
runPrimitiveAssembly(primitive,ctx.vao,t,ctx.prg)
runPerspectiveDivision(primitive)
runViewportTransformation(primitive,ctx.frame)
rasterizeTriangle(ctx.frame,primitive,ctx.prg);
}
}
void gpu_execute(...){
}
//![gpu_execute_after_vs]
//![gpu_execute_pfo]
void runVertexAssembly(){
computeVertexID()
readVertexAttributes();
}
void runPrimitiveAssembly(primitive,VertexArray vao,t,vertexShader,shaderInterface){
for(every vertex v in triangle){
InVertex inVertex;
runVertexAssembly(inVertex,vao,t+v);
vertexShader(primitive.vertex,inVertex,shaderInterface);
}
}
void rasterizeTriangle(framebuffer,primitive,fragmentShader){
for(pixels in frame){
if(pixels in primitive){
InFragment inFragment;
createFragment(inFragment,primitive,barycentrics,pixelCoord,prg);
OutFragment outFragment;
ShaderInterface si;
si.uniforms = ...;
si.textures = ...;
fragmentShader(outFragment,inFragment,si);
clampColor(outFragment,0,1);
perFragmentOperations(framebuffer,outFragment,inFragment.gl_FragCoord.z)
}
}
}
void draw(GPUMemory&mem,DrawCommand const&cmd){
for(every triangle t){
Primitive primitive;
runPrimitiveAssembly(primitive,vao,t,vertexShader,shaderInterface)
runPerspectiveDivision(primitive)
runViewportTransformation(primitive,width,height)
rasterizeTriangle(framebuffer,primitive,fragmentShader);
}
}
void gpu_execute(...){
}
//![gpu_execute_pfo]
//![gpu_execute]
void runVertexAssembly(){
computeVertexID()
readVertexAttributes();
}
void runPrimitiveAssembly(primitive,vertexArray,t,vertexShader,shaderInterface){
for(every vertex v in triangle){
InVertex inVertex;
runVertexAssembly(inVertex,vertexArray,t+v);
vertexShader(primitive.vertex,inVertex,shaderInterface);
}
}
void rasterizeTriangle(framebuffer,primitive,fragmentShader){
for(pixel in framebuffer){
if(pixel in primitive){
InFragment inFragment;
createFragment(inFragment,primitive,barycentrics,pixelCoord,vs2fs);
OutFragment outFragment;
fragmentShader(outFragment,inFragment,shaderInterface);
clampColor(outFragment,0,1);
perFragmentOperations(framebuffer,outFragment,inFragment.gl_FragCoord.z)
}
}
}
void draw(mem,drawCommand,gl_DrawID){
for(every triangle t){
Primitive primitive;
runPrimitiveAssembly(primitive,vertexArray,t,vertexShader,gl_DrawID)
ClippedPrimitive clipped;
performeClipping(clipped,primitive);
for(all clipped triangle c in clipped){
runPerspectiveDivision(c)
runViewportTransformation(c,width,height)
rasterizeTriangle(framebuffer,c,fragmentShader);
}
}
}
void clear(mem,clearCommad){
}
void gpu_execute(mem,commandBuffer){
for(every command in commandBuffer){
gl_DrawID = computeDrawID();
if(isClearCommand)clear(mem,clearCommand)
if(isDrawCommand )draw (mem,drawCommand,gl_DrawID)
}
}
//![gpu_execute]
//![prepareModel]
void prepareNode(GPUMemory&mem,CommandBuffer&cb,Node const&node,Model const&model,glm::mat4 const&prubeznaMatice,...){
if(node.mesh>=0){
mesh = model.meshes[node.mesh];
//tvorba kreliciho prikazu
mesh.doubleSided ...;
mesh.position ...;
mesh.normal ...;
mesh.texCoord ...;
mesh.indexBufferID ...;
mesh.indexOffset ...;
mesh.indexType ...
mesh.nofVertices ...
createDrawCall(cb)
//zapis dat do paměti (uniformy pro draw call)
ZKOBINUJ(prubeznaMatice,node.modelMatrix);
inverzni tranponovana...
mesh.diffuseColor
mesh.doubleSided
writeToMemory(mem);
}
for(size_t i=0;i<node.children.size();++i)
prepareNode(mem,node.children[i],model,...); rekurze
}
void prepareModel(GPUMemory&mem,CommandBuffer&cb,Model const&model){
mem.buffers = ...;
mem.textures = ...;
mem.programs = ...;
clearCommand ...;
glm::mat4 jednotkovaMatrice = glm::mat4(1.f);
for(size_t i=0;i<model.roots.size();++i)
prepareNode(mem,cb,model.roots[i],jednotkovaMatrice,...);
}
//![prepareModel]
///\endcond