Esempio n. 1
0
    def __init__(self, lattice, include_materials=True):
        self.lattice = lattice
        self.include_materials = include_materials
        self.gl_texture_buffer = numpy.ndarray(
            shape=(self.lattice.memory.volume, 4),
            dtype=self.lattice.memory.float_type)
        self.gl_texture_buffer[:, :] = 0.0

        self.gl_moments = glGenTextures(1)
        self.gl_texture_type = {
            2: GL_TEXTURE_2D,
            3: GL_TEXTURE_3D
        }.get(self.lattice.descriptor.d)
        glBindTexture(self.gl_texture_type, self.gl_moments)

        if self.gl_texture_type == GL_TEXTURE_3D:
            glTexImage3D(self.gl_texture_type, 0, GL_RGBA32F,
                         self.lattice.memory.size_x,
                         self.lattice.memory.size_y,
                         self.lattice.memory.size_z, 0, GL_RGBA, GL_FLOAT,
                         self.gl_texture_buffer)
            glTexParameteri(self.gl_texture_type, GL_TEXTURE_MIN_FILTER,
                            GL_NEAREST)
            glTexParameteri(self.gl_texture_type, GL_TEXTURE_MAG_FILTER,
                            GL_LINEAR)
            glTexParameteri(self.gl_texture_type, GL_TEXTURE_WRAP_T,
                            GL_CLAMP_TO_EDGE)
            glTexParameteri(self.gl_texture_type, GL_TEXTURE_WRAP_S,
                            GL_CLAMP_TO_EDGE)
            glTexParameteri(self.gl_texture_type, GL_TEXTURE_WRAP_R,
                            GL_CLAMP_TO_EDGE)
            self.cl_gl_moments = cl.GLTexture(self.lattice.context,
                                              mf.READ_WRITE,
                                              self.gl_texture_type, 0,
                                              self.gl_moments, 3)
        elif self.gl_texture_type == GL_TEXTURE_2D:
            glTexImage2D(self.gl_texture_type, 0, GL_RGBA32F,
                         self.lattice.memory.size_x,
                         self.lattice.memory.size_y, 0, GL_RGBA, GL_FLOAT,
                         self.gl_texture_buffer)
            glTexParameteri(self.gl_texture_type, GL_TEXTURE_MIN_FILTER,
                            GL_NEAREST)
            glTexParameteri(self.gl_texture_type, GL_TEXTURE_MAG_FILTER,
                            GL_NEAREST)
            glTexParameteri(self.gl_texture_type, GL_TEXTURE_WRAP_T,
                            GL_CLAMP_TO_EDGE)
            glTexParameteri(self.gl_texture_type, GL_TEXTURE_WRAP_S,
                            GL_CLAMP_TO_EDGE)
            self.cl_gl_moments = cl.GLTexture(self.lattice.context,
                                              mf.READ_WRITE,
                                              self.gl_texture_type, 0,
                                              self.gl_moments, 2)

        self.build_kernel()
    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)
Esempio n. 3
0
    def _init_OGL(self):
        # Create an openGL texture
        glEnable(GL_TEXTURE_2D)
        glGenerateMipmap(GL_TEXTURE_2D)
        textureID = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, textureID)
        # Bind it
        # Create a texture array and send it to opengl
        m_texture = np.zeros((self.nx, self.ny, 4),
                             dtype=np.float32)  # An nx*ny*4 array

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)
        # 1D texture
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)
        # 2D texture

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, self.nx, self.ny, 0, GL_RGBA,
                     GL_UNSIGNED_BYTE, m_texture)

        # Setting the projection
        glMatrixMode(GL_PROJECTION)
        glOrtho(0, self.win_width, 0, self.win_height, -1, 1)
        glMatrixMode(GL_MODELVIEW)
        # Set the opengl blend option
        glClearColor(0.01, 0.01, 0.01, 1.)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glEnable(GL_BLEND)
        # Create OpenCL memory object from gl texture
        self.textureMem = cl.GLTexture(self.lbm_ocl.ocl_d.ctx,
                                       cl.mem_flags.READ_WRITE, GL_TEXTURE_2D,
                                       0, textureID, 2)
Esempio n. 4
0
    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
Esempio n. 5
0
    def cl_init(self):
        self.cl_init_context()

        cur_dir = os.path.dirname(os.path.abspath(__file__))
        with open('%s/curvature_flow.cl' % cur_dir) as f:
            from mako.template import Template
            code = str(
                Template(f.read()).render(
                    window_size=self.window_size,
                    # smoothing timestep
                    dt=self.smoothing_dt,
                    projection_matrix=self.projection_matrix))
            self.prg = cl.Program(self.ctx, code).build()

        self.depth_cl = cl.GLTexture(self.ctx, cl.mem_flags.READ_WRITE,
                                     GL_TEXTURE_2D, 0,
                                     self.depth_target.texture, 2)
        self.depth2_cl = cl.GLTexture(self.ctx, cl.mem_flags.READ_WRITE,
                                      GL_TEXTURE_2D, 0,
                                      self.depth2_target.texture, 2)
        self.test_cl = cl.GLTexture(self.ctx, cl.mem_flags.READ_WRITE,
                                    GL_TEXTURE_2D, 0, self.test_target.texture,
                                    2)
        self.cl_gl_objects = [self.depth_cl, self.depth2_cl, self.test_cl]

        # local work size: (lw, lh) where lw (lh) must divide window width (height).
        lw, lh = 16, 16
        local_size_limit = min(device.max_work_group_size
                               for device in self.ctx.devices)
        if lw * lh > local_size_limit:
            import sys
            sys.stderr.write(
                'Warning: work group size too large, try reducing it. Until then, we are letting OpenCL implementation can pick something.\n'
            )
            self.cl_local_size = None
        else:
            assert self.window_size[0] % lw == 0 and self.window_size[
                1] % lh == 0, "Window width (height) must be divisible by %i (%i)" % (
                    lw, lh)
            self.cl_local_size = (lw, lh)
Esempio n. 6
0
    def __init__(self, texture, tex_dim, world):
        self.tex_dim = tex_dim
        self.world = world

        self.clinit()
        self.loadProgram("raytrace.cl")

        world.init_cldata(self.ctx)

        self.tex = cl.GLTexture(self.ctx, cl.mem_flags.READ_WRITE,
                                GL_TEXTURE_2D, 0, texture, 2)

        self.gl_objects = [self.tex]
Esempio n. 7
0
    def __init__(self, gl_image, gl_lapl_mask):
        platform = cl.get_platforms()[0]
        self.ctx = cl.Context(
            properties=[(cl.context_properties.PLATFORM, platform)] +
            get_gl_sharing_context_properties(),
            devices=None)
        self.queue = cl.CommandQueue(self.ctx)
        self.program = cl.Program(self.ctx, kernel_code).build()

        self.shape = gl_image.shape[1], gl_image.shape[0]
        self.cl_image = cl.GLTexture(self.ctx, cl.mem_flags.READ_WRITE,
                                     GL_TEXTURE_2D, 0, gl_image._id, 2)
        self.cl_lapl_mask = cl.GLTexture(self.ctx, cl.mem_flags.READ_ONLY,
                                         GL_TEXTURE_2D, 0, gl_lapl_mask._id, 2)

        if self.use_gl_texture_as_tmp:
            self.gl_tmp = Texture2D(gl_image.data)
            self.cl_tmp = cl.GLTexture(self.ctx, cl.mem_flags.READ_WRITE,
                                       GL_TEXTURE_2D, 0, self.gl_tmp._id, 2)
        else:
            self.cl_tmp = cl.Image(
                self.ctx, cl.mem_flags.READ_WRITE,
                self.cl_image.get_image_info(cl.image_info.FORMAT), self.shape)
Esempio n. 8
0
    def init_openCL(self):
        self.gl_program.draw(gloo.gl.GL_TRIANGLE_STRIP)
        self.ocl_ctx = openCL.get_OCL_context()
        print("OpenCL context on device: %s" % self.ocl_ctx.devices[0])
        self.queue = pyopencl.CommandQueue(self.ocl_ctx)
        self.ocl_prg = pyopencl.Program(self.ocl_ctx, src).build()
        self.ary_float = pyopencl.array.empty(self.queue, shape=(self.tex_size,self.tex_size),dtype=numpy.float32)
    
        self.ocl_tex = pyopencl.GLTexture(self.ocl_ctx, pyopencl.mem_flags.READ_WRITE,
                        gloo.gl.GL_TEXTURE_2D, 0, int(self.gl_tex.handle), 2)

        img = numpy.random.randint(0,65000,self.tex_size**2).reshape((self.tex_size,self.tex_size)).astype(numpy.uint16)
        self.ocl_ary = pyopencl.array.to_device(self.queue, img)
    
        self.new_image(img)
Esempio n. 9
0
 def initialize_sim(self):
     # Now that OpenGL is setup, we can initialize interop correctly...
     self.sim_is_initialized = True
     self.sim = lb_cl.Pipe_Flow(diameter=D,
                                rho=rho,
                                viscosity=nu,
                                pressure_grad=pressure_grad,
                                pipe_length=pipe_length,
                                N=N,
                                time_prefactor=1,
                                two_d_local_size=(32, 32),
                                three_d_local_size=(32, 32, 1),
                                use_interop=True)
     print self.texture.id
     self.texture.glir
     gl_textureBuf = cl.GLTexture(self.sim.context, cl.mem_flags.READ_WRITE,
                                  gloo.gl.GL_TEXTURE_2D, 0, self.texture.id)
     # Replace "v" with the GL texture
     self.sim.v = gl_textureBuf
Esempio n. 10
0
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()
Esempio n. 11
0
    def __init__(self, lattice, origins):
        self.lattice = lattice
        self.context = self.lattice.context
        self.queue = self.lattice.queue
        self.float_type = self.lattice.memory.float_type

        self.count = len(origins)
        self.np_origins = numpy.ndarray(shape=(self.count, 2),
                                        dtype=self.float_type)

        for i, pos in enumerate(origins):
            self.np_origins[i, :] = pos

        self.cl_origins = cl.Buffer(self.context,
                                    mf.READ_ONLY,
                                    size=self.count * 2 *
                                    numpy.float32(0).nbytes)
        cl.enqueue_copy(self.queue, self.cl_origins, self.np_origins).wait()

        self.gl_texture_buffer = numpy.ndarray(
            shape=(self.lattice.memory.volume, 4),
            dtype=self.lattice.memory.float_type)
        self.gl_texture_buffer[:, :] = 0.0

        self.gl_streamlines = glGenTextures(1)
        self.gl_texture_type = GL_TEXTURE_2D
        glBindTexture(self.gl_texture_type, self.gl_streamlines)

        glTexImage2D(self.gl_texture_type, 0, GL_RGBA32F,
                     self.lattice.memory.size_x, self.lattice.memory.size_y, 0,
                     GL_RGBA, GL_FLOAT, self.gl_texture_buffer)
        glTexParameteri(self.gl_texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameteri(self.gl_texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameteri(self.gl_texture_type, GL_TEXTURE_WRAP_T,
                        GL_CLAMP_TO_EDGE)
        glTexParameteri(self.gl_texture_type, GL_TEXTURE_WRAP_S,
                        GL_CLAMP_TO_EDGE)
        self.cl_gl_streamlines = cl.GLTexture(self.lattice.context,
                                              mf.READ_WRITE,
                                              self.gl_texture_type, 0,
                                              self.gl_streamlines, 2)

        self.build_kernel()
Esempio n. 12
0
 def __init__(self, width=512, height=512):
     self.sample = 0
     self.width = width
     self.height = height
     self.tex = [_create_texture(width, height) for i in range(2)]
     self.ctx = _create_context()
     self.cache = ProgramCache(self.ctx)
     self.queue = cl.CommandQueue(self.ctx)
     mf = cl.mem_flags
     self.buf = [
         cl.GLTexture(self.ctx, mf.READ_WRITE, GL_TEXTURE_2D, 0, t, 2)
         for t in self.tex
     ]
     self.front = 0
     ident = np.array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
                      np.float32)
     self.cam_xform = cl.Buffer(self.ctx,
                                mf.READ_ONLY | mf.COPY_HOST_PTR,
                                hostbuf=ident)
     self.env = _load_exr(self.ctx, 'images/MeadowTrail.exr')
Esempio n. 13
0
File: filter.py Progetto: h3tch/psm
    def __init__(self, image_width, image_height, opencl_file, gl_image=None):
        enable_gl_sharing = gl_image is not None
        init_opencl(enable_gl_sharing)

        with open(opencl_file, 'r') as f:
            source = f.read()

        self._program = cl.Program(
            context,
            source).build(options=['-I', f'"{os.path.dirname(opencl_file)}"'])

        if gl_image is None:
            shape = (image_height, image_width)
            fmt = cl.ImageFormat(cl.channel_order.RGBA, cl.channel_type.UNORM_INT8)
            self._result_image = cl.Image(context, mem.WRITE_ONLY, fmt, shape)
        else:
            from OpenGL.GL import GL_TEXTURE_2D
            self._result_image = cl.GLTexture(context,
                                              mem.WRITE_ONLY,
                                              GL_TEXTURE_2D,
                                              0,
                                              gl_image,
                                              dims=2)