-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_ShaderandUniforms.h
219 lines (180 loc) · 9.29 KB
/
08_ShaderandUniforms.h
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
#pragma once // include guard
//# -----------------------------------------------------
// Step 8 - SetupShaderandUniforms
//
//________//________// START Variables and Functions before main function of this step
//________//________// END Variables and Functions before main function of this step
void SetupShaderandUniforms(VkDevice device,
VkPhysicalDevice physicalDevice,
int width,
int height,
VkShaderModule* outVertShaderModule,
VkShaderModule* outFragShaderModule,
VkBuffer* outBuffer,
VkDeviceMemory* outMemory)
{
{
// Simple Shaders (Vertex & Fragment)
uint32_t codeSize = 0;
// assume your file is less than 10,000 bytes
char *code = malloc( sizeof(char[10000]) );
FILE* fileHandle = NULL;
// see Section 6.11 for details on the shaders load your vertex shaders :
// (see Appendix for the shader text readable version)
fileHandle = fopen( "vert.spv", "rb" );
// did you successfully find file
if (fileHandle == NULL) ERR_EXIT("Failed to open shader file." );
// read the file contents
codeSize = fread(code, 1, 10000, fileHandle);
// close the file
fclose(fileHandle);
fileHandle = NULL;
VkShaderModuleCreateInfo vertexShaderCreationInfo = {};
vertexShaderCreationInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
vertexShaderCreationInfo.codeSize = codeSize;
vertexShaderCreationInfo.pCode = (uint32_t *) code;
VkResult result =
vkCreateShaderModule( device,
// device is the logical device that creates the shader module
&vertexShaderCreationInfo,
// pointer the VkShaderModuleCreateInfo structure
NULL,
// optional controling host memory allocation
outVertShaderModule );
// pointer to VkShaderModule handle which the shader module object is returned
ERR_VULKAN_EXIT( result, "Failed to create vertex shader module." );
// load your fragment shader:
fileHandle = fopen( "frag.spv", "rb" );
// did you successfully find file
if (fileHandle == NULL) ERR_EXIT("Failed to open shader frag file." );
// read the file contents
codeSize = fread(code, 1, 10000, fileHandle);
// close the file
fclose(fileHandle);
fileHandle = NULL;
VkShaderModuleCreateInfo fragmentShaderCreationInfo = {};
fragmentShaderCreationInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
fragmentShaderCreationInfo.codeSize = codeSize;
fragmentShaderCreationInfo.pCode = (uint32_t *) code;
result =
vkCreateShaderModule( device,
// device is the logical device that creates the shader module
&fragmentShaderCreationInfo,
// pointer the VkShaderModuleCreateInfo structure
NULL,
// optional controling host memory allocation
outFragShaderModule );
// pointer to VkShaderModule handle which the shader module object is returned
ERR_VULKAN_EXIT( result, "Failed to create vertex shader module." );
//Cleanup (for every "malloc" there must be a "free"
free(code);
code=NULL;
}
{
// Create 'uniform' buffer for passing constant data to the shader (connecting shader with the data)
const float PI = 3.14159265359f;
const float TORAD = PI/180.0f;
// perspective projection parameters:
float fov = 45.0f;
float nearZ = 0.1f;
float farZ = 1000.0f;
float aspectRatio = width / (float) height;
float t = 1.0f / tanf( fov * TORAD * 0.5f );
float nf = nearZ - farZ;
// simple matrices (model/view/projection)
float lprojectionMatrix[16] =
{
t / aspectRatio, 0, 0, 0,
0, t, 0, 0,
0, 0, (-nearZ-farZ) / nf, (2*nearZ*farZ) / nf,
0, 0, 1, 0
};
// identity matrix
float lviewMatrix[16] =
{
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
//position of the object
float xp = 0;
float yp = 2;
float zp = 10;
// matrix/transform
float lmodelMatrix[16] =
{
1, 0, 0, xp,
0, 1, 0, yp,
0, 0, 0, zp,
0, 0, 0, 1
};
// create your uniforms buffers:
VkBufferCreateInfo bufferCreateInfo = {};
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
// size in bytes ( 3 matrices containing 4x4=16 floats)
bufferCreateInfo.size = (float) sizeof( 16 * 3 );
bufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VkResult result =
vkCreateBuffer( device,
// logical device that creates the buffer object
&bufferCreateInfo,
// pointer to returned VkBufferCreateInfo structure containing buffer creation info
NULL,
// optional controling host memory allocation
outBuffer );
// pointer to returned VkBuffer handle object
ERR_VULKAN_EXIT( result, "Failed to create uniforms buffer." );
// allocate memoory for buffer:
VkMemoryRequirements bufferMemoryRequirements = {};
vkGetBufferMemoryRequirements( device,
*outBuffer,
&bufferMemoryRequirements );
VkMemoryAllocateInfo matrixAllocateInfo = {};
matrixAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
matrixAllocateInfo.allocationSize = bufferMemoryRequirements.size;
VkPhysicalDeviceMemoryProperties memoryProperties;
vkGetPhysicalDeviceMemoryProperties( physicalDevice, &memoryProperties );
for ( uint32_t index = 0; index < VK_MAX_MEMORY_TYPES; ++index )
{
VkMemoryType memoryType = memoryProperties.memoryTypes[index];
// is this the memory type you are looking for ?
if ( ( memoryType.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ) )
{
// save location
matrixAllocateInfo.memoryTypeIndex = index;
break;
}
}// END for loop index
result = vkAllocateMemory ( device,
&matrixAllocateInfo,
NULL,
outMemory );
ERR_VULKAN_EXIT( result, "Failed to allocate uniforms buffer memory." );
result = vkBindBufferMemory( device,
*outBuffer,
*outMemory,
0 );
ERR_VULKAN_EXIT( result, "Failed to bind uniforms buffer memory." );
// set buffer content:
void *matrixMapped = NULL;
result = vkMapMemory( device,
// logical device that owns the memory
*outMemory,
// VkDeviceMemory object to be mapped
0,
// zero-based byte offset from the beginning of the memory object
VK_WHOLE_SIZE,
// size of the memory range to map
0,
// reserved for furture use
&matrixMapped );
// pointer in which is returned a host-accessible pointer to the beginning of the mapped range
ERR_VULKAN_EXIT( result, "Failed to map uniform buffer memory." );
memcpy( matrixMapped, &lprojectionMatrix[0], sizeof( lprojectionMatrix ) );
memcpy( ((float *)matrixMapped + 16), &lviewMatrix[0], sizeof( lviewMatrix ) );
memcpy( ((float *)matrixMapped + 32), &lmodelMatrix[0], sizeof( lmodelMatrix ) );
vkUnmapMemory(device, *outMemory );
}
}// END SetupShaderandUniforms(..)