Skip to content

Commit

Permalink
fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-m committed May 20, 2024
1 parent 94a17fa commit 2b76d8b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
5 changes: 4 additions & 1 deletion examples/pmatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func main() {

// Benchmark.
bench(func() {
fn(img, pat.(*image.RGBA))
_, _, _, err := fn(img, pat.(*image.RGBA))
if err != nil {
panic(err)
}
}, 100)
}
26 changes: 20 additions & 6 deletions pkg/pmatch/vk.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func (r results) size() int {
return buf.Len()
}

// SearchVk holds the state needed for patch matching search via Vulkan.
// Use NewSearchVk create an instance.
type SearchVk struct {
searchRect image.Rectangle
h *vk.Handle
Expand All @@ -58,6 +60,7 @@ type SearchVk struct {
pipe *vk.Pipe
}

// Destroy destroys a SearchVk and frees resources.
func (s *SearchVk) Destroy() {
if s.pipe != nil {
s.pipe.Destroy(s.h)
Expand All @@ -81,6 +84,8 @@ func (s *SearchVk) Destroy() {
}
}

// NewSearchVk creates a new instance of SearchVk.
// Destroy must be called to clean up.
func NewSearchVk(imgBounds, patBounds image.Rectangle, imgStride, patStride int) (*SearchVk, error) {
if patBounds.Size().X > imgBounds.Size().X ||
patBounds.Size().Y > imgBounds.Size().Y {
Expand Down Expand Up @@ -123,7 +128,7 @@ func NewSearchVk(imgBounds, patBounds image.Rectangle, imgStride, patStride int)
}

// Create pipe.
binary.Write(&s.pushConstants, binary.LittleEndian, pushConstants{})
binary.Write(&s.pushConstants, binary.LittleEndian, pushConstants{}) // #nosec G104: bytes.Buffer.Write{} always returns err = nil
specInfo := []int{
// Local size.
localSizeX,
Expand All @@ -146,6 +151,7 @@ func NewSearchVk(imgBounds, patBounds image.Rectangle, imgStride, patStride int)
return &s, nil
}

// Run runs a search.
func (s *SearchVk) Run(img, pat *image.RGBA) (maxX, maxY int, maxCos float64, err error) {
searchRect := image.Rectangle{
Min: img.Bounds().Min,
Expand All @@ -157,22 +163,25 @@ func (s *SearchVk) Run(img, pat *image.RGBA) (maxX, maxY int, maxCos float64, er
}

// Write to buffers.
err = s.imgBuf.Write(s.h, unsafe.Pointer(&img.Pix[0]), bufsz(img))
err = s.imgBuf.Write(s.h, unsafe.Pointer(&img.Pix[0]), bufsz(img)) // #nosec G103
if err != nil {
return
}

err = s.patBuf.Write(s.h, unsafe.Pointer(&pat.Pix[0]), bufsz(pat))
err = s.patBuf.Write(s.h, unsafe.Pointer(&pat.Pix[0]), bufsz(pat)) // #nosec G103
if err != nil {
return
}

// Prepare push constants.
s.pushConstants.Reset()
binary.Write(
err = binary.Write(
&s.pushConstants,
binary.LittleEndian,
pushConstants{uint32(img.Stride), uint32(pat.Stride)})
if err != nil {
return
}

// Run.
err = s.pipe.Run(
Expand All @@ -190,14 +199,19 @@ func (s *SearchVk) Run(img, pat *image.RGBA) (maxX, maxY int, maxCos float64, er
// Read results.
res := results{}
resMem := make([]byte, res.size())
err = s.resultsBuf.Read(s.h, unsafe.Pointer(&resMem[0]), s.resSize)
err = s.resultsBuf.Read(s.h, unsafe.Pointer(&resMem[0]), s.resSize) // #nosec G103
if err != nil {
return
}
err = binary.Read(bytes.NewReader(resMem), binary.LittleEndian, &res)
if err != nil {
return
}
binary.Read(bytes.NewReader(resMem), binary.LittleEndian, &res)
return int(res.MaxX), int(res.MaxY), float64(res.Max), nil
}

// SearchRGBAVk is a wrapper which allocates a SearchVk, runs a search, and then destroys the instance again.
// Should only be used for testing, in real applications the instance should be reused.
func SearchRGBAVk(img, pat *image.RGBA) (maxX, maxY int, maxCos float64) {
h, err := NewSearchVk(img.Bounds(), pat.Bounds(), img.Stride, pat.Stride)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/vk/vk.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"unsafe"
)

// Package errors.
var (
ErrNotLittleEndian = errors.New("only little endian supported for now")
ErrNeedAtLeastOneBuffer = errors.New("need at least one buffer")
Expand Down Expand Up @@ -302,7 +303,7 @@ func (p *Pipe) Run(h *Handle, workgroupSize [3]uint, pushConstantBuf []byte) err
C.uint32_t(workgroupSize[2]),
}

var pushConstantPtr *byte = nil
var pushConstantPtr *byte
if len(pushConstantBuf) > 0 {
pushConstantPtr = &pushConstantBuf[0]
}
Expand Down

0 comments on commit 2b76d8b

Please sign in to comment.