Example #1
0
    def load(self, name):
        props = self._loader.load(name, 'props.txt')
        args = []
        if props is not None:
            args = parse_args(props)

        in_img = StructArg('input_image', ImagePRGBA(1, 1))
        out_img = StructArg('output_image', ImagePRGBA(1, 1))
        args.extend([in_img, out_img])

        code = self._loader.load(name, 'code.py')
        self._shader = Shader(code=code, args=args)
        self._shader.compile()
        self._runtime = Runtime()
        self._shader.prepare([self._runtime])
Example #2
0
def _parse_image(line, image_factory=None):
    if image_factory is None:
        img = ImagePRGBA(1, 1)  # image of 1 pixel in size
    else:
        img = image_factory()
    name = line
    arg = StructArg(name, img)
    return arg
Example #3
0
    def load(self, shader_name, color_mgr):

        text = self._loader.load(shader_name, 'props.txt')
        args = []
        if text is not None:
            args = parse_args(text)
        code = self._loader.load(shader_name, 'code.py')
        if code is None:
            raise ValueError("Integrator %s code is missing!" % shader_name)

        #array of shadepoints for debuging purpose
        sp = ShadePoint.factory(color_mgr.zero())
        arr = ObjArray(sp)
        for i in range(10):
            arr.append(sp)
        arg = ArrayArg('path_array', arr)
        args.append(arg)

        hdr_buffer = StructArg('hdr_buffer', ImagePRGBA(1, 1))
        args.append(hdr_buffer)
        self.shader = Shader(code=code, args=args)
        self._color_mgr = color_mgr
Example #4
0
from sdl import ImagePRGBA
from imldr import load_image
from hdr import Tmo

tm = Tmo()
tm.load('exp')

in_img = load_image('E:/hdr_images/Desk_oBA2.hdr')
w, h = in_img.size()
out_img = ImagePRGBA(w, h)

tm.tmo(in_img, out_img)
Example #5
0
 def _create_hdr_buffer(self):
     width, height = self.sampler.get_resolution()
     self._hdr_buffer = ImagePRGBA(width, height)
     self._hdr_buffer.clear()
     self._hdr_output = ImagePRGBA(width, height)
Example #6
0
class Renderer:
    def __init__(self, color_mgr=RGBManager()):
        # def __init__(self, color_mgr=SampledManager()):
        self._color_mgr = color_mgr
        register_prototypes(color_mgr.zero())

        self.sampler = RegularSampler(width=512,
                                      height=512,
                                      pixelsize=1.0,
                                      nthreads=1)
        self.camera = Camera(eye=Vector3(5.0, 5.0, 5.0),
                             lookat=Vector3(0.0, 0.0, 0.0),
                             distance=200)
        self.camera.load('pinhole')
        self.shapes = ShapeManager()
        self.intersector = LinearIsect(self.shapes)
        self.materials = MaterialManager()
        self.lights = LightManager()
        self.filter = SampleFilter()
        self.filter.load('box')
        self.integrator = Integrator()
        self.integrator.load('test', self._color_mgr)

        self.tone_mapping = Tmo()
        self.tone_mapping.load('exp')

        self._ready = False
        self._create_hdr_buffer()

    @property
    def color_mgr(self):
        return self._color_mgr

    def _create_defaults(self):
        pass

    def _create_hdr_buffer(self):
        width, height = self.sampler.get_resolution()
        self._hdr_buffer = ImagePRGBA(width, height)
        self._hdr_buffer.clear()
        self._hdr_output = ImagePRGBA(width, height)

    def prepare(self):
        self._create_hdr_buffer()
        self.sync_area_lights()
        runtimes = [Runtime(code=4) for i in range(self.sampler.nthreads)]

        shaders_funcs = shaders_functions()
        for s in shaders_funcs:
            s.compile()
            s.prepare(runtimes)

        self.sampler.create_shader()
        self.sampler.compile()
        self.sampler.prepare(runtimes)
        self.sampler.reset()

        self.camera.compile()
        self.camera.prepare(runtimes)

        self.filter.compile()
        self.filter.prepare(runtimes)

        self.intersector.prepare_accel()
        self.intersector.compile()
        self.intersector.prepare(runtimes)

        self.lights.compile_shaders(self._color_mgr)
        self.lights.prepare_shaders(runtimes)
        for shape in self.lights.shapes_to_update():
            self.shapes.update(shape)

        lum_shader = luminance_shader(self._color_mgr)
        lum_shader.compile(color_mgr=self._color_mgr)
        lum_shader.prepare(runtimes)
        spec_to_rgb_shader = spectrum_to_rgb_shader(self._color_mgr)
        spec_to_rgb_shader.compile(color_mgr=self._color_mgr)
        spec_to_rgb_shader.prepare(runtimes)
        rgb_to_spec_shader = rgb_to_spectrum_shader(self._color_mgr)
        rgb_to_spec_shader.compile(color_mgr=self._color_mgr)
        rgb_to_spec_shader.prepare(runtimes)

        mat_shader_funcs = shaders_funcs + [lum_shader, rgb_to_spec_shader]
        self.materials.compile_shaders(self._color_mgr, mat_shader_funcs)
        self.materials.prepare_shaders(runtimes)

        shaders = [
            self.sampler.shader, self.camera.shader, self.intersector.shader,
            self.lights.rad_shader, self.lights.nlights_shader,
            self.lights.env_shader, self.lights.emission_shader,
            self.materials.ref_shader, spec_to_rgb_shader,
            self.intersector.visible_shader, lum_shader,
            self.materials.sampling_shader, self.materials.pdf_shader,
            self.filter.shader
        ]

        self.integrator.compile(shaders)
        self.integrator.prepare(runtimes)

        self._ready = True

    def reset(self):
        if self._hdr_buffer is not None:
            self._hdr_buffer.clear()
        self.sampler.reset()

    def render(self):
        """Execute rendering of one sample for each pixel. If we have
        multiple samples per pixel we must call this function multiple
        times. Function return True if all samples are processed otherwise
        False.
        """
        if not self._ready:
            self.prepare()

        if not self.sampler.has_more_samples():
            return True

        self.integrator.execute(self._hdr_buffer)
        # print (self.integrator.shader._asm_code)
        # self.integrator.shader._mc.print_machine_code()
        self.sampler.increment_pass()

        if not self.sampler.has_more_samples():
            return True

        return False

    def tmo(self):
        if self._hdr_buffer is not None:
            self.tone_mapping.tmo(self._hdr_buffer, self._hdr_output)

    def save(self, filename):
        f = open(filename, mode='w')
        directory = os.path.dirname(os.path.abspath(f.name))
        mesh_dir = os.path.join(directory, 'meshes')
        if not os.path.isdir(mesh_dir):
            os.mkdir(mesh_dir)

        txt = self.sampler.output()
        f.write(txt)
        f.write('\n')
        txt = self.camera.output()
        f.write(txt)
        f.write('\n')
        txt = self.lights.output()
        f.write(txt)
        f.write('\n')
        txt = self.materials.output()
        f.write(txt)
        f.write('\n')

        for shape in self.shapes:
            text = 'Shape\n'
            fname = self.shapes.name(shape) + '.data'
            relative_name = os.path.join('meshes', fname)
            text += shape.output(directory, relative_name)
            text += 'name = %s\n' % self.shapes.name(shape)
            text += 'material = %s\n' % self.materials.name(shape.mat_idx)
            text += 'End\n\n'
            f.write(text)

        f.close()

    def load(self, filename):

        import_scene(filename, self)
        self._ready = False

    def sync_area_lights(self):
        for shape in self.shapes:
            material = self.materials.material(index=shape.mat_idx)
            area_light = self.lights.arealight(shape)
            if material.is_emissive() and area_light is None:
                light = AreaLight(shape=shape, material=material)
                light.load('general', self._color_mgr)
                name = 'arealight_%i' % id(light)
                self.lights.add(name, light)
            elif not material.is_emissive() and area_light is not None:
                area_light.shape.light_id = -1
                self.lights.remove(light=area_light)
                self.shapes.update(area_light.shape)
Example #7
0
 def _create_hdr_buffer(self):
     width, height = self.sampler.get_resolution()
     self._hdr_buffer = ImagePRGBA(width, height)
     self._hdr_buffer.clear()
     self._hdr_output = ImagePRGBA(width, height)
Example #8
0
class Renderer:
    def __init__(self, color_mgr=RGBManager()):
    # def __init__(self, color_mgr=SampledManager()):
        self._color_mgr = color_mgr
        register_prototypes(color_mgr.zero())

        self.sampler = RegularSampler(width=512, height=512,
                                      pixelsize=1.0, nthreads=1)
        self.camera = Camera(eye=Vector3(5.0, 5.0, 5.0),
                             lookat=Vector3(0.0, 0.0, 0.0), distance=200)
        self.camera.load('pinhole')
        self.shapes = ShapeManager()
        self.intersector = LinearIsect(self.shapes)
        self.materials = MaterialManager()
        self.lights = LightManager()
        self.filter = SampleFilter()
        self.filter.load('box')
        self.integrator = Integrator()
        self.integrator.load('test', self._color_mgr)

        self.tone_mapping = Tmo()
        self.tone_mapping.load('exp')

        self._ready = False
        self._create_hdr_buffer()

    @property
    def color_mgr(self):
        return self._color_mgr

    def _create_defaults(self):
        pass

    def _create_hdr_buffer(self):
        width, height = self.sampler.get_resolution()
        self._hdr_buffer = ImagePRGBA(width, height)
        self._hdr_buffer.clear()
        self._hdr_output = ImagePRGBA(width, height)

    def prepare(self):
        self._create_hdr_buffer()
        self.sync_area_lights()
        runtimes = [Runtime(code=4) for i in range(self.sampler.nthreads)]

        shaders_funcs = shaders_functions()
        for s in shaders_funcs:
            s.compile()
            s.prepare(runtimes)

        self.sampler.create_shader()
        self.sampler.compile()
        self.sampler.prepare(runtimes)
        self.sampler.reset()

        self.camera.compile()
        self.camera.prepare(runtimes)

        self.filter.compile()
        self.filter.prepare(runtimes)

        self.intersector.prepare_accel()
        self.intersector.compile()
        self.intersector.prepare(runtimes)

        self.lights.compile_shaders(self._color_mgr)
        self.lights.prepare_shaders(runtimes)
        for shape in self.lights.shapes_to_update():
            self.shapes.update(shape)

        lum_shader = luminance_shader(self._color_mgr)
        lum_shader.compile(color_mgr=self._color_mgr)
        lum_shader.prepare(runtimes)
        spec_to_rgb_shader = spectrum_to_rgb_shader(self._color_mgr)
        spec_to_rgb_shader.compile(color_mgr=self._color_mgr)
        spec_to_rgb_shader.prepare(runtimes)
        rgb_to_spec_shader = rgb_to_spectrum_shader(self._color_mgr)
        rgb_to_spec_shader.compile(color_mgr=self._color_mgr)
        rgb_to_spec_shader.prepare(runtimes)

        mat_shader_funcs = shaders_funcs + [lum_shader, rgb_to_spec_shader]
        self.materials.compile_shaders(self._color_mgr, mat_shader_funcs)
        self.materials.prepare_shaders(runtimes)

        shaders = [self.sampler.shader, self.camera.shader,
                   self.intersector.shader, self.lights.rad_shader,
                   self.lights.nlights_shader, self.lights.env_shader,
                   self.lights.emission_shader, self.materials.ref_shader,
                   spec_to_rgb_shader, self.intersector.visible_shader,
                   lum_shader, self.materials.sampling_shader,
                   self.materials.pdf_shader, self.filter.shader]

        self.integrator.compile(shaders)
        self.integrator.prepare(runtimes)

        self._ready = True

    def reset(self):
        if self._hdr_buffer is not None:
            self._hdr_buffer.clear()
        self.sampler.reset()

    def render(self):
        """Execute rendering of one sample for each pixel. If we have
        multiple samples per pixel we must call this function multiple
        times. Function return True if all samples are processed otherwise
        False.
        """
        if not self._ready:
            self.prepare()

        if not self.sampler.has_more_samples():
            return True

        self.integrator.execute(self._hdr_buffer)
        # print (self.integrator.shader._asm_code)
        # self.integrator.shader._mc.print_machine_code()
        self.sampler.increment_pass()

        if not self.sampler.has_more_samples():
            return True

        return False

    def tmo(self):
        if self._hdr_buffer is not None:
            self.tone_mapping.tmo(self._hdr_buffer, self._hdr_output)

    def save(self, filename):
        f = open(filename, mode='w')
        directory = os.path.dirname(os.path.abspath(f.name))
        mesh_dir = os.path.join(directory, 'meshes')
        if not os.path.isdir(mesh_dir):
            os.mkdir(mesh_dir)

        txt = self.sampler.output()
        f.write(txt)
        f.write('\n')
        txt = self.camera.output()
        f.write(txt)
        f.write('\n')
        txt = self.lights.output()
        f.write(txt)
        f.write('\n')
        txt = self.materials.output()
        f.write(txt)
        f.write('\n')

        for shape in self.shapes:
            text = 'Shape\n'
            fname = self.shapes.name(shape) + '.data'
            relative_name = os.path.join('meshes', fname)
            text += shape.output(directory, relative_name)
            text += 'name = %s\n' % self.shapes.name(shape)
            text += 'material = %s\n' % self.materials.name(shape.mat_idx)
            text += 'End\n\n'
            f.write(text)

        f.close()

    def load(self, filename):

        import_scene(filename, self)
        self._ready = False

    def sync_area_lights(self):
        for shape in self.shapes:
            material = self.materials.material(index=shape.mat_idx)
            area_light = self.lights.arealight(shape)
            if material.is_emissive() and area_light is None:
                light = AreaLight(shape=shape, material=material)
                light.load('general', self._color_mgr)
                name = 'arealight_%i' % id(light)
                self.lights.add(name, light)
            elif not material.is_emissive() and area_light is not None:
                area_light.shape.light_id = -1
                self.lights.remove(light=area_light)
                self.shapes.update(area_light.shape)