forked from cogentcore/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampler.go
129 lines (106 loc) · 3.26 KB
/
sampler.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
// Copyright (c) 2024, 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 gpu
import (
"cogentcore.org/core/base/errors"
"github.com/cogentcore/webgpu/wgpu"
)
// Sampler represents a WebGPU 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 WebGPU sampler
sampler *wgpu.Sampler
}
func (sm *Sampler) Defaults() {
sm.UMode = Repeat
sm.VMode = Repeat
sm.WMode = Repeat
sm.Border = BorderTrans
}
// Config configures sampler on device. If the sampler
// already exists, then it is not reconfigured.
// Use Release first to force a reconfigure.
func (sm *Sampler) Config(dev *Device) error {
if sm.sampler != nil {
return nil
}
samp, err := dev.Device.CreateSampler(&wgpu.SamplerDescriptor{
AddressModeU: sm.UMode.Mode(),
AddressModeV: sm.VMode.Mode(),
AddressModeW: sm.WMode.Mode(),
MagFilter: wgpu.FilterModeLinear, // nearest?
MinFilter: wgpu.FilterModeLinear,
MipmapFilter: wgpu.MipmapFilterModeLinear,
LodMinClamp: 0,
LodMaxClamp: 32,
Compare: wgpu.CompareFunctionUndefined,
MaxAnisotropy: 1,
})
// MaxAnisotropy: gp.GPUProperties.Limits.MaxSamplerAnisotropy,
// BorderColor: sm.Border.VkColor(),
// UnnormalizedCoordinates: vk.False,
// CompareEnable: vk.False,
if errors.Log(err) != nil {
return err
}
sm.sampler = samp
return nil
}
func (sm *Sampler) Release() {
if sm.sampler == nil {
return
}
sm.sampler.Release()
sm.sampler = nil
}
// 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.
MirrorRepeat
// 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) Mode() wgpu.AddressMode {
return WebGPUSamplerModes[sm]
}
var WebGPUSamplerModes = map[SamplerModes]wgpu.AddressMode{
Repeat: wgpu.AddressModeRepeat,
MirrorRepeat: wgpu.AddressModeMirrorRepeat,
ClampToEdge: wgpu.AddressModeClampToEdge,
}
//////////////////////////////////////////////////////
// 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
)
// todo: not (yet) supported
//
// func (bc BorderColors) VkColor() vk.BorderColor {
// return VulkanBorderColors[bc]
// }
//
// var VulkanBorderColors = map[BorderColors]vk.BorderColor{
// BorderTrans: vk.BorderColorIntTransparentBlack,
// BorderBlack: vk.BorderColorIntOpaqueBlack,
// BorderWhite: vk.BorderColorIntOpaqueWhite,
// }