-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path24_sampler.diff
More file actions
147 lines (138 loc) · 4.77 KB
/
24_sampler.diff
File metadata and controls
147 lines (138 loc) · 4.77 KB
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
diff --git a/../steps/23_texture_image/main.go b/../steps/24_sampler/main.go
index 38e9db5..5fdfdf0 100644
--- a/../steps/23_texture_image/main.go
+++ b/../steps/24_sampler/main.go
@@ -147,6 +147,8 @@ type HelloTriangleApplication struct {
textureImage core1_0.Image
textureImageMemory core1_0.DeviceMemory
+ textureImageView core1_0.ImageView
+ textureSampler core1_0.Sampler
}
func (app *HelloTriangleApplication) Run() error {
@@ -249,6 +251,16 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
+ err = app.createTextureImageView()
+ if err != nil {
+ return err
+ }
+
+ err = app.createSampler()
+ if err != nil {
+ return err
+ }
+
err = app.createVertexBuffer()
if err != nil {
return err
@@ -372,6 +384,14 @@ func (app *HelloTriangleApplication) cleanupSwapChain() {
func (app *HelloTriangleApplication) cleanup() {
app.cleanupSwapChain()
+ if app.textureSampler != nil {
+ app.textureSampler.Destroy(nil)
+ }
+
+ if app.textureImageView != nil {
+ app.textureImageView.Destroy(nil)
+ }
+
if app.textureImage != nil {
app.textureImage.Destroy(nil)
}
@@ -658,8 +678,10 @@ func (app *HelloTriangleApplication) createLogicalDevice() error {
}
app.device, _, err = app.physicalDevice.CreateDevice(nil, core1_0.DeviceCreateInfo{
- QueueCreateInfos: queueFamilyOptions,
- EnabledFeatures: &core1_0.PhysicalDeviceFeatures{},
+ QueueCreateInfos: queueFamilyOptions,
+ EnabledFeatures: &core1_0.PhysicalDeviceFeatures{
+ SamplerAnisotropy: true,
+ },
EnabledExtensionNames: extensionNames,
})
if err != nil {
@@ -738,24 +760,7 @@ func (app *HelloTriangleApplication) createImageViews() error {
var imageViews []core1_0.ImageView
for _, image := range images {
- view, _, err := app.device.CreateImageView(nil, core1_0.ImageViewCreateInfo{
- ViewType: core1_0.ImageViewType2D,
- Image: image,
- Format: app.swapchainImageFormat,
- Components: core1_0.ComponentMapping{
- R: core1_0.ComponentSwizzleIdentity,
- G: core1_0.ComponentSwizzleIdentity,
- B: core1_0.ComponentSwizzleIdentity,
- A: core1_0.ComponentSwizzleIdentity,
- },
- SubresourceRange: core1_0.ImageSubresourceRange{
- AspectMask: core1_0.ImageAspectColor,
- BaseMipLevel: 0,
- LevelCount: 1,
- BaseArrayLayer: 0,
- LayerCount: 1,
- },
- })
+ view, err := app.createImageView(image, app.swapchainImageFormat)
if err != nil {
return err
}
@@ -1081,6 +1086,52 @@ func (app *HelloTriangleApplication) createTextureImage() error {
return nil
}
+func (app *HelloTriangleApplication) createTextureImageView() error {
+ var err error
+ app.textureImageView, err = app.createImageView(app.textureImage, core1_0.FormatR8G8B8A8SRGB)
+ return err
+}
+
+func (app *HelloTriangleApplication) createSampler() error {
+ properties, err := app.physicalDevice.Properties()
+ if err != nil {
+ return err
+ }
+
+ app.textureSampler, _, err = app.device.CreateSampler(nil, core1_0.SamplerCreateInfo{
+ MagFilter: core1_0.FilterLinear,
+ MinFilter: core1_0.FilterLinear,
+ AddressModeU: core1_0.SamplerAddressModeRepeat,
+ AddressModeV: core1_0.SamplerAddressModeRepeat,
+ AddressModeW: core1_0.SamplerAddressModeRepeat,
+
+ AnisotropyEnable: true,
+ MaxAnisotropy: properties.Limits.MaxSamplerAnisotropy,
+
+ BorderColor: core1_0.BorderColorIntOpaqueBlack,
+
+ MipmapMode: core1_0.SamplerMipmapModeLinear,
+ })
+
+ return err
+}
+
+func (app *HelloTriangleApplication) createImageView(image core1_0.Image, format core1_0.Format) (core1_0.ImageView, error) {
+ imageView, _, err := app.device.CreateImageView(nil, core1_0.ImageViewCreateInfo{
+ Image: image,
+ ViewType: core1_0.ImageViewType2D,
+ Format: format,
+ SubresourceRange: core1_0.ImageSubresourceRange{
+ AspectMask: core1_0.ImageAspectColor,
+ BaseMipLevel: 0,
+ LevelCount: 1,
+ BaseArrayLayer: 0,
+ LayerCount: 1,
+ },
+ })
+ 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) {
image, _, err := app.device.CreateImage(nil, core1_0.ImageCreateInfo{
ImageType: core1_0.ImageType2D,
@@ -1695,7 +1746,8 @@ func (app *HelloTriangleApplication) isDeviceSuitable(device core1_0.PhysicalDev
swapChainAdequate = len(swapChainSupport.Formats) > 0 && len(swapChainSupport.PresentModes) > 0
}
- return indices.IsComplete() && extensionsSupported && swapChainAdequate
+ features := device.Features()
+ return indices.IsComplete() && extensionsSupported && swapChainAdequate && features.SamplerAnisotropy
}
func (app *HelloTriangleApplication) checkDeviceExtensionSupport(device core1_0.PhysicalDevice) bool {