texture.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //go:build !js
  2. package wgpu
  3. /*
  4. #include <stdlib.h>
  5. #include "./lib/wgpu.h"
  6. extern void gowebgpu_error_callback_c(WGPUErrorType type, char const * message, void * userdata);
  7. static inline WGPUTextureView gowebgpu_texture_create_view(WGPUTexture texture, WGPUTextureViewDescriptor const * descriptor, WGPUDevice device, void * error_userdata) {
  8. WGPUTextureView ref = NULL;
  9. wgpuDevicePushErrorScope(device, WGPUErrorFilter_Validation);
  10. ref = wgpuTextureCreateView(texture, descriptor);
  11. wgpuDevicePopErrorScope(device, gowebgpu_error_callback_c, error_userdata);
  12. return ref;
  13. }
  14. static inline void gowebgpu_texture_release(WGPUTexture texture, WGPUDevice device) {
  15. wgpuDeviceRelease(device);
  16. wgpuTextureRelease(texture);
  17. }
  18. */
  19. import "C"
  20. import (
  21. "errors"
  22. "runtime/cgo"
  23. "unsafe"
  24. )
  25. type Texture struct {
  26. deviceRef C.WGPUDevice
  27. ref C.WGPUTexture
  28. }
  29. func (p *Texture) CreateView(descriptor *TextureViewDescriptor) (*TextureView, error) {
  30. var desc *C.WGPUTextureViewDescriptor
  31. if descriptor != nil {
  32. desc = &C.WGPUTextureViewDescriptor{
  33. format: C.WGPUTextureFormat(descriptor.Format),
  34. dimension: C.WGPUTextureViewDimension(descriptor.Dimension),
  35. baseMipLevel: C.uint32_t(descriptor.BaseMipLevel),
  36. mipLevelCount: C.uint32_t(descriptor.MipLevelCount),
  37. baseArrayLayer: C.uint32_t(descriptor.BaseArrayLayer),
  38. arrayLayerCount: C.uint32_t(descriptor.ArrayLayerCount),
  39. aspect: C.WGPUTextureAspect(descriptor.Aspect),
  40. }
  41. if descriptor.Label != "" {
  42. label := C.CString(descriptor.Label)
  43. defer C.free(unsafe.Pointer(label))
  44. desc.label = label
  45. }
  46. }
  47. var err error = nil
  48. var cb errorCallback = func(_ ErrorType, message string) {
  49. err = errors.New("wgpu.(*Texture).CreateView(): " + message)
  50. }
  51. errorCallbackHandle := cgo.NewHandle(cb)
  52. defer errorCallbackHandle.Delete()
  53. ref := C.gowebgpu_texture_create_view(
  54. p.ref,
  55. desc,
  56. p.deviceRef,
  57. unsafe.Pointer(&errorCallbackHandle),
  58. )
  59. if err != nil {
  60. C.wgpuTextureViewRelease(ref)
  61. return nil, err
  62. }
  63. return &TextureView{ref}, nil
  64. }
  65. func (p *Texture) Destroy() {
  66. C.wgpuTextureDestroy(p.ref)
  67. }
  68. func (p *Texture) GetDepthOrArrayLayers() uint32 {
  69. return uint32(C.wgpuTextureGetDepthOrArrayLayers(p.ref))
  70. }
  71. func (p *Texture) GetDimension() TextureDimension {
  72. return TextureDimension(C.wgpuTextureGetDimension(p.ref))
  73. }
  74. func (p *Texture) GetFormat() TextureFormat {
  75. return TextureFormat(C.wgpuTextureGetFormat(p.ref))
  76. }
  77. func (p *Texture) GetHeight() uint32 {
  78. return uint32(C.wgpuTextureGetHeight(p.ref))
  79. }
  80. func (p *Texture) GetMipLevelCount() uint32 {
  81. return uint32(C.wgpuTextureGetMipLevelCount(p.ref))
  82. }
  83. func (p *Texture) GetSampleCount() uint32 {
  84. return uint32(C.wgpuTextureGetSampleCount(p.ref))
  85. }
  86. func (p *Texture) GetUsage() TextureUsage {
  87. return TextureUsage(C.wgpuTextureGetUsage(p.ref))
  88. }
  89. func (p *Texture) GetWidth() uint32 {
  90. return uint32(C.wgpuTextureGetWidth(p.ref))
  91. }
  92. func (p *Texture) Release() {
  93. C.gowebgpu_texture_release(p.ref, p.deviceRef)
  94. }