def run(self): for ii in range(0, 10): for jj in range(0, 10): r = np.random.random([self.nsample, 3]) r[:, 0] = (r[:, 0] + ii) * 0.1 r[:, 1] = (r[:, 1] + jj) * 0.1 self.X = np.zeros((self.nsample, 4), dtype=np.float32) self.X[:, 0:3] = r self.X[:, 3] = 1. self.I = np.zeros((self.nsample, 4), dtype=np.float32) self.I[:, 0:3] = 1. #self.I[:,3] = 0. cl.enqueue_acquire_gl_objects(self.queue, [self.X_cl, self.I_cl]) cl.enqueue_copy(self.queue, self.X_cl, self.X) cl.enqueue_copy(self.queue, self.I_cl, self.I) self.program.Solve(self.queue, (self.nsample, self.na), None, self.A_cl, self.X_cl, self.I_cl, self.alpha) cl.enqueue_release_gl_objects(self.queue, [self.X_cl, self.I_cl]) self.queue.finish() self.draw() self.scrnData = np.zeros((self.width, self.height), dtype=np.float32) glReadPixels(0, 0, self.width, self.height, GL_ALPHA, GL_FLOAT, self.scrnData) print np.max(self.scrnData) scipy.misc.imsave('render.png', np.flipud(self.scrnData))
def execute(self, subintervals): dt = numpy.float32(self.dt) dx = numpy.float32(self.dx) ntracers = numpy.int32(self.ntracers) num = numpy.int32(self.num) choice = numpy.int32(self.params[0]) # choice) k = numpy.float32(self.params[1]) # k) ymin = numpy.float32(self.params[2]) # ymin) ymax = numpy.float32(self.params[3]) # ymax) """ print "in execute, num", self.num print "tracers", self.ntracers print "choice", choice print "k", k print "ymin", ymin print "ymax", ymax print "dt", dt print "dx", dx """ cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) global_size = (self.num,) local_size = None kernelargs = (self.pos_cl, self.col_cl, self.pos_gen_cl, ntracers, choice, num, k, ymin, ymax, dt, dx) for i in xrange(0, subintervals): self.program.wave(self.queue, global_size, local_size, *(kernelargs)) cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish()
def initialize(): plats = cl.get_platforms() ctx_props = cl.context_properties props = [(ctx_props.PLATFORM, plats[0]), (ctx_props.GL_CONTEXT_KHR, platform.GetCurrentContext())] import sys if sys.platform == "linux2": props.append( (ctx_props.GLX_DISPLAY_KHR, GLX.glXGetCurrentDisplay())) elif sys.platform == "win32": props.append( (ctx_props.WGL_HDC_KHR, WGL.wglGetCurrentDC())) ctx = cl.Context(properties=props) glClearColor(1, 1, 1, 1) glColor(0, 0, 1) vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) rawGlBufferData(GL_ARRAY_BUFFER, n_vertices * 2 * 4, None, GL_STATIC_DRAW) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointer(2, GL_FLOAT, 0, None) coords_dev = cl.GLBuffer(ctx, cl.mem_flags.READ_WRITE, int(vbo)) prog = cl.Program(ctx, src).build() queue = cl.CommandQueue(ctx) cl.enqueue_acquire_gl_objects(queue, [coords_dev]) prog.generate_sin(queue, (n_vertices,), None, coords_dev) cl.enqueue_release_gl_objects(queue, [coords_dev]) queue.finish() glFlush()
def buildCopImageDataTexture(self): if not cl.have_gl(): raise BaseException("No OpenGL interop !!!") if self.node: # bind texture from current compy node cl_image_buffer = self.node.getCookedData() glBindTexture(GL_TEXTURE_2D, self.node_gl_tex_id) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, self.node.xRes(), self.node.yRes(), 0, GL_RGB, GL_FLOAT, None) logger.debug("Node size to display: %s %s" % (self.node.xRes(), self.node.yRes())) node_gl_texture = cl.GLTexture(hou.openclContext(), cl.mem_flags.WRITE_ONLY, GL_TEXTURE_2D, 0, self.node_gl_tex_id, 2) # Aquire OpenGL texture object cl.enqueue_acquire_gl_objects(hou.openclQueue(), [node_gl_texture]) # copy OpenCL buffer to OpenGl texture cl.enqueue_copy_image(hou.openclQueue(), cl_image_buffer, node_gl_texture, (0,0), (0,0), (self.node.xRes(), self.node.yRes()), wait_for=None) # Release OpenGL texturte object cl.enqueue_release_gl_objects(hou.openclQueue(), [node_gl_texture]) hou.openclQueue().finish() glGenerateMipmap(GL_TEXTURE_2D) #Generate mipmaps now!!! glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glBindTexture(GL_TEXTURE_2D, 0)
def run(self): for ii in range(0,10): for jj in range(0,10): r = np.random.random([self.nsample,3]) r[:,0]=(r[:,0]+ii)*0.1 r[:,1]=(r[:,1]+jj)*0.1 self.X = np.zeros((self.nsample,4), dtype = np.float32) self.X[:,0:3] = r self.X[:,3] = 1. self.I = np.zeros((self.nsample,4), dtype = np.float32) self.I[:,0:3] = 1. #self.I[:,3] = 0. cl.enqueue_acquire_gl_objects(self.queue, [self.X_cl,self.I_cl]) cl.enqueue_copy(self.queue, self.X_cl, self.X) cl.enqueue_copy(self.queue, self.I_cl, self.I) self.program.Solve(self.queue, (self.nsample, self.na), None, self.A_cl, self.X_cl, self.I_cl, self.alpha) cl.enqueue_release_gl_objects(self.queue, [self.X_cl,self.I_cl]) self.queue.finish() self.draw() self.scrnData = np.zeros((self.width,self.height), dtype = np.float32) glReadPixels(0, 0, self.width, self.height, GL_ALPHA, GL_FLOAT, self.scrnData) print np.max(self.scrnData) scipy.misc.imsave('render.png', np.flipud(self.scrnData))
def _generate_bodies(self): r1 = 0.5 r2 = 2.5 sigma = 0.25 mu = (r1 + r2) / 2 eccentricity = 0.0 cl.enqueue_acquire_gl_objects(cl_queue, [ self.body_positions_cl_buffer, self.body_velocities_cl_buffer, self.body_colors_cl_buffer ]) kernel_init( cl_queue, (self.body_count, ), None, self.body_positions_cl_buffer, self.body_velocities_cl_buffer, self.body_colors_cl_buffer, np.uint(self.body_count), np.array( [self.position.x, self.position.y, self.position.z, self.mass], dtype=np.float32), np.array([self.velocity.x, self.velocity.y, self.velocity.z, 0], dtype=np.float32), np.array([ self.rotation.x, self.rotation.y, self.rotation.z, self.rotation.w ], dtype=np.float32), np.float32(Universe.G), np.float32(Universe.dt), np.float32(mu), np.float32(sigma), np.float32(eccentricity)) cl.enqueue_release_gl_objects(cl_queue, [ self.body_positions_cl_buffer, self.body_velocities_cl_buffer, self.body_colors_cl_buffer ]) cl_queue.finish()
def step(self, step_size): if len(self.network_data.gl_objects) > 0: cl.enqueue_acquire_gl_objects(self.queue, self.network_data.gl_objects) global_size = (len(self.units),) kernel_args = [ self.network_data.unit_state_index_buf, self.network_data.unit_param_index_buf, self.network_data.state_buf, self.network_data.params_buf, self.network_data.unit_weight_index_buf, self.network_data.conn_index_buf, self.network_data.num_connections_buf, self.network_data.weights_buf, self.network_data.next_state_buf, np.float32(step_size), self.network_data.color_buf, ] self.program.unit_step(self.queue, global_size, None, *kernel_args) self.time += step_size self.network_data.update_state(self.cl_context, self.queue, self.time) if len(self.network_data.gl_objects) > 0: cl.enqueue_release_gl_objects(self.queue, self.network_data.gl_objects) return self.network_data.state
def execute(self, newp, t): self.count += 1 if self.count >= self.ntracers-1: self.count = 0 cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) global_size = (self.ntracers,) local_size = None ca_kernelargs = (self.pos_cl, self.col_cl, self.time_cl, self.props_cl, np.int32(self.count), np.float32(t), newp, self.params_cl ) #print len(self.params) self.prgs["cartist"].cartist(self.queue, global_size, local_size, *(ca_kernelargs)) cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish()
def __call__(self, line_x: float, line_y: float, line_angle: float, filter_radius: float, filter_noise: float, filter_samples: float, image_angle: float, result: np.array = None) -> np.array: shape = self._result_image.shape if hasattr(self._result_image, 'gl_object'): cl.enqueue_acquire_gl_objects(command_queue, [self._result_image]) self._program.filtered_line(command_queue, shape, (1, 1), np.uint32(shape[1]), np.uint32(shape[0]), np.float32(line_x), np.float32(line_y), np.float32(line_angle), np.float32(max(1.0, filter_radius)), np.float32(filter_noise), np.float32(filter_samples), np.float32(image_angle), self._result_image) if hasattr(self._result_image, 'gl_object'): cl.enqueue_release_gl_objects(command_queue, [self._result_image]) if result is not None: cl.enqueue_copy(command_queue, result, self._result_image, origin=(0, 0), region=shape) return result
def execute(self, sub_intervals): cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) global_size = (self.num, ) local_size_threads = 33 # group size for i in range(1, 64): # choose group size if (self.num % i == 0): local_size_threads = i local_size = (local_size_threads, ) # pos_shared = cl.LocalMemory(4 * local_size_threads) # col_shared = cl.LocalMemory(4 * local_size_threads) kernelargs = (self.pos_cl, self.vel_cl, self.pos_gen_cl, self.vel_gen_cl, self.col_cl, self.dt, self.num_cl) kernelargsT = (self.pos_gen_cl, self.vel_gen_cl, self.pos_cl, self.vel_cl, self.col_cl, self.dt, self.num_cl) for i in xrange(0, sub_intervals): self.program.nbody(self.queue, global_size, local_size, *(kernelargs)) self.program.nbody( self.queue, global_size, local_size, *(kernelargsT )) # change role of kernelargs to do double buffered calc cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish() self.totaltime += 2 * self.dt sys.stdout.write("\rT = {0} fm/c>".format(self.totaltime)) sys.stdout.flush()
def initialize(): plats = cl.get_platforms() ctx_props = cl.context_properties props = [(ctx_props.PLATFORM, plats[0]), (ctx_props.GL_CONTEXT_KHR, platform.GetCurrentContext())] import sys if sys.platform == "linux2": props.append((ctx_props.GLX_DISPLAY_KHR, GLX.glXGetCurrentDisplay())) elif sys.platform == "win32": props.append((ctx_props.WGL_HDC_KHR, WGL.wglGetCurrentDC())) ctx = cl.Context(properties=props) glClearColor(1, 1, 1, 1) glColor(0, 0, 1) vbo = glGenBuffers(1) glBindBuffer(GL_ARRAY_BUFFER, vbo) rawGlBufferData(GL_ARRAY_BUFFER, n_vertices * 2 * 4, None, GL_STATIC_DRAW) glEnableClientState(GL_VERTEX_ARRAY) glVertexPointer(2, GL_FLOAT, 0, None) coords_dev = cl.GLBuffer(ctx, cl.mem_flags.READ_WRITE, int(vbo)) prog = cl.Program(ctx, src).build() queue = cl.CommandQueue(ctx) cl.enqueue_acquire_gl_objects(queue, [coords_dev]) prog.generate_sin(queue, (n_vertices, ), None, coords_dev) cl.enqueue_release_gl_objects(queue, [coords_dev]) queue.finish() glFlush()
def execute(self, sub_intervals): cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) global_size = (self.num,) local_size = None # set up the Kernel argument list w = numpy.int32(640) h = numpy.int32(480) kernelargs = (self.pos_cl, self.col_cl, self.depth_cl, self.rgb_cl, self.pt_cl, self.ipt_cl, w, h) for i in xrange(0, sub_intervals): self.program.project(self.queue, global_size, local_size, *(kernelargs)) #pos = numpy.ndarray((self.imsize*4, 1), dtype=numpy.float32) #cl.enqueue_read_buffer(self.queue, self.pos_cl, pos).wait() #for i in xrange(0, 100, 4): # print pos[i], pos[i+1], pos[i+2], pos[i+3] cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish()
def buildCopImageDataTexture(self): if not self.node: return image_width, image_height = self.node.xRes(), self.node.yRes() logger.debug("Node size to display: %s %s" % (image_width, image_height)) cl_image = self.node.getCookedData() cl_ctx = hou.OpenCL.context() cl_queue = hou.OpenCL.queue() event = None glBindTexture(GL_TEXTURE_2D, self.node_gl_tex_id) if cl.have_gl(): glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, image_width, image_height, 0, GL_RGB, GL_FLOAT, None) node_gl_texture = cl.GLTexture(cl_ctx, cl.mem_flags.WRITE_ONLY, GL_TEXTURE_2D, 0, self.node_gl_tex_id, 2) # Aquire OpenGL texture object cl.enqueue_acquire_gl_objects(cl_queue, [node_gl_texture]) # copy OpenCL buffer to OpenGl texture cl.enqueue_copy_image(cl_queue, cl_image, node_gl_texture, (0, 0), (0, 0), (image_width, image_height), wait_for=(event, )) # Release OpenGL texturte object cl.enqueue_release_gl_objects(cl_queue, [node_gl_texture]) hou.openclQueue().finish() else: mapped_buff = cl.enqueue_map_image(cl_queue, cl_image, cl.map_flags.READ, (0, 0), (image_width, image_height), (image_height, image_width, 4), np.float32, 'C') texture_data = mapped_buff[0].copy() mapped_buff[0].base.release(cl_queue) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, image_width, image_height, 0, GL_RGBA, GL_FLOAT, texture_data) glGenerateMipmap(GL_TEXTURE_2D) #Generate mipmaps now!!! glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glBindTexture(GL_TEXTURE_2D, 0) self.rebuild_node_image = False
def post_run(self): if (len(self.post_run_functions) != 0): if (self.interop == True): cl.enqueue_acquire_gl_objects(self.ocl_d.queue, [self.textureMem]) for func in self.post_run_functions: func() if (self.interop == True): cl.enqueue_release_gl_objects(self.ocl_d.queue, [self.textureMem])
def step(self): """ Advance simulation. """ prg = self.prg queue = self.queue if self.gl_interop: cl.enqueue_acquire_gl_objects(queue, self.cl_gl_objects) ## one simulation step consists of four steps: ## step 1) assign particles to cells in the uniform grid ## step 2) compute densities ## step 3) compute accelerations from forces ## step 4) move particles according to accelerations. ## for now, simple collision detection is done in the last step. # step 1) if self.use_grid: self.assign_cells() global_size = self.global_size local_size = self.local_size # if we use a grid based neighbour search, we need to pass # some additional arguments. grid_args = [self.grid_index_cl, self.cell_start_cl, self.cell_end_cl] if self.use_grid else [] # step 2) prg.stepDensity(queue, global_size, local_size, self.position_sorted_cl if self.use_grid and self.reorder else self.position_cl, self.density_cl, self.pressure_cl, self.params_cl, *grid_args).wait() # step 3) prg.stepForces(queue, global_size, local_size, self.position_sorted_cl if self.use_grid and self.reorder else self.position_cl, self.velocity_sorted_cl if self.use_grid and self.reorder else self.velocity_cl, self.acceleration_cl, self.density_cl, self.pressure_cl, self.params_cl, *grid_args).wait() # step 4) prg.stepMove(queue, global_size, local_size, self.position_cl, self.velocity_cl, self.acceleration_cl, self.params_cl).wait() if self.gl_interop: cl.enqueue_release_gl_objects(queue, self.cl_gl_objects)
def render_advanced(self): """ Render particles as a fluid surface (Simon Green's screen space rendering). """ self._create_thickness_map() # render depth to depth target with self.depth_target: self.render_point_sprites(self.depth_shader) # smooth depth texture if self.smooth_depth: cl.enqueue_acquire_gl_objects(self.queue, self.cl_gl_objects) local_size = self.cl_local_size for i in range(self.smoothing_iterations): # alternate between writing to depth2_target and depth1_target # (can't read from and write to the same texture at the same time). args = (np.float32(self.smoothing_dt), np.float32(self.smoothing_z_contrib),) self.prg.curvatureFlow(self.queue, self.window_size, local_size, self.depth_cl, self.depth2_cl, *args).wait() self.prg.curvatureFlow(self.queue, self.window_size, local_size, self.depth2_cl, self.depth_cl, *args).wait() cl.enqueue_release_gl_objects(self.queue, self.cl_gl_objects) if self.render_mean_curvature: cl.enqueue_acquire_gl_objects(self.queue, self.cl_gl_objects) self.prg.test(self.queue, self.window_size, self.cl_local_size, self.depth_cl, self.test_cl).wait() cl.enqueue_release_gl_objects(self.queue, self.cl_gl_objects) with self.tex_shader: self.render_texture(self.test_target.texture) return # # testing # cl.enqueue_acquire_gl_objects(self.queue, [self.depth_cl, self.test_cl]) # self.prg.test(self.queue, self.depth_target.size, self.cl_local_size, self.depth_cl, self.test_cl).wait() # with self.tex_shader: # self.render_texture(self.test_target.texture) # #self.render_texture(self.depth_target.texture) # cl.enqueue_release_gl_objects(self.queue, [self.depth_cl, self.test_cl]) # return # use alpha blending glEnable (GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); # bind thickness texture glActiveTexture(GL_TEXTURE0+1) glBindTexture(GL_TEXTURE_2D, self.thickness_target.texture) with self.final_shader: self.render_texture(self.depth_target.texture) glActiveTexture(GL_TEXTURE0) glDisable(GL_BLEND)
def execute(self, a=1.0): if self.use_gl_texture_as_tmp: cl.enqueue_acquire_gl_objects(self.queue, [self.cl_image, self.cl_lapl_mask, self.cl_tmp]) else: cl.enqueue_acquire_gl_objects(self.queue, [self.cl_image, self.cl_lapl_mask]) self.program.laplace(self.queue, self.shape, None, self.cl_tmp, self.cl_image, self.cl_lapl_mask, float32(a)) self.program.laplace(self.queue, self.shape, None, self.cl_image, self.cl_tmp, self.cl_lapl_mask, float32(a)) if self.use_gl_texture_as_tmp: cl.enqueue_release_gl_objects(self.queue, [self.cl_image, self.cl_lapl_mask, self.cl_tmp]) else: cl.enqueue_release_gl_objects(self.queue, [self.cl_image, self.cl_lapl_mask])
def execute(self): """Execute the OpenCL kernel. """ # Beginning of execute loop start_time = time.time() # get secure access to GL-CL interop objects cl.enqueue_acquire_gl_objects(self.queue, [self.glclbuf, self.colclbuf]) # Execute the kernel for the duration of the timer cycle while time.time() - start_time < 40.0e-3: if self.current_iter == 0: for i in range(self.time_points): self.update_m_of_t( self.queue, (self.current_steps, self.duration_steps), (1, 1), self.m.data, self.glclbuf, self.colclbuf, self.time_points, self.realizations, i % self.time_points).wait() self.current_time += realdt self.current_iter += 1 # Generate random numbers for the stochastic process if temperature > 0: self.rg.fill_normal(self.dW) # Run a single LLG evolution step self.evolve( self.queue, (self.current_steps * self.realizations, self.duration_steps), (self.realizations, 1), self.m.data, self.dW.data, self.currents, self.durations, self.current_time).wait() # Periodic Normalizations if (self.current_iter % (normalize_interval) == 0): self.normalize_m(self.queue, (self.current_steps * self.realizations, self.duration_steps), (self.realizations, 1), self.m.data).wait() # Push data to the gl buffer if (self.current_iter % (display_interval) == 0): self.update_m_of_t( self.queue, (self.current_steps, self.duration_steps), (1, 1), self.m.data, self.glclbuf, self.colclbuf, self.time_points, self.realizations, self.current_timepoint % self.time_points).wait() self.current_timepoint += 1 # release access to the GL-CL interop objects cl.enqueue_release_gl_objects(self.queue, [self.glclbuf, self.colclbuf]) self.queue.finish() gl.glFlush()
def execute(self): """Execute the OpenCL kernel. """ # get secure access to GL-CL interop objects cl.enqueue_acquire_gl_objects(self.queue, [self.glclbuf]) # arguments to the OpenCL kernel kernelargs = (self.glclbuf, ) # execute the kernel self.program.clkernel(self.queue, (self.count, ), None, *kernelargs) # release access to the GL-CL interop objects cl.enqueue_release_gl_objects(self.queue, [self.glclbuf]) self.queue.finish()
def execute(self): """Execute the OpenCL kernel. """ # get secure access to GL-CL interop objects cl.enqueue_acquire_gl_objects(self.queue, [self.glclbuf]) # arguments to the OpenCL kernel kernelargs = (self.clbuf, self.glclbuf) # execute the kernel self.program.clkernel(self.queue, (self.count,), None, *kernelargs) # release access to the GL-CL interop objects cl.enqueue_release_gl_objects(self.queue, [self.glclbuf]) self.queue.finish()
def on_display(): """Render the particles""" # Update or particle positions by calling the OpenCL kernel cl.enqueue_acquire_gl_objects(queue, [cl_gl_position, cl_gl_color]) kernelargs = ( cl_gl_position, cl_gl_color, cl_velocity, cl_start_position, cl_start_velocity, numpy.float32(time_step), ) program.particle_fountain(queue, (num_particles, ), None, *(kernelargs)) cl.enqueue_release_gl_objects(queue, [cl_gl_position, cl_gl_color]) queue.finish() glFlush() glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # Handle mouse transformations glTranslatef(initial_translate["x"], initial_translate["y"], initial_translate["z"]) glRotatef(rotate["x"], 1, 0, 0) glRotatef(rotate["y"], 0, 1, 0) # we switched around the axis so make this rotate_z glTranslatef(translate["x"], translate["y"], translate["z"]) # Render the particles glEnable(GL_POINT_SMOOTH) glPointSize(2) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) # Set up the VBOs gl_color.bind() glColorPointer(4, GL_FLOAT, 0, gl_color) gl_position.bind() glVertexPointer(4, GL_FLOAT, 0, gl_position) glEnableClientState(GL_VERTEX_ARRAY) glEnableClientState(GL_COLOR_ARRAY) # Draw the VBOs glDrawArrays(GL_POINTS, 0, num_particles) glDisableClientState(GL_COLOR_ARRAY) glDisableClientState(GL_VERTEX_ARRAY) glDisable(GL_BLEND) glutSwapBuffers()
def render(self): self.sample += 1 front_buf = self.buf[self.front] back_buf = self.buf[1 - self.front] self.front = 1 - self.front prog = self.cache.get('kernels/test.cl') cl.enqueue_acquire_gl_objects(self.queue, self.buf) global_size = (self.width, self.height) args = (self.cam_xform, np.int32(self.sample), self.env, back_buf, front_buf) prog.test(self.queue, global_size, None, *args) cl.enqueue_release_gl_objects(self.queue, self.buf) self.queue.finish() return front_buf.gl_object
def execute(self, sub_intervals): cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) global_size = (self.num,) local_size = None kernelargs = (self.pos_cl, self.col_cl, self.vel_cl, self.pos_gen_cl, self.vel_gen_cl, self.dt) for i in xrange(0, sub_intervals): self.program.part2(self.queue, global_size, local_size, *(kernelargs)) cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish()
def on_draw(self, event): gloo.clear(color=True, depth=True) if not self.sim_is_initialized: self.texture.set_data(I) self.program.draw('triangle_strip') self.update() self.initialize_sim() else: cl.enqueue_acquire_gl_objects(self.sim.queue, [self.sim.v]) self.sim.run(1) cl.enqueue_release_gl_objects(self.sim.queue, [self.sim.v]) self.texture.set_data(I) self.program.draw('triangle_strip')
def change_display(image) : image_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=image) mem = cl.GLBuffer(ctx, mf.WRITE_ONLY, numpy.float32(buf)) cl.enqueue_acquire_gl_objects(queue, [mem]) add_knl = prog.add add_knl.set_args(image_buf, mem) cl.enqueue_nd_range_kernel(queue, add_knl, image.shape, None) cl.enqueue_release_gl_objects(queue, [mem]) queue.finish() glFlush()
def execute(self, sub_intervals): cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) global_size = (self.num, ) local_size = None kernelargs = (self.pos_cl, self.col_cl, self.vel_cl, self.pos_gen_cl, self.vel_gen_cl, self.dt) for i in xrange(0, sub_intervals): self.program.part2(self.queue, global_size, local_size, *(kernelargs)) cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish()
def toggle_color_profile(self): self.color_profile_id = (self.color_profile_id + 1) % len(COLOR_PROFILES) print( Style.BRIGHT + Fore.GREEN + 'Color Profile: \t' + Fore.RESET + Style.RESET_ALL, COLOR_PROFILES[self.color_profile_id]) # Run __kernel change_color() cl.enqueue_acquire_gl_objects(self.queue, self.gl_buffers) self.kernel.change_color(self.queue, (self.n_particles, ), None, self.color_buffer, self.seed_buffer, np.int32(self.color_profile_id)) cl.enqueue_release_gl_objects(self.queue, self.gl_buffers) self.queue.finish() glFlush()
def euler(self): cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) kernelargs = (self.pos_cl, self.col_cl, self.vel_cl, self.dt, self.dim) self.programs["euler"].euler(self.queue, self.global_size, self.local_size, *(kernelargs)) cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish()
def step(self, delta_time): if pause: return centers = np.ndarray((self.galaxy_count, 4), dtype=np.float32) for i in range(self.galaxy_count): centers[i][:3] = self.galaxies[i].position centers[i][3] = self.galaxies[i].mass centers_buffer = clarray.to_device(cl_queue, centers) gl.glFlush() gl.glFinish() for i, galaxy in enumerate(self.galaxies): cl.enqueue_acquire_gl_objects(cl_queue, [ galaxy.body_positions_cl_buffer, galaxy.body_velocities_cl_buffer ]) kernel_step(cl_queue, (galaxy.body_count, ), None, galaxy.body_positions_cl_buffer, galaxy.body_velocities_cl_buffer, centers_buffer.data, np.uint(galaxy.body_count), np.uint(self.galaxy_count), np.float32(self.dt * delta_time), np.float32(self.G)) cl.enqueue_release_gl_objects(cl_queue, [ galaxy.body_positions_cl_buffer, galaxy.body_velocities_cl_buffer ]) cl_queue.finish() centers = [ mathutils.Vector((galaxy.position.x, galaxy.position.y, galaxy.position.z, galaxy.mass)) for galaxy in self.galaxies ] for i in range(self.galaxy_count): this_galaxy = self.galaxies[i] f = mathutils.Vector((0, 0, 0)) #f = np.zeros((4,), dtype=np.float32) for j in self.others(i): delta_pos = mathutils.Vector(centers[j] - centers[i]).xyz length = max(1.0, delta_pos.length_squared) f += delta_pos.normalized() * self.G * centers[i][3] * centers[ j][3] / delta_pos.length_squared this_galaxy.velocity += f * delta_time * self.dt this_galaxy.position += this_galaxy.velocity * delta_time * self.dt
def execute(self, a=1.0): if self.use_gl_texture_as_tmp: cl.enqueue_acquire_gl_objects( self.queue, [self.cl_image, self.cl_lapl_mask, self.cl_tmp]) else: cl.enqueue_acquire_gl_objects(self.queue, [self.cl_image, self.cl_lapl_mask]) self.program.laplace(self.queue, self.shape, None, self.cl_tmp, self.cl_image, self.cl_lapl_mask, float32(a)) self.program.laplace(self.queue, self.shape, None, self.cl_image, self.cl_tmp, self.cl_lapl_mask, float32(a)) if self.use_gl_texture_as_tmp: cl.enqueue_release_gl_objects( self.queue, [self.cl_image, self.cl_lapl_mask, self.cl_tmp]) else: cl.enqueue_release_gl_objects(self.queue, [self.cl_image, self.cl_lapl_mask])
def execute(self): if self.PreExecute: self.PreExecute() if self.gl_objects: cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) #hack for now eval("self.program.%s(self.queue, self.global_size, self.local_size, *(self.kernelargs))" % self.kernelname) if self.gl_objects: cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish() if self.PostExecute: self.PostExecute()
def execute(self, subintervals): dt = numpy.float32(self.dt) dx = numpy.float32(self.dx) ntracers = numpy.int32(self.ntracers) num = numpy.int32(self.num) choice = numpy.int32(self.params[0])#choice) k = numpy.float32(self.params[1])#k) ymin = numpy.float32(self.params[2])#ymin) ymax = numpy.float32(self.params[3])#ymax) """ print "in execute, num", self.num print "tracers", self.ntracers print "choice", choice print "k", k print "ymin", ymin print "ymax", ymax print "dt", dt print "dx", dx """ cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) global_size = (self.num,) local_size = None kernelargs = (self.pos_cl, self.col_cl, self.pos_gen_cl, ntracers, choice, num, k, ymin, ymax, dt, dx ) for i in xrange(0, subintervals): self.program.wave(self.queue, global_size, local_size, *(kernelargs)) cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish()
def on_display(): cl.enqueue_acquire_gl_objects(queue, [cl_gl_buffer]) program.generate_sin(queue, (num_points,), None, cl_gl_buffer) cl.enqueue_release_gl_objects(queue, [cl_gl_buffer]) queue.finish() glFlush() glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # pos_vbo.bind() # glVertexPointer(4, GL_FLOAT, 0, pos_vbo) # glEnableClientState(GL_VERTEX_ARRAY) glDrawArrays(GL_POINTS, 0, num_points) # glDisableClientState(GL_VERTEX_ARRAY) glutSwapBuffers()
def execute(self): if self.PreExecute: self.PreExecute() if self.gl_objects: cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) #self.program.part2(self.queue, self.pos.shape, None, # *(self.kernelargs)) if self.gl_objects: cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish() glFlush() if self.PostExecute: self.PostExecute()
def new_image(self, img): if self.ocl_prg is None: self.init_openCL() self.ocl_ary.set(img) pyopencl.enqueue_acquire_gl_objects(self.queue, [self.ocl_tex]) self.ocl_prg.u16_to_float(self.queue, (self.tex_size,self.tex_size), (8,8), self.ocl_ary.data, self.ary_float.data, numpy.int32(self.tex_size), numpy.int32(self.tex_size)) self.ocl_prg.buf_to_tex(self.queue, (self.tex_size,self.tex_size), (8,8), self.ocl_ary.data, numpy.int32(self.tex_size), numpy.int32(self.tex_size), numpy.float32(0.0), numpy.float32(65000.0), self.ocl_tex) pyopencl.enqueue_release_gl_objects(self.queue, [self.ocl_tex]).wait() t = time.time() print("pyopencl program called fps= %.3fs"%(1.0/(t-self.last_start))) self.last_start = t self.on_paint(None)
def execute(self, sub_intervals): cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) mf = cl.mem_flags global_size = (self.num, ) local_size = None #(2**9,) kernelargs = ( self.pos_cl, self.vel_cl, #cl.LocalMemory(1024*3*4), #cl.Buffer(self.ctx, mf.READ_ONLY, size=self.vel_cl.size), self.dt) for i in xrange(0, sub_intervals): self.program.part2(self.queue, global_size, local_size, *(kernelargs)) cl.enqueue_release_gl_objects(self.queue, self.gl_objects)
def test_clgl_texture_interop(gl_context, cl_context): """Tests that an OpenGL texture can be used in an OpenCL kernel.""" from scipy.misc import lena img = np.dstack([lena() / 256.] * 3).astype(np.float32) hei, wid = img.shape[:2] gl_img = Texture2D(img, mipmap=True, context=gl_context) cl_img = cl.GLTexture(cl_context, cl.mem_flags.READ_ONLY, gl.GL_TEXTURE_2D, 1, gl_img._id, 2) cl_queue = cl.CommandQueue(cl_context) cl_program = cl.Program(cl_context, cl_source).build() if True: # usable in loop cl_gl_data = [cl_img] cl.enqueue_acquire_gl_objects(cl_queue, cl_gl_data) cl_args = [np.uint32(wid), np.uint32(hei), cl_img] assert 3 == len(cl_args) cl_program.run(cl_queue, (wid, hei), None, *cl_args) cl.enqueue_release_gl_objects(cl_queue, cl_gl_data) cl_queue.flush() cl_queue.finish()
def execute(self, newp, t): self.count += 1 if self.count >= self.ntracers - 1: self.count = 0 cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) global_size = (self.ntracers, ) local_size = None ca_kernelargs = (self.pos_cl, self.col_cl, self.time_cl, self.props_cl, np.int32(self.count), np.float32(t), newp, self.params_cl) #print len(self.params) self.prgs["cartist"].cartist(self.queue, global_size, local_size, *(ca_kernelargs)) cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish()
def rules(self): cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) kernelargs = (self.pos_cl, self.col_cl, self.vel_cl, self.acc_cl, self.steer_cl, self.avg_pos_cl, self.avg_vel_cl, self.dt, self.dim) self.programs["boids"].rules1(self.queue, self.global_size, self.local_size, *(kernelargs)) self.programs["boids"].rules2(self.queue, self.global_size, self.local_size, *(kernelargs)) cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish()
def execute(self): glFinish() cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) global_size = self.tex_dim local_size = None world = self.world kernelargs = (self.tex, numpy.float32(world.camera.rot_x), numpy.float32(world.camera.rot_y), numpy.float32(world.camera.x), numpy.float32(world.camera.y), numpy.float32(world.camera.z), numpy.float32(world.camera.fov_x()), numpy.float32(world.camera.fov_y()), world.mapdat_clbuf) self.program.raytrace(self.queue, global_size, local_size, *kernelargs) cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.flush() self.queue.finish()
def execute(self, sub_intervals): # First, we have to make sure that OpenGL is done using the buffer objects: cl.enqueue_acquire_gl_objects(self.queue, [self.cl_positions, self.cl_colors]) # Now, we can safely call the kernel. Its arguments are buffer objects: args = (self.cl_positions, self.cl_colors, self.cl_velocities, self.cl_initial_positions, self.cl_initial_velocities, self.dt) # The kernel will be executed several times with a small step size. # This increases the accuracy with respect to a single step with a # large step size. However, it is not necessary to display all the # intermediate results. for i in range(0, sub_intervals): # In each step, the <code>animate</code> kernel function is called. # Its arguments are the queue object that schedules its execution, # the global and local block sizes, and any arguments that will be # passed to the actual kernel. self.program.animate(self.queue, [len(self.gl_positions)], None, *args) # Finally, we allow OpenGL to access the buffers again: cl.enqueue_release_gl_objects(self.queue, [self.cl_positions, self.cl_colors])
def execute(self): if self.PreExecute: self.PreExecute() if self.gl_objects: cl.enqueue_acquire_gl_objects(self.queue, self.gl_objects) #really we should use a python trick to get the name of the kernel function to call #self.program.calibrate(self.queue, self.pos.shape, None, # *(self.kernelargs)) #here we assume we only have one kernel in the program and we call that one... self.program.all_kernels()[0](self.queue, self.pos.shape, None, *(self.kernelargs)) if self.gl_objects: cl.enqueue_release_gl_objects(self.queue, self.gl_objects) self.queue.finish() glFlush() if self.PostExecute: self.PostExecute()