def thread_func(): darr_out = vki.SVVector('float', 5) darr_out2 = vki.SVVector('int', 5) forLoop.launch_n(5, [darr, darr_out, vki.SVFloat(10.0)]) forLoop.launch_n(5, [darr2, darr_out2, vki.SVInt32(5)]) print(darr_out.to_host()) print(darr_out2.to_host())
def __init__(self, width, height): self.m_width = width self.m_height = height self.m_svwidth = vki.SVInt32(width) self.m_svheight = vki.SVInt32(height) self.m_svbuf = vki.SVBuffer('vec3', width * height) self.m_cptr = SVCombine_Create( { 'data': self.m_svbuf, 'width': self.m_svwidth, 'height': self.m_svheight }, ''' vec3 read_pixel(in Comb_#hash# img, int x, int y) { return img.data[x + y*img.width].v; } void write_pixel(in Comb_#hash# img, int x, int y, in vec3 v) { img.data[x + y*img.width].v = v; } void incr_pixel(in Comb_#hash# img, int x, int y, in vec3 v) { vec3 col = read_pixel(img, x, y); write_pixel(img, x, y, col + v); } void incr_pixel(in Comb_#hash# img, int x, int y, in Spectrum col) { incr_pixel(img, x, y, to_xyz(col)); } void incr_pixel_atomic(in Comb_#hash# img, int x, int y, in vec3 v) { atomicAdd(img.data[x + y*img.width].v.x, v.x); atomicAdd(img.data[x + y*img.width].v.y, v.y); atomicAdd(img.data[x + y*img.width].v.z, v.z); } void incr_pixel_atomic(in Comb_#hash# img, int x, int y, in Spectrum col) { incr_pixel_atomic(img, x, y, to_xyz(col)); } ''') self.m_times_exposure = 0.0
def render(self, cube, filename = None, up_face_only=False, up_face_id = 2, cross_mode = False): colorBuf = vki.Texture2D(self.wh, self.wh, VK_FORMAT_R8G8B8A8_SRGB) gpu_map = vki.device_vector_from_numpy(cube.map) gpu_colors = vki.device_vector_from_numpy(self.colors) if cross_mode: up_face_only = True if not up_face_only: up_face_id = -1 gpu_up_face_id = vki.SVInt32(up_face_id) gpu_cross_mode = vki.SVInt32(cross_mode) self.rp.launch([(4*3+9)*6], [colorBuf], None, self.bg_color, 1.0, [gpu_map, gpu_colors, gpu_up_face_id, gpu_cross_mode]) image_out = np.empty((self.wh, self.wh, 4), dtype=np.uint8) colorBuf.download(image_out) if not filename is None: Image.fromarray(image_out, 'RGBA').save(filename) return image_out
def __init__(self, texId, transform=glm.identity(glm.mat3)): self.m_texId = vki.SVInt32(texId) self.m_trans = vki.SVMat3x3(transform) self.m_cptr = SVCombine_Create( { 'texId': self.m_texId, 'transform': self.m_trans }, ''' Spectrum get_sky_color(in Comb_#hash# sky, in vec3 dir) { vec3 direction = sky.transform*dir; Spectrum col; from_rgb(col, texture(arr_cubemap[sky.texId], direction).rgb); return col; } ''')
def __init__(self): self.m_t = vki.SVFloat(0.0) self.m_light_id = vki.SVInt32(0) self.m_intensity = Spectrum() self.m_cptr = SVCombine_Create( { 't': self.m_t, 'light_id': self.m_light_id, 'intensity': self.m_intensity }, ''' Spectrum Le(in Comb_#hash# self, in vec3 wo, int depth_iter) { Spectrum ret = self.intensity; if (depth_iter>0) { from_rgb(ret, vec3(0.0)); } return ret; } ''')
import VkInline as vki import numpy as np # interface with numpy harr = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype='float32') darr = vki.device_vector_from_numpy(harr) harr2 = np.array([6,7,8,9,10], dtype='int32') darr2 = vki.device_vector_from_numpy(harr2) # test launching non-templated for forLoop = vki.For(['arr_in','arr_out','k'], "inner", ''' void inner(uint idx) { set_value(arr_out, idx, get_value(arr_in, idx)*k); } ''') darr_out = vki.SVVector('float', 5) forLoop.launch(0, 5, [darr, darr_out, vki.SVFloat(10.0)]) print (darr_out.to_host()) darr_out = vki.SVVector('int', 5) forLoop.launch_n(5, [darr2, darr_out, vki.SVInt32(5)]) print (darr_out.to_host())
darr = vki.device_vector_from_numpy(harr) print(darr.to_host()) # GLSL data type print(darr.name_view_type()) harr2 = np.array([6, 7, 8, 9, 10], dtype='int32') darr2 = vki.device_vector_from_numpy(harr2) # kernel with auto parameters, launched twice with different types kernel = vki.Computer(['arr_in', 'arr_out', 'k'], ''' void main() { uint id = gl_GlobalInvocationID.x; if (id >= get_size(arr_in)) return; set_value(arr_out, id, get_value(arr_in, id)*k); } ''') darr_out = vki.SVVector('float', 5) kernel.launch(1, 128, [darr, darr_out, vki.SVFloat(10.0)]) print(darr_out.to_host()) darr_out = vki.SVVector('int', 5) kernel.launch(1, 128, [darr2, darr_out, vki.SVInt32(5)]) print(darr_out.to_host()) # create a vector from python list with GLSL type specified darr3 = vki.device_vector_from_list([3.0, 5.0, 7.0, 9.0, 11.0], 'float') print(darr3.to_host())
if ((tMin <= t1 && t1 < tMax) || (tMin <= t2 && t2 < tMax)) { float t = t1; if (tMin <= t1 && t1 < tMax) { hitpoint = origin + direction * t1; } else { t = t2; hitpoint = origin + direction * t2; } reportIntersectionEXT(t, 0); } } } ''') ]) svwidth = vki.SVInt32(width) svheight = vki.SVInt32(height) raytracer.launch((width, height), [darr_out, svwidth, svheight], [tlas]) out = darr_out.to_host() out = out.reshape((height, width, 3)) * 255.0 out = out.astype(np.uint8) Image.fromarray(out, 'RGB').save('output.png')
void main() { uint x = gl_GlobalInvocationID.x; uint y = gl_GlobalInvocationID.y; if (x >= width || y>=height) return; float u = (float(x)+0.5)/float(width); float v = (float(y)+0.5)/float(height); vec4 rgba = texture(arr_tex2d[0], vec2(u,v)); set_value(arr, x+y*width, rgba); } ''') blockSize = (8, 8) gridSize = (int((width + 7) / 8), int((height + 7) / 8)) kernel.launch( gridSize, blockSize, [vki.SVInt32(width), vki.SVInt32(height), darr], tex2ds=[tex2d]) harr = darr.to_host() harr = np.reshape(harr, (height, width, 4)) * 255.0 harr = harr.astype(np.uint8) img_out = Image.fromarray(harr, 'RGBA') img_out.save('output.png')