forked from vkngwrapper/vulkan-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28_mipmapping.diff
253 lines (239 loc) · 10.1 KB
/
28_mipmapping.diff
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
diff --git a/../steps/27_model_loading/main.go b/../steps/28_mipmapping/main.go
index adacdcb..4178fd7 100644
--- a/../steps/27_model_loading/main.go
+++ b/../steps/28_mipmapping/main.go
@@ -145,6 +145,7 @@ type HelloTriangleApplication struct {
uniformBuffers []core1_0.Buffer
uniformBuffersMemory []core1_0.DeviceMemory
+ mipLevels int
textureImage core1_0.Image
textureImageMemory core1_0.DeviceMemory
textureImageView core1_0.ImageView
@@ -793,7 +794,7 @@ func (app *HelloTriangleApplication) createImageViews() error {
var imageViews []core1_0.ImageView
for _, image := range images {
- view, err := app.createImageView(image, app.swapchainImageFormat, core1_0.ImageAspectColor)
+ view, err := app.createImageView(image, app.swapchainImageFormat, core1_0.ImageAspectColor, 1)
if err != nil {
return err
}
@@ -1101,6 +1102,7 @@ func (app *HelloTriangleApplication) createDepthResources() error {
app.depthImage, app.depthImageMemory, err = app.createImage(app.swapchainExtent.Width,
app.swapchainExtent.Height,
+ 1,
depthFormat,
core1_0.ImageTilingOptimal,
core1_0.ImageUsageDepthStencilAttachment,
@@ -1108,7 +1110,7 @@ func (app *HelloTriangleApplication) createDepthResources() error {
if err != nil {
return err
}
- app.depthImageView, err = app.createImageView(app.depthImage, depthFormat, core1_0.ImageAspectDepth)
+ app.depthImageView, err = app.createImageView(app.depthImage, depthFormat, core1_0.ImageAspectDepth, 1)
return err
}
@@ -1151,6 +1153,8 @@ func (app *HelloTriangleApplication) createTextureImage() error {
imageDims := imageBounds.Size()
imageSize := imageDims.X * imageDims.Y * 4
+ app.mipLevels = int(math.Log2(math.Max(float64(imageDims.X), float64(imageDims.Y))))
+
stagingBuffer, stagingMemory, err := app.createBuffer(imageSize, core1_0.BufferUsageTransferSrc, core1_0.MemoryPropertyHostVisible|core1_0.MemoryPropertyHostCoherent)
if err != nil {
return err
@@ -1171,13 +1175,13 @@ func (app *HelloTriangleApplication) createTextureImage() error {
}
//Create final image
- app.textureImage, app.textureImageMemory, err = app.createImage(imageDims.X, imageDims.Y, core1_0.FormatR8G8B8A8SRGB, core1_0.ImageTilingOptimal, core1_0.ImageUsageTransferDst|core1_0.ImageUsageSampled, core1_0.MemoryPropertyDeviceLocal)
+ app.textureImage, app.textureImageMemory, err = app.createImage(imageDims.X, imageDims.Y, app.mipLevels, core1_0.FormatR8G8B8A8SRGB, core1_0.ImageTilingOptimal, core1_0.ImageUsageTransferSrc|core1_0.ImageUsageTransferDst|core1_0.ImageUsageSampled, core1_0.MemoryPropertyDeviceLocal)
if err != nil {
return err
}
// Copy staging to final
- err = app.transitionImageLayout(app.textureImage, core1_0.FormatR8G8B8A8SRGB, core1_0.ImageLayoutUndefined, core1_0.ImageLayoutTransferDstOptimal)
+ err = app.transitionImageLayout(app.textureImage, core1_0.FormatR8G8B8A8SRGB, core1_0.ImageLayoutUndefined, core1_0.ImageLayoutTransferDstOptimal, app.mipLevels)
if err != nil {
return err
}
@@ -1185,7 +1189,8 @@ func (app *HelloTriangleApplication) createTextureImage() error {
if err != nil {
return err
}
- err = app.transitionImageLayout(app.textureImage, core1_0.FormatR8G8B8A8SRGB, core1_0.ImageLayoutTransferDstOptimal, core1_0.ImageLayoutShaderReadOnlyOptimal)
+
+ err = app.generateMipmaps(app.textureImage, core1_0.FormatR8G8B8A8SRGB, imageDims.X, imageDims.Y, app.mipLevels)
if err != nil {
return err
}
@@ -1196,9 +1201,117 @@ func (app *HelloTriangleApplication) createTextureImage() error {
return nil
}
+func (app *HelloTriangleApplication) generateMipmaps(image core1_0.Image, imageFormat core1_0.Format, width, height int, mipLevels int) error {
+
+ properties := app.physicalDevice.FormatProperties(imageFormat)
+
+ if (properties.OptimalTilingFeatures & core1_0.FormatFeatureSampledImageFilterLinear) == 0 {
+ return errors.Newf("texture image format %s does not support linear blitting", imageFormat)
+ }
+
+ commandBuffer, err := app.beginSingleTimeCommands()
+ if err != nil {
+ return err
+ }
+
+ barrier := core1_0.ImageMemoryBarrier{
+ Image: image,
+ SrcQueueFamilyIndex: -1,
+ DstQueueFamilyIndex: -1,
+ SubresourceRange: core1_0.ImageSubresourceRange{
+ AspectMask: core1_0.ImageAspectColor,
+ BaseArrayLayer: 0,
+ LayerCount: 1,
+ LevelCount: 1,
+ },
+ }
+
+ mipWidth := width
+ mipHeight := height
+ for i := 1; i < mipLevels; i++ {
+ barrier.SubresourceRange.BaseMipLevel = i - 1
+ barrier.OldLayout = core1_0.ImageLayoutTransferDstOptimal
+ barrier.NewLayout = core1_0.ImageLayoutTransferSrcOptimal
+ barrier.SrcAccessMask = core1_0.AccessTransferWrite
+ barrier.DstAccessMask = core1_0.AccessTransferRead
+
+ err = commandBuffer.CmdPipelineBarrier(core1_0.PipelineStageTransfer, core1_0.PipelineStageTransfer, 0, nil, nil, []core1_0.ImageMemoryBarrier{barrier})
+ if err != nil {
+ return err
+ }
+
+ nextMipWidth := mipWidth
+ nextMipHeight := mipHeight
+
+ if nextMipWidth > 1 {
+ nextMipWidth /= 2
+ }
+ if nextMipHeight > 1 {
+ nextMipHeight /= 2
+ }
+ err = commandBuffer.CmdBlitImage(image, core1_0.ImageLayoutTransferSrcOptimal, image, core1_0.ImageLayoutTransferDstOptimal, []core1_0.ImageBlit{
+ {
+ SrcSubresource: core1_0.ImageSubresourceLayers{
+ AspectMask: core1_0.ImageAspectColor,
+ MipLevel: i - 1,
+ BaseArrayLayer: 0,
+ LayerCount: 1,
+ },
+ SrcOffsets: [2]core1_0.Offset3D{
+ {X: 0, Y: 0, Z: 0},
+ {X: mipWidth, Y: mipHeight, Z: 1},
+ },
+
+ DstSubresource: core1_0.ImageSubresourceLayers{
+ AspectMask: core1_0.ImageAspectColor,
+ MipLevel: i,
+ BaseArrayLayer: 0,
+ LayerCount: 1,
+ },
+ DstOffsets: [2]core1_0.Offset3D{
+ {X: 0, Y: 0, Z: 0},
+ {X: nextMipWidth, Y: nextMipHeight, Z: 1},
+ },
+ },
+ }, core1_0.FilterLinear)
+ if err != nil {
+ return err
+ }
+
+ barrier.OldLayout = core1_0.ImageLayoutTransferSrcOptimal
+ barrier.NewLayout = core1_0.ImageLayoutShaderReadOnlyOptimal
+ barrier.SrcAccessMask = core1_0.AccessTransferRead
+ barrier.DstAccessMask = core1_0.AccessShaderRead
+ err = commandBuffer.CmdPipelineBarrier(core1_0.PipelineStageTransfer, core1_0.PipelineStageFragmentShader, 0, nil, nil, []core1_0.ImageMemoryBarrier{barrier})
+ if err != nil {
+ return err
+ }
+
+ mipWidth = nextMipWidth
+ mipHeight = nextMipHeight
+ }
+
+ barrier.SubresourceRange.BaseMipLevel = mipLevels - 1
+ barrier.OldLayout = core1_0.ImageLayoutTransferDstOptimal
+ barrier.NewLayout = core1_0.ImageLayoutShaderReadOnlyOptimal
+ barrier.SrcAccessMask = core1_0.AccessTransferWrite
+ barrier.DstAccessMask = core1_0.AccessShaderRead
+
+ err = commandBuffer.CmdPipelineBarrier(
+ core1_0.PipelineStageTransfer,
+ core1_0.PipelineStageFragmentShader,
+ 0, nil, nil,
+ []core1_0.ImageMemoryBarrier{barrier})
+ if err != nil {
+ return err
+ }
+
+ return app.endSingleTimeCommands(commandBuffer)
+}
+
func (app *HelloTriangleApplication) createTextureImageView() error {
var err error
- app.textureImageView, err = app.createImageView(app.textureImage, core1_0.FormatR8G8B8A8SRGB, core1_0.ImageAspectColor)
+ app.textureImageView, err = app.createImageView(app.textureImage, core1_0.FormatR8G8B8A8SRGB, core1_0.ImageAspectColor, app.mipLevels)
return err
}
@@ -1221,12 +1334,14 @@ func (app *HelloTriangleApplication) createSampler() error {
BorderColor: core1_0.BorderColorIntOpaqueBlack,
MipmapMode: core1_0.SamplerMipmapModeLinear,
+ MinLod: 0,
+ MaxLod: float32(app.mipLevels),
})
return err
}
-func (app *HelloTriangleApplication) createImageView(image core1_0.Image, format core1_0.Format, aspect core1_0.ImageAspectFlags) (core1_0.ImageView, error) {
+func (app *HelloTriangleApplication) createImageView(image core1_0.Image, format core1_0.Format, aspect core1_0.ImageAspectFlags, mipLevels int) (core1_0.ImageView, error) {
imageView, _, err := app.device.CreateImageView(nil, core1_0.ImageViewCreateInfo{
Image: image,
ViewType: core1_0.ImageViewType2D,
@@ -1234,7 +1349,7 @@ func (app *HelloTriangleApplication) createImageView(image core1_0.Image, format
SubresourceRange: core1_0.ImageSubresourceRange{
AspectMask: aspect,
BaseMipLevel: 0,
- LevelCount: 1,
+ LevelCount: mipLevels,
BaseArrayLayer: 0,
LayerCount: 1,
},
@@ -1242,7 +1357,7 @@ func (app *HelloTriangleApplication) createImageView(image core1_0.Image, format
return imageView, err
}
-func (app *HelloTriangleApplication) createImage(width, height int, format core1_0.Format, tiling core1_0.ImageTiling, usage core1_0.ImageUsageFlags, memoryProperties core1_0.MemoryPropertyFlags) (core1_0.Image, core1_0.DeviceMemory, error) {
+func (app *HelloTriangleApplication) createImage(width, height int, mipLevels int, format core1_0.Format, tiling core1_0.ImageTiling, usage core1_0.ImageUsageFlags, memoryProperties core1_0.MemoryPropertyFlags) (core1_0.Image, core1_0.DeviceMemory, error) {
image, _, err := app.device.CreateImage(nil, core1_0.ImageCreateInfo{
ImageType: core1_0.ImageType2D,
Extent: core1_0.Extent3D{
@@ -1250,7 +1365,7 @@ func (app *HelloTriangleApplication) createImage(width, height int, format core1
Height: height,
Depth: 1,
},
- MipLevels: 1,
+ MipLevels: mipLevels,
ArrayLayers: 1,
Format: format,
Tiling: tiling,
@@ -1282,7 +1397,7 @@ func (app *HelloTriangleApplication) createImage(width, height int, format core1
return image, imageMemory, nil
}
-func (app *HelloTriangleApplication) transitionImageLayout(image core1_0.Image, format core1_0.Format, oldLayout core1_0.ImageLayout, newLayout core1_0.ImageLayout) error {
+func (app *HelloTriangleApplication) transitionImageLayout(image core1_0.Image, format core1_0.Format, oldLayout core1_0.ImageLayout, newLayout core1_0.ImageLayout, mipLevels int) error {
buffer, err := app.beginSingleTimeCommands()
if err != nil {
return err
@@ -1315,7 +1430,7 @@ func (app *HelloTriangleApplication) transitionImageLayout(image core1_0.Image,
SubresourceRange: core1_0.ImageSubresourceRange{
AspectMask: core1_0.ImageAspectColor,
BaseMipLevel: 0,
- LevelCount: 1,
+ LevelCount: mipLevels,
BaseArrayLayer: 0,
LayerCount: 1,
},