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)
| 458 | // vk.FormatR8g8b8a8Srgb (strongly recommended in any case). |
| 459 | // If format is vk.FormatR8g8b8a8Unorm, it will be converted to srgb. |
| 460 | func (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 |
no test coverage detected