Skip to content

Commit

Permalink
refactor: Update create_image_2d function to use local variable for f…
Browse files Browse the repository at this point in the history
…ormat
  • Loading branch information
ulises-jeremias committed Jun 10, 2024
1 parent b47d5a6 commit 506e218
Showing 1 changed file with 1 addition and 66 deletions.
67 changes: 1 addition & 66 deletions vcl/image.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ fn (d &Device) create_image_2d(image_type ImageChannelOrder, bounds Rect, data v
size *= 4
row_pitch *= 4
}
format := C.create_image_format(usize(image_type), usize(ImageChannelDataType.unorm_int8))
format := create_image_format(usize(image_type), usize(ImageChannelDataType.unorm_int8))

mut flags := mem_read_write

if !isnil(data) {
flags = mem_read_write | mem_copy_host_ptr
}
Expand Down Expand Up @@ -123,70 +122,6 @@ fn (image &Image) write_queue() !int {
return error_or_default(ret, ret)
}

// image_general allocates an image buffer TODO not accomplish - broken
fn (d &Device) image_general(@type ImageChannelOrder, bounds Rect) !&Image {
println(@STRUCT + '.' + @FN + ' is not stable yet. Issues are expected.')
return d.create_image_general(@type, bounds, 0, unsafe { nil })
}

// from_image_general creates new Image and copies data from Image TODO not accomplish - broken
fn (d &Device) from_image_general(img IImage) !&Image {
println(@STRUCT + '.' + @FN + ' is not stable yet. Issues are expected.')
data := img.data
mut row_pitch := 0
mut image_type := ImageChannelOrder.intensity

if img.nr_channels in [3, 4] {
image_type = ImageChannelOrder.rgba
}

bounds := Rect{0, 0, img.width, img.height}
return d.create_image_general(image_type, bounds, row_pitch, data)
}

// create_image_general creates a new image TODO not accomplish - broken
fn (d &Device) create_image_general(image_type ImageChannelOrder, bounds Rect, row_pitch int, data voidptr) !&Image {
format := C.create_image_format(usize(image_type), usize(ImageChannelDataType.unorm_int8))
desc := C.create_image_desc(C.CL_MEM_OBJECT_IMAGE2D, usize(bounds.width), usize(bounds.height),
0, 0, usize(row_pitch), 0, 0, 0, unsafe { nil })

mut flags := mem_read_write

if !isnil(data) {
flags = mem_read_write | mem_copy_host_ptr
}

mut ret := 0

memobj := cl_create_image(d.ctx, flags, format, desc, data, &ret)
if ret != success {
return error_from_code(ret)
}

if isnil(memobj) {
return err_unknown
}

mut size := int(bounds.width * bounds.height)
if image_type == ImageChannelOrder.rgba {
size *= 4
}

buf := &Buffer{
memobj: memobj
size: size
device: d
}

return &Image{
buf: buf
bounds: bounds
@type: image_type
format: format
desc: desc
}
}

// buffer returns the underlying buffer
fn (image &Image) buffer() &Buffer {
return image.buf
Expand Down

0 comments on commit 506e218

Please sign in to comment.