Skip to content

Commit

Permalink
refactor: Update image creation functions to use generic 'image' inst…
Browse files Browse the repository at this point in the history
…ead of 'image_2d'
  • Loading branch information
ulises-jeremias committed Jun 10, 2024
1 parent 506e218 commit 4800dc9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
8 changes: 4 additions & 4 deletions examples/vcl_opencl_fractals_one_argument/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn run_kernel(kernel_name string) ! {
}

// Create image buffer (image2d_t) to kernel
mut img := device.image_2d(.rgba, width: width, height: height)!
mut img := device.image(.rgba, width: width, height: height)!
defer {
img.release() or { panic(err) }
}
Expand All @@ -52,11 +52,11 @@ fn run_kernel(kernel_name string) ! {
}

// get image data from buffer and save it
buffer := img.data_2d()!
img := img.data()!
stbi.stbi_write_bmp(os.join_path(output_dir, '${kernel_name}.bmp'), width, height,
4, buffer.data) or { return err }
4, img.data) or { return err }
stbi.stbi_write_png(os.join_path(output_dir, '${kernel_name}.png'), width, height,
4, buffer.data, 0) or { return err }
4, img.data, 0) or { return err }
}

fn main() {
Expand Down
7 changes: 4 additions & 3 deletions examples/vcl_opencl_image_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ fn main() {
stbi_img := stbi.load(os.join_path(root, 'julia.png')) or { panic(err) }

// Create image buffer (image2d_t) to read_only
mut img := device.from_image_2d(stbi_img) or { panic(err) }
mut img := device.from_image(stbi_img) or { panic(err) }
defer {
img.release() or { panic(err) }
}

// Create image buffer (image2d_t) to write_only
mut inverted_img := device.image_2d(.rgba, width: img.bounds.width, height: img.bounds.height)!
// mut inverted_img := device.image(.rgba, width: img.bounds.width, height: img.bounds.height)!
mut inverted_img := device.from_image(stbi_img) or { panic(err) }
defer {
inverted_img.release() or { panic(err) }
}
Expand All @@ -42,7 +43,7 @@ fn main() {
panic(kernel_err)
}

next_inverted_img := inverted_img.data_2d()!
next_inverted_img := inverted_img.data()!
// save image
stbi.stbi_write_png(os.join_path(output_dir, 'inverted.png'), int(inverted_img.bounds.width),
int(inverted_img.bounds.height), 4, next_inverted_img.data, 0)!
Expand Down
39 changes: 24 additions & 15 deletions vcl/image.c.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module vcl

import stbi

// Rect is a struct that represents a rectangle shape
@[params]
pub struct Rect {
Expand Down Expand Up @@ -35,13 +37,13 @@ pub fn (mut img Image) release() ! {
return img.buf.release()
}

// image_2d allocates an image buffer
pub fn (d &Device) image_2d(@type ImageChannelOrder, bounds Rect) !&Image {
return d.create_image_2d(@type, bounds, unsafe { nil })
// image allocates an image buffer
pub fn (d &Device) image(@type ImageChannelOrder, bounds Rect) !&Image {
return d.create_image(@type, bounds, 0, unsafe { nil })
}

// from_image_2d creates new Image and copies data from Image
pub fn (d &Device) from_image_2d(img IImage) !&Image {
// from_image creates new Image and copies data from Image
pub fn (d &Device) from_image(img IImage) !&Image {
data := img.data
mut image_type := ImageChannelOrder.intensity

Expand All @@ -50,16 +52,16 @@ pub fn (d &Device) from_image_2d(img IImage) !&Image {
}

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

// create_image_2d creates a new image
fn (d &Device) create_image_2d(image_type ImageChannelOrder, bounds Rect, data voidptr) !&Image {
mut row_pitch := int(bounds.width)
// create_image creates a new image
fn (d &Device) create_image(image_type ImageChannelOrder, bounds Rect, row_pitch int, data voidptr) !&Image {
mut row_pitch_ := int(bounds.width)
mut size := int(bounds.width * bounds.height)
if image_type == ImageChannelOrder.rgba {
size *= 4
row_pitch *= 4
row_pitch_ *= 4
}
format := create_image_format(usize(image_type), usize(ImageChannelDataType.unorm_int8))

Expand All @@ -68,8 +70,9 @@ fn (d &Device) create_image_2d(image_type ImageChannelOrder, bounds Rect, data v
flags = mem_read_write | mem_copy_host_ptr
}
mut ret := 0
// TODO: Figure out how to avoid using the deprecated clCreateImage2D
memobj := cl_create_image2d(d.ctx, flags, format, usize(bounds.width), usize(bounds.height),
usize(row_pitch), data, &ret)
usize(row_pitch_), data, &ret)
if ret != success {
return error_from_code(ret)
}
Expand Down Expand Up @@ -98,14 +101,20 @@ fn (d &Device) create_image_2d(image_type ImageChannelOrder, bounds Rect, data v
return img
}

pub fn (image &Image) data_2d() ![]u8 {
pub fn (image &Image) data() !IImage {
origin := [3]usize{init: 0}
region0 := [usize(image.bounds.width), usize(image.bounds.height), 1]
region := [3]usize{init: region0[index]}
result := []u8{len: image.buf.size, cap: image.buf.size}
read_image_result := []u8{len: image.buf.size, cap: image.buf.size}
ret := cl_enqueue_read_image(image.buf.device.queue, image.buf.memobj, true, origin,
region, 0, 0, unsafe { &result[0] }, 0, unsafe { nil }, unsafe { nil })
return error_or_default(ret, result)
region, 0, 0, unsafe { &read_image_result[0] }, 0, unsafe { nil }, unsafe { nil })
img := stbi.Image{
width: int(image.bounds.width)
height: int(image.bounds.height)
nr_channels: 4
data: unsafe { &read_image_result[0] }
}
return error_or_default(ret, IImage(img))
}

fn (image &Image) write_queue() !int {
Expand Down

0 comments on commit 4800dc9

Please sign in to comment.