MCPcopy Create free account
hub / github.com/cogentcore/core / DevGoImageCopy

Method DevGoImageCopy

vgpu/image.go:460–487  ·  view source on GitHub ↗

DevGoImageCopy sets the given image.RGBA standard Go image to a copy of the HostOnly Device memory representation, re-sizing the pixel memory as needed. If the image pixels are sufficiently sized, no memory allocation occurs. Only works if ImageOnHostOnly, and works best if Format is default vk.Form

(rgba *image.RGBA)

Source from the content-addressed store, hash-verified

458// vk.FormatR8g8b8a8Srgb (strongly recommended in any case).
459// If format is vk.FormatR8g8b8a8Unorm, it will be converted to srgb.
460func (im *Image) DevGoImageCopy(rgba *image.RGBA) error {
461 if !im.HasFlag(ImageOnHostOnly) || im.Mem == vk.NullDeviceMemory {
462 return fmt.Errorf("vgpu.Image DevGoImage: Image not available because device Image is not HostOnly, or Mem is nil: %s", im.Name)
463 }
464 if !im.Format.IsStdRGBA() && !im.Format.IsRGBAUnorm() {
465 return fmt.Errorf("vgpu.Image DevGoImage: Device image is not standard RGBA or Unorm format: %s", im.Format.String())
466 }
467
468 size := im.Format.LayerByteSize()
469 subrec := vk.ImageSubresource{}
470 subrec.AspectMask = vk.ImageAspectFlags(vk.ImageAspectColorBit)
471 sublay := vk.SubresourceLayout{}
472 vk.GetImageSubresourceLayout(im.Dev, im.Image, &subrec, &sublay)
473 offset := int(sublay.Offset)
474 ptr := MapMemoryAll(im.Dev, im.Mem)
475 pix := (*[ByteCopyMemoryLimit]byte)(ptr)[offset : size+offset]
476
477 rgba.Pix = slicesx.SetLength(rgba.Pix, size)
478 copy(rgba.Pix, pix)
479 vk.UnmapMemory(im.Dev, im.Mem)
480 if im.Format.IsRGBAUnorm() {
481 fmt.Println("converting to linear")
482 SetImageSRGBFromLinear(rgba)
483 }
484 rgba.Stride = im.Format.Stride()
485 rgba.Rect = image.Rect(0, 0, im.Format.Size.X, im.Format.Size.Y)
486 return nil
487}
488
489// UnmapDev calls UnmapMemory on the mapped memory for this image,
490// set by MapMemoryAll. This must be called after image is used in

Callers 1

ImageCopyMethod · 0.80

Calls 11

HasFlagMethod · 0.95
SetLengthFunction · 0.92
MapMemoryAllFunction · 0.85
SetImageSRGBFromLinearFunction · 0.85
IsStdRGBAMethod · 0.80
IsRGBAUnormMethod · 0.80
LayerByteSizeMethod · 0.80
PrintlnMethod · 0.80
StrideMethod · 0.80
ErrorfMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected