-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtexture.go
148 lines (118 loc) · 3.8 KB
/
texture.go
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
// Copyright (c) 2022, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vgpu
import (
vk "github.com/goki/vulkan"
)
// Texture supplies an Image and a Sampler
type Texture struct {
Image
// sampler for image
Sampler
}
func (tx *Texture) Defaults() {
tx.Image.Format.Defaults()
tx.Sampler.Defaults()
}
func (tx *Texture) Destroy() {
tx.Sampler.Destroy(tx.Image.Dev)
tx.Image.Destroy()
}
// AllocTexture allocates texture device image, stdview, and sampler
func (tx *Texture) AllocTexture() {
tx.AllocImage()
if tx.Sampler.VkSampler == vk.NullSampler {
tx.Sampler.Config(tx.GPU, tx.Dev)
}
tx.ConfigStdView()
}
///////////////////////////////////////////////////
// Sampler represents a vulkan image sampler
type Sampler struct {
Name string
// for U (horizontal) axis -- what to do when going off the edge
UMode SamplerModes
// for V (vertical) axis -- what to do when going off the edge
VMode SamplerModes
// for W (horizontal) axis -- what to do when going off the edge
WMode SamplerModes
// border color for Clamp modes
Border BorderColors
// the vulkan sampler
VkSampler vk.Sampler
}
func (sm *Sampler) Defaults() {
sm.UMode = Repeat
sm.VMode = Repeat
sm.WMode = Repeat
sm.Border = BorderTrans
}
// Config configures sampler on device
func (sm *Sampler) Config(gp *GPU, dev vk.Device) {
sm.Destroy(dev)
var samp vk.Sampler
ret := vk.CreateSampler(dev, &vk.SamplerCreateInfo{
SType: vk.StructureTypeSamplerCreateInfo,
MagFilter: vk.FilterLinear,
MinFilter: vk.FilterLinear,
AddressModeU: sm.UMode.VkMode(),
AddressModeV: sm.VMode.VkMode(),
AddressModeW: sm.WMode.VkMode(),
AnisotropyEnable: vk.True,
MaxAnisotropy: gp.GPUProperties.Limits.MaxSamplerAnisotropy,
BorderColor: sm.Border.VkColor(),
UnnormalizedCoordinates: vk.False,
CompareEnable: vk.False,
MipmapMode: vk.SamplerMipmapModeLinear,
}, nil, &samp)
IfPanic(NewError(ret))
sm.VkSampler = samp
}
func (sm *Sampler) Destroy(dev vk.Device) {
if sm.VkSampler != vk.NullSampler {
vk.DestroySampler(dev, sm.VkSampler, nil)
sm.VkSampler = vk.NullSampler
}
}
// Texture image sampler modes
type SamplerModes int32 //enums:enum
const (
// Repeat the texture when going beyond the image dimensions.
Repeat SamplerModes = iota
// Like repeat, but inverts the coordinates to mirror the image when going beyond the dimensions.
MirroredRepeat
// Take the color of the edge closest to the coordinate beyond the image dimensions.
ClampToEdge
// Return a solid color when sampling beyond the dimensions of the image.
ClampToBorder
// Like clamp to edge, but instead uses the edge opposite to the closest edge.
MirrorClampToEdge
)
func (sm SamplerModes) VkMode() vk.SamplerAddressMode {
return VulkanSamplerModes[sm]
}
var VulkanSamplerModes = map[SamplerModes]vk.SamplerAddressMode{
Repeat: vk.SamplerAddressModeRepeat,
MirroredRepeat: vk.SamplerAddressModeMirroredRepeat,
ClampToEdge: vk.SamplerAddressModeClampToEdge,
ClampToBorder: vk.SamplerAddressModeClampToBorder,
MirrorClampToEdge: vk.SamplerAddressModeMirrorClampToEdge,
}
//////////////////////////////////////////////////////
// Texture image sampler modes
type BorderColors int32 //enums:enum -trim-prefix Border
const (
// Repeat the texture when going beyond the image dimensions.
BorderTrans BorderColors = iota
BorderBlack
BorderWhite
)
func (bc BorderColors) VkColor() vk.BorderColor {
return VulkanBorderColors[bc]
}
var VulkanBorderColors = map[BorderColors]vk.BorderColor{
BorderTrans: vk.BorderColorIntTransparentBlack,
BorderBlack: vk.BorderColorIntOpaqueBlack,
BorderWhite: vk.BorderColorIntOpaqueWhite,
}