示例#1
0
def compute_image_load_store(cfg, show_dbg_points=False):
    size = _N
    texture_data = ngl.BufferFloat(
        data=array.array("f", [x / (size**2) for x in range(size**2)]))
    texture_r = ngl.Texture2D(format="r32_sfloat",
                              width=size,
                              height=size,
                              data_src=texture_data)
    texture_g = ngl.Texture2D(format="r32_sfloat",
                              width=size,
                              height=size,
                              data_src=texture_data)
    texture_b = ngl.Texture2D(format="r32_sfloat",
                              width=size,
                              height=size,
                              data_src=texture_data)
    scale = ngl.Block(
        fields=[ngl.UniformVec2(value=(-1.0, 1.0), label="factors")],
        layout="std140",
    )
    texture_rgba = ngl.Texture2D(width=size, height=size)
    program = ngl.ComputeProgram(_IMAGE_LOAD_STORE_COMPUTE,
                                 workgroup_size=(size, size, 1))
    program.update_properties(
        texture_r=ngl.ResourceProps(as_image=True),
        texture_g=ngl.ResourceProps(as_image=True),
        texture_b=ngl.ResourceProps(as_image=True),
        texture_rgba=ngl.ResourceProps(as_image=True, writable=True),
    )
    compute = ngl.Compute(workgroup_count=(1, 1, 1), program=program)
    compute.update_resources(texture_r=texture_r,
                             texture_g=texture_g,
                             texture_b=texture_b,
                             scale=scale,
                             texture_rgba=texture_rgba)

    render = ngl.RenderTexture(texture_rgba)
    group = ngl.Group(children=(compute, render))

    if show_dbg_points:
        cuepoints = _get_compute_histogram_cuepoints()
        group.add_children(get_debug_points(cfg, cuepoints))

    return group
示例#2
0
def mountain(cfg, ndim=3, nb_layers=7,
             ref_color=(0.5, .75, .75, 1.0), nb_mountains=6):
    '''Mountain generated with a stack of noise shaders using Textures as random source'''
    random.seed(0)
    random_dim = 1 << ndim
    cfg.aspect_ratio = (16, 9)
    cfg.duration = nb_mountains ** 2

    def get_rand():
        return array.array('f', [random.uniform(0, 1) for x in range(random_dim)])

    black, white = (0, 0, 0, 1), (1, 1, 1, 1)
    quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0))

    prog = ngl.Program(fragment=cfg.get_frag('mountain'))
    hscale = 1/2.
    mountains = []
    for i in range(nb_mountains):
        yoffset = (nb_mountains-i-1)/float(nb_mountains-1) * (1.0 - hscale)

        if i < nb_mountains/2:
            c0, c1 = ref_color, white
            x = (i + 1) / float(nb_mountains/2 + 1)
        else:
            c0, c1 = black, ref_color
            x = (i - nb_mountains/2) / float((nb_mountains-1)/2)
        mcolor = [x*a + (1.0-x)*b for a, b in zip(c0, c1)]

        random_buf = ngl.BufferFloat(data=get_rand())
        random_tex = ngl.Texture2D(data_src=random_buf, width=random_dim, height=1)

        utime_animkf = [ngl.AnimKeyFrameFloat(0, 0),
                        ngl.AnimKeyFrameFloat(cfg.duration, i+1)]
        utime = ngl.AnimatedFloat(utime_animkf)

        uyoffset_animkf = [ngl.AnimKeyFrameFloat(0, yoffset/2.),
                           ngl.AnimKeyFrameFloat(cfg.duration/2.0, yoffset),
                           ngl.AnimKeyFrameFloat(cfg.duration, yoffset/2.)]
        uyoffset = ngl.AnimatedFloat(uyoffset_animkf)

        render = ngl.Render(quad, prog)
        render.update_textures(tex0=random_tex)
        render.update_uniforms(dim=ngl.UniformInt(random_dim))
        render.update_uniforms(nb_layers=ngl.UniformInt(nb_layers))
        render.update_uniforms(time=utime)
        render.update_uniforms(lacunarity=ngl.UniformFloat(2.0))
        render.update_uniforms(gain=ngl.UniformFloat(0.5))
        render.update_uniforms(mcolor=ngl.UniformVec4(mcolor))
        render.update_uniforms(yoffset=uyoffset)
        render.update_uniforms(hscale=ngl.UniformFloat(hscale))

        mountains.append(render)

    prog = ngl.Program(fragment=cfg.get_frag('color'))
    sky = ngl.Render(quad, prog)
    sky.update_uniforms(color=ngl.UniformVec4(white))

    group = ngl.Group(children=[sky] + mountains)
    blend = ngl.GraphicConfig(group,
                              blend=True,
                              blend_src_factor='src_alpha',
                              blend_dst_factor='one_minus_src_alpha',
                              blend_src_factor_a='zero',
                              blend_dst_factor_a='one')
    return blend
示例#3
0
     ngl.AnimKeyFrameVec3, data)),
 animated_vec4=lambda data: ngl.AnimatedVec4(keyframes=_get_anim_kf(
     ngl.AnimKeyFrameVec4, data)),
 animated_buffer_float=lambda data: ngl.AnimatedBufferFloat(
     keyframes=_get_anim_kf(ngl.AnimKeyFrameBuffer, data)),
 animated_buffer_vec2=lambda data: ngl.AnimatedBufferVec2(
     keyframes=_get_anim_kf(ngl.AnimKeyFrameBuffer, data)),
 animated_buffer_vec3=lambda data: ngl.AnimatedBufferVec3(
     keyframes=_get_anim_kf(ngl.AnimKeyFrameBuffer, data)),
 animated_buffer_vec4=lambda data: ngl.AnimatedBufferVec4(
     keyframes=_get_anim_kf(ngl.AnimKeyFrameBuffer, data)),
 animated_quat_mat4=lambda data: ngl.AnimatedQuat(
     keyframes=_get_anim_kf(ngl.AnimKeyFrameQuat, data), as_mat4=True),
 animated_quat_vec4=lambda data: ngl.AnimatedQuat(
     keyframes=_get_anim_kf(ngl.AnimKeyFrameQuat, data), as_mat4=False),
 array_float=lambda data: ngl.BufferFloat(data=data),
 array_int=lambda data: ngl.BufferInt(data=data),
 array_ivec2=lambda data: ngl.BufferIVec2(data=data),
 array_ivec3=lambda data: ngl.BufferIVec3(data=data),
 array_ivec4=lambda data: ngl.BufferIVec4(data=data),
 array_mat4=lambda data: ngl.BufferMat4(data=data),
 array_vec2=lambda data: ngl.BufferVec2(data=data),
 array_vec3=lambda data: ngl.BufferVec3(data=data),
 array_vec4=lambda data: ngl.BufferVec4(data=data),
 single_bool=lambda data: ngl.UniformBool(data),
 single_float=lambda data: ngl.UniformFloat(data),
 single_int=lambda data: ngl.UniformInt(data),
 single_ivec2=lambda data: ngl.UniformIVec2(data),
 single_ivec3=lambda data: ngl.UniformIVec3(data),
 single_ivec4=lambda data: ngl.UniformIVec4(data),
 single_uint=lambda data: ngl.UniformUInt(data),
def mountain(cfg,
             ndim=3,
             nb_layers=7,
             ref_color=(0.5, 0.75, 0.75),
             nb_mountains=6):
    """Mountain generated with a stack of noise shaders using Textures as random source"""
    random_dim = 1 << ndim
    cfg.aspect_ratio = (16, 9)
    cfg.duration = nb_mountains**2

    def get_rand():
        return array.array("f",
                           [cfg.rng.uniform(0, 1) for x in range(random_dim)])

    black, white = (0, 0, 0), (1, 1, 1)
    quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0))

    prog = ngl.Program(vertex=cfg.get_vert("texture"),
                       fragment=cfg.get_frag("mountain"))
    prog.update_vert_out_vars(var_uvcoord=ngl.IOVec2(),
                              var_tex0_coord=ngl.IOVec2())
    hscale = 1 / 2.0
    mountains = []
    for i in range(nb_mountains):
        yoffset = (nb_mountains - i - 1) / float(nb_mountains - 1) * (1.0 -
                                                                      hscale)

        if i < nb_mountains / 2:
            c0, c1 = ref_color, white
            x = (i + 1) / float(nb_mountains / 2 + 1)
        else:
            c0, c1 = black, ref_color
            x = (i - nb_mountains / 2) / float((nb_mountains - 1) / 2)
        mcolor = [x * a + (1.0 - x) * b for a, b in zip(c0, c1)]

        random_buf = ngl.BufferFloat(data=get_rand())
        random_tex = ngl.Texture2D(data_src=random_buf,
                                   width=random_dim,
                                   height=1)

        utime_animkf = [
            ngl.AnimKeyFrameFloat(0, 0),
            ngl.AnimKeyFrameFloat(cfg.duration, i + 1)
        ]
        utime = ngl.AnimatedFloat(utime_animkf)

        uyoffset_animkf = [
            ngl.AnimKeyFrameFloat(0, yoffset / 2.0),
            ngl.AnimKeyFrameFloat(cfg.duration / 2.0, yoffset),
            ngl.AnimKeyFrameFloat(cfg.duration, yoffset / 2.0),
        ]
        uyoffset = ngl.AnimatedFloat(uyoffset_animkf)

        render = ngl.Render(quad, prog, blending="src_over")
        render.update_frag_resources(tex0=random_tex)
        render.update_frag_resources(dim=ngl.UniformInt(random_dim))
        render.update_frag_resources(nb_layers=ngl.UniformInt(nb_layers))
        render.update_frag_resources(time=utime)
        render.update_frag_resources(lacunarity=ngl.UniformFloat(2.0))
        render.update_frag_resources(gain=ngl.UniformFloat(0.5))
        render.update_frag_resources(mcolor=ngl.UniformVec3(mcolor))
        render.update_frag_resources(yoffset=uyoffset)
        render.update_frag_resources(hscale=ngl.UniformFloat(hscale))

        mountains.append(render)

    sky = ngl.RenderColor(white[:3])

    group = ngl.Group(children=[sky] + mountains)
    return group