def _rtt_load_attachment(cfg): quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) program = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color')) program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(), var_uvcoord=ngl.IOVec2()) background = ngl.Render(quad, program) background.update_frag_resources(color=ngl.UniformVec4(value=COLORS['white'])) program = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color')) program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(), var_uvcoord=ngl.IOVec2()) render = ngl.Render(quad, program) render.update_frag_resources(color=ngl.UniformVec4(value=COLORS['orange'])) texture = ngl.Texture2D(width=16, height=16) rtt = ngl.RenderToTexture(render, [texture]) texture_noop = ngl.Texture2D(width=16, height=16) rtt_noop = ngl.RenderToTexture(render, [texture_noop]) quad = ngl.Quad((0, 0, 0), (1, 0, 0), (0, 1, 0)) program = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('texture')) program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(), var_uvcoord=ngl.IOVec2()) foreground = ngl.Render(quad, program) foreground.update_frag_resources(tex0=texture) return ngl.Group(children=(background, rtt, rtt_noop, foreground))
def rtt_clear_attachment_with_timeranges(cfg): cfg.aspect_ratio = (1, 1) # Time-disabled full screen white quad render = ngl.RenderColor(COLORS.white) time_range_filter = ngl.TimeRangeFilter(render) time_range_filter.add_ranges(ngl.TimeRangeModeNoop(0)) # Intermediate no-op RTT to force the use of a different render pass internally texture = ngl.Texture2D(width=32, height=32) rtt_noop = ngl.RenderToTexture(ngl.Identity(), [texture]) # Centered rotating quad quad = ngl.Quad((-0.5, -0.5, 0), (1, 0, 0), (0, 1, 0)) render = ngl.RenderColor(COLORS.orange, geometry=quad) animkf = [ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, -360)] render = ngl.Rotate(render, angle=ngl.AnimatedFloat(animkf)) group = ngl.Group(children=(time_range_filter, rtt_noop, render)) # Root RTT texture = ngl.Texture2D(width=512, height=512) rtt = ngl.RenderToTexture(group, [texture]) # Full screen render of the root RTT result render = ngl.RenderTexture(texture) return ngl.Group(children=(rtt, render))
def media_timeranges_rtt(cfg): m0 = cfg.medias[0] cfg.duration = d = 10 cfg.aspect_ratio = (m0.width, m0.height) # Use a media/texture as leaf to exercise its prefetch/release mechanism media = ngl.Media(m0.filename) texture = ngl.Texture2D(data_src=media) # Diamond tree on the same media texture render0 = ngl.RenderTexture(texture, label="leaf 0") render1 = ngl.RenderTexture(texture, label="leaf 1") # Create intermediate RTT "proxy" to exercise prefetch/release at this # level as well dst_tex0 = ngl.Texture2D(width=m0.width, height=m0.height) dst_tex1 = ngl.Texture2D(width=m0.width, height=m0.height) rtt0 = ngl.RenderToTexture(render0, [dst_tex0]) rtt1 = ngl.RenderToTexture(render1, [dst_tex1]) # Render the 2 RTTs vertically split (one half content each) quad0 = ngl.Quad((-1, -1, 0), (1, 0, 0), (0, 2, 0), uv_corner=(0, 0), uv_width=(0.5, 0)) quad1 = ngl.Quad((0, -1, 0), (1, 0, 0), (0, 2, 0), uv_corner=(0.5, 0), uv_width=(0.5, 0)) rtt_render0 = ngl.RenderTexture(dst_tex0, geometry=quad0, label="render RTT 0") rtt_render1 = ngl.RenderTexture(dst_tex1, geometry=quad1, label="render RTT 1") proxy0 = ngl.Group(children=(rtt0, rtt_render0), label="proxy 0") proxy1 = ngl.Group(children=(rtt1, rtt_render1), label="proxy 1") # We want to make sure the idle times are enough to exercise the # prefetch/release mechanism prefetch_time = 1 assert prefetch_time < d / 5 # Split the presentation in 5 segments such that there are inactive times, # prefetch times and both overlapping and non-overlapping times for the # RTTs ranges0 = ( ngl.TimeRangeModeNoop(0), ngl.TimeRangeModeCont(1 / 5 * d), ngl.TimeRangeModeNoop(3 / 5 * d), ) ranges1 = ( ngl.TimeRangeModeNoop(0), ngl.TimeRangeModeCont(2 / 5 * d), ngl.TimeRangeModeNoop(4 / 5 * d), ) trange0 = ngl.TimeRangeFilter(proxy0, ranges=ranges0, prefetch_time=prefetch_time, label="left") trange1 = ngl.TimeRangeFilter(proxy1, ranges=ranges1, prefetch_time=prefetch_time, label="right") return ngl.Group(children=(trange0, trange1))
def _rtt_load_attachment(): background = ngl.RenderColor(COLORS.white) render = ngl.RenderColor(COLORS.orange) texture = ngl.Texture2D(width=16, height=16) rtt = ngl.RenderToTexture(render, [texture]) texture_noop = ngl.Texture2D(width=16, height=16) rtt_noop = ngl.RenderToTexture(render, [texture_noop]) quad = ngl.Quad((0, 0, 0), (1, 0, 0), (0, 1, 0)) foreground = ngl.RenderTexture(texture, geometry=quad) return ngl.Group(children=(background, rtt, rtt_noop, foreground))
def rtt_clear_attachment_with_timeranges(cfg): cfg.aspect_ratio = (1, 1) # Time-disabled full screen white quad quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) program = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color')) program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(), var_uvcoord=ngl.IOVec2()) render = ngl.Render(quad, program) render.update_frag_resources(color=ngl.UniformVec4(value=COLORS['white'])) time_range_filter = ngl.TimeRangeFilter(render) time_range_filter.add_ranges(ngl.TimeRangeModeNoop(0)) # Intermediate no-op RTT to force the use of a different render pass internally texture = ngl.Texture2D(width=32, height=32) rtt_noop = ngl.RenderToTexture(ngl.Identity(), [texture]) # Centered rotating quad quad = ngl.Quad((-0.5, -0.5, 0), (1, 0, 0), (0, 1, 0)) program = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color')) program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(), var_uvcoord=ngl.IOVec2()) render = ngl.Render(quad, program) render.update_frag_resources(color=ngl.UniformVec4(value=COLORS['orange'])) animkf = [ ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, -360) ] render = ngl.Rotate(render, anim=ngl.AnimatedFloat(animkf)) group = ngl.Group(children=(time_range_filter, rtt_noop, render)) # Root RTT texture = ngl.Texture2D(width=512, height=512) rtt = ngl.RenderToTexture(group, [texture]) # Full screen render of the root RTT result quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) program = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('texture')) program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(), var_uvcoord=ngl.IOVec2()) render = ngl.Render(quad, program) render.update_frag_resources(tex0=texture) return ngl.Group(children=(rtt, render))
def _get_texture_cubemap_from_mrt_scene_2_pass(cfg, samples=0): group = ngl.Group() quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) cube = ngl.TextureCube(size=64, min_filter="linear", mag_filter="linear") layer_base = 0 for layer_count, fragment in ((2, _RENDER_TO_CUBEMAP_1_FRAG), (4, _RENDER_TO_CUBEMAP_2_FRAG)): program = ngl.Program(vertex=_RENDER_TO_CUBEMAP_VERT, fragment=fragment, nb_frag_output=layer_count) program.update_vert_out_vars(var_uvcoord=ngl.IOVec3()) render = ngl.Render(quad, program) color_textures = [ ngl.TextureView(cube, layer) for layer in range(layer_base, layer_base + layer_count) ] rtt = ngl.RenderToTexture(render, color_textures, samples=samples) group.add_children(rtt) layer_base += layer_count program = ngl.Program(vertex=_RENDER_CUBEMAP_VERT, fragment=_RENDER_CUBEMAP_FRAG) program.update_vert_out_vars(var_uvcoord=ngl.IOVec3()) render = ngl.Render(quad, program) render.update_frag_resources(tex0=cube) group.add_children(render) return group
def _get_rtt_scene(cfg, features='depth', texture_ds_format=None, samples=0, mipmap_filter='none', sample_depth=False): cfg.duration = 10 cfg.aspect_ratio = (1, 1) cube = _get_cube() program = ngl.Program(vertex=_RENDER_CUBE_VERT, fragment=_RENDER_CUBE_FRAG) program.update_vert_out_vars(var_normal=ngl.IOVec3()) render = ngl.Render(cube, program) render = ngl.Scale(render, (0.5, 0.5, 0.5)) for i in range(3): rot_animkf = ngl.AnimatedFloat([ ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360 * (i + 1)) ]) axis = [int(i == x) for x in range(3)] render = ngl.Rotate(render, axis=axis, anim=rot_animkf) config = ngl.GraphicConfig(render, depth_test=True) camera = ngl.Camera( config, eye=(0.0, 0.0, 3.0), center=(0.0, 0.0, 0.0), up=(0.0, 1.0, 0.0), perspective=(45.0, cfg.aspect_ratio_float), clipping=(1.0, 10.0), ) size = 1024 texture_depth = None if texture_ds_format: texture_depth = ngl.Texture2D(width=size, height=size, format=texture_ds_format) texture = ngl.Texture2D( width=size, height=size, min_filter='linear', mipmap_filter=mipmap_filter, ) rtt = ngl.RenderToTexture( camera, [texture], features=features, depth_texture=texture_depth, samples=samples, clear_color=(0, 0, 0, 1), ) quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) if sample_depth: program = ngl.Program(vertex=cfg.get_vert('texture'), fragment=_RENDER_DEPTH) program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(), var_uvcoord=ngl.IOVec2()) render = ngl.Render(quad, program) render.update_frag_resources(tex0=texture_depth) else: program = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('texture')) program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(), var_uvcoord=ngl.IOVec2()) render = ngl.Render(quad, program) render.update_frag_resources(tex0=texture) return ngl.Group(children=(rtt, render))
def _shape_geometry_rtt(cfg, depth=False, samples=0): w, h = 640, 480 scene = _shape_geometry(cfg, set_normals=True) if depth: scene = ngl.GraphicConfig(scene, depth_test=True) texture = ngl.Texture2D() texture.set_width(w) texture.set_height(h) rtt = ngl.RenderToTexture(scene) rtt.add_color_textures(texture) if depth: texture = ngl.Texture2D() texture.set_format('d16_unorm') texture.set_width(w) texture.set_height(h) rtt.set_depth_texture(texture) else: rtt.set_clear_color(*COLORS['cgreen']) if samples: rtt.set_samples(samples) quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) program = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('texture')) render = ngl.Render(quad, program) render.update_textures(tex0=texture) return ngl.Group(children=(rtt, render))
def _get_random_layer(cfg, rng, t0, t1, enable_computes, layer=4): nb_elems = rng.randint(2, 5) children = [] sub_layers = rng.sample(range(nb_elems), 2) for i in range(nb_elems): if i in sub_layers and layer != 0: # Recursively create another layer child = _get_random_layer(cfg, rng, t0, t1, enable_computes, layer=layer - 1) child = _get_random_transform(rng, t0, t1, child) child.set_label(f"layer={layer}") # Create a (small) RTT of the children rtt_tex = ngl.Texture2D( width=rng.randint(50, 90), height=rng.randint(15, 70), ) rtt = ngl.RenderToTexture( child, clear_color=_get_random_color(rng) + (1, ), ) rtt.add_color_textures(rtt_tex) rtt_render = ngl.RenderTexture( rtt_tex, geometry=_get_random_geometry(rng), blending="src_over", ) rtt_render = _get_random_transform(rng, t0, t1, rtt_render) t_start, t_end = _get_random_time_range(rng, t0, t1) rtt_group = ngl.Group(children=(rtt, rtt_render)) t_filter = ngl.TimeRangeFilter(rtt_group) t_filter.add_ranges( ngl.TimeRangeModeNoop(0), ngl.TimeRangeModeCont(t_start), ngl.TimeRangeModeNoop(t_end), ) # Draw both the children and the rendered texture child = ngl.Group(children=(t_filter, child)) else: # We are at a leaf (last layer) so we create a random render t_start, t_end = _get_random_time_range(rng, t0, t1) child = _get_random_render(cfg, rng, t_start, t_end, enable_computes) if rng.random() < 1 / 3: child = _get_random_transform(rng, t_start, t_end, child) child = ngl.TimeRangeFilter(child) child.add_ranges( ngl.TimeRangeModeNoop(0), ngl.TimeRangeModeCont(t_start), ngl.TimeRangeModeNoop(t_end), ) children.append(child) return ngl.Group(children=children)
def cube(cfg, display_depth_buffer=False): ''' Cube with a common media Texture but a different color tainting on each side. Also includes a depth map visualization. ''' cube = ngl.Group(label='cube') frag_data = cfg.get_frag('tex-tint') program = ngl.Program(fragment=frag_data) texture = ngl.Texture2D(data_src=ngl.Media(cfg.medias[0].filename)) children = [_get_cube_side(texture, program, qi[0], qi[1], qi[2], qi[3]) for qi in _get_cube_quads()] cube.add_children(*children) for i in range(3): rot_animkf = ngl.AnimatedFloat([ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360 * (i + 1))]) axis = [int(i == x) for x in range(3)] cube = ngl.Rotate(cube, axis=axis, anim=rot_animkf) config = ngl.GraphicConfig(cube, depth_test=True) camera = ngl.Camera(config) camera.set_eye(0.0, 0.0, 2.0) camera.set_center(0.0, 0.0, 0.0) camera.set_up(0.0, 1.0, 0.0) camera.set_perspective(45.0, cfg.aspect_ratio_float) camera.set_clipping(1.0, 10.0) if not display_depth_buffer: return camera else: group = ngl.Group() depth_texture = ngl.Texture2D() depth_texture.set_format('d16_unorm') depth_texture.set_width(640) depth_texture.set_height(480) texture = ngl.Texture2D() texture.set_width(640) texture.set_height(480) rtt = ngl.RenderToTexture(camera) rtt.add_color_textures(texture) rtt.set_depth_texture(depth_texture) quad = ngl.Quad((-1.0, -1.0, 0), (1, 0, 0), (0, 1, 0)) program = ngl.Program() render = ngl.Render(quad, program) render.update_textures(tex0=texture) group.add_children(rtt, render) quad = ngl.Quad((0.0, 0.0, 0), (1, 0, 0), (0, 1, 0)) program = ngl.Program() render = ngl.Render(quad, program) render.update_textures(tex0=depth_texture) group.add_children(rtt, render) return group
def _rtt_load_attachment_nested(samples=0): scene = _rtt_load_attachment() texture = ngl.Texture2D(width=16, height=16) rtt = ngl.RenderToTexture(scene, [texture], samples=samples) foreground = ngl.RenderTexture(texture) return ngl.Group(children=(rtt, foreground))
def histogram(cfg): """Histogram using compute shaders""" m0 = cfg.medias[0] cfg.duration = m0.duration cfg.aspect_ratio = (m0.width, m0.height) g = ngl.Group() m = ngl.Media(cfg.medias[0].filename) t = ngl.Texture2D(data_src=m) h = ngl.Block(label="histogram_block", layout="std430") h.add_fields( ngl.BufferUInt(256, label="r"), ngl.BufferUInt(256, label="g"), ngl.BufferUInt(256, label="b"), ngl.UniformUInt(label="maximum"), ) r = ngl.RenderTexture(t) proxy_size = 128 proxy = ngl.Texture2D(width=proxy_size, height=proxy_size) rtt = ngl.RenderToTexture(r) rtt.add_color_textures(proxy) g.add_children(rtt) compute_program = ngl.ComputeProgram(cfg.get_comp("histogram-clear"), workgroup_size=(1, 1, 1)) compute_program.update_properties(hist=ngl.ResourceProps(writable=True)) compute = ngl.Compute(workgroup_count=(256, 1, 1), program=compute_program, label="histogram-clear") compute.update_resources(hist=h) g.add_children(compute) local_size = 8 group_size = proxy_size / local_size compute_program = ngl.ComputeProgram(cfg.get_comp("histogram-exec"), workgroup_size=(local_size, local_size, 1)) compute = ngl.Compute(workgroup_count=(group_size, group_size, 1), program=compute_program, label="histogram-exec") compute.update_resources(hist=h, source=proxy) compute_program.update_properties(hist=ngl.ResourceProps(writable=True)) compute_program.update_properties(source=ngl.ResourceProps(as_image=True)) g.add_children(compute) q = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) p = ngl.Program(vertex=cfg.get_vert("histogram-display"), fragment=cfg.get_frag("histogram-display")) p.update_vert_out_vars(var_uvcoord=ngl.IOVec2(), var_tex0_coord=ngl.IOVec2()) render = ngl.Render(q, p) render.update_frag_resources(tex0=t, hist=h) g.add_children(render) return g
def histogram(cfg): '''Histogram using compute shaders''' m0 = cfg.medias[0] cfg.duration = m0.duration cfg.aspect_ratio = (m0.width, m0.height) g = ngl.Group() m = ngl.Media(cfg.medias[0].filename) t = ngl.Texture2D(data_src=m) h = ngl.Block(label='histogram_block', layout='std430') h.add_fields( ngl.BufferUInt(256, label='r'), ngl.BufferUInt(256, label='g'), ngl.BufferUInt(256, label='b'), ngl.UniformInt(label='maximum'), ) q = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) r = ngl.Render(q) r.update_textures(tex0=t) proxy_size = 128 proxy = ngl.Texture2D(width=proxy_size, height=proxy_size) rtt = ngl.RenderToTexture(r) rtt.add_color_textures(proxy) g.add_children(rtt) shader_version = '310 es' if cfg.backend == 'gles' else '430' shader_header = '#version %s\n' % shader_version if cfg.backend == 'gles' and cfg.system == 'Android': shader_header += '#extension GL_ANDROID_extension_pack_es31a: require\n' compute_program = ngl.ComputeProgram(shader_header + cfg.get_comp('histogram-clear')) compute = ngl.Compute(256, 1, 1, compute_program, label='histogram-clear') compute.update_blocks(histogram_buffer=h) g.add_children(compute) local_size = 8 group_size = proxy_size / local_size compute_shader = cfg.get_comp('histogram-exec') % {'local_size': local_size} compute_program = ngl.ComputeProgram(shader_header + compute_shader) compute = ngl.Compute(group_size, group_size, 1, compute_program, label='histogram-exec') compute.update_blocks(histogram_buffer=h) compute.update_textures(source=proxy) g.add_children(compute) q = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) p = ngl.Program(vertex=shader_header + cfg.get_vert('histogram-display'), fragment=shader_header + cfg.get_frag('histogram-display')) render = ngl.Render(q, p) render.update_textures(tex0=t) render.update_blocks(histogram_buffer=h) g.add_children(render) return g
def texture_scissor(cfg): cfg.aspect_ratio = (1, 1) render = ngl.RenderColor(COLORS.orange) graphic_config = ngl.GraphicConfig(render, scissor_test=True, scissor=(32, 32, 32, 32)) texture = ngl.Texture2D(width=64, height=64) rtt = ngl.RenderToTexture(graphic_config, [texture], clear_color=(0, 0, 0, 1)) render = ngl.RenderTexture(texture) return ngl.Group(children=(rtt, render))
def texture_clear_and_scissor(cfg): render = ngl.RenderColor(COLORS.white) graphic_config = ngl.GraphicConfig(render, scissor_test=True, scissor=(0, 0, 0, 0), color_write_mask="") texture = ngl.Texture2D(width=64, height=64) rtt = ngl.RenderToTexture(ngl.Identity(), [texture], clear_color=list(COLORS.orange) + [1]) render = ngl.RenderTexture(texture) return ngl.Group(children=(graphic_config, rtt, render))
def rtt_load_attachment_nested(cfg): scene = _rtt_load_attachment(cfg) texture = ngl.Texture2D(width=16, height=16) rtt = ngl.RenderToTexture(scene, [texture]) quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) program = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('texture')) program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(), var_uvcoord=ngl.IOVec2()) foreground = ngl.Render(quad, program) foreground.update_frag_resources(tex0=texture) return ngl.Group(children=(rtt, foreground))
def _get_texture_cubemap_from_mrt_scene(cfg, samples=0): program = ngl.Program(vertex=_RENDER_TO_CUBEMAP_VERT, fragment=_RENDER_TO_CUBEMAP_FRAG, nb_frag_output=6) program.update_vert_out_vars(var_uvcoord=ngl.IOVec3()) quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) render = ngl.Render(quad, program) cube = ngl.TextureCube(size=64, min_filter="linear", mag_filter="linear") rtt = ngl.RenderToTexture(render, [cube], samples=samples) program = ngl.Program(vertex=_RENDER_CUBEMAP_VERT, fragment=_RENDER_CUBEMAP_FRAG) program.update_vert_out_vars(var_uvcoord=ngl.IOVec3()) render = ngl.Render(quad, program) render.update_frag_resources(tex0=cube) return ngl.Group(children=(rtt, render))
def filter_gamma_correct(cfg, linear=True): """This test operates a gamma correct blending (the blending happens in linear space)""" # Hue colors rotated clockwise dst = ngl.RenderGradient4( color_tl=COLORS.rose, color_tr=COLORS.blue, color_br=COLORS.sgreen, color_bl=COLORS.yellow, ) # Hue colors rotated counter-clockwise started with another color src = ngl.RenderGradient4( color_tl=COLORS.orange, color_tr=COLORS.magenta, color_br=COLORS.azure, color_bl=COLORS.green, ) # Screen blending so that working in linear space makes a significant # difference blend = ngl.GraphicConfig( ngl.Group(children=(dst, src)), blend=True, blend_src_factor="one", blend_dst_factor="one_minus_src_color", blend_src_factor_a="one", blend_dst_factor_a="zero", ) # Intermediate RTT so that we can gamma correct the result tex = ngl.Texture2D(width=320, height=240) rtt = ngl.RenderToTexture(blend, color_textures=[tex]) render = ngl.RenderTexture(tex) if linear: # The result of the combination is linear dst.add_filters(ngl.FilterSRGB2Linear()) src.add_filters(ngl.FilterSRGB2Linear()) # ...and we compress it back to sRGB render.add_filters(ngl.FilterLinear2sRGB()) return ngl.Group(children=(rtt, render))
def texture_scissor(cfg): cfg.aspect_ratio = (1, 1) quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) color = ngl.UniformVec4(COLORS['orange']) program = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color')) render = ngl.Render(quad, program) render.update_uniforms(color=color) graphic_config = ngl.GraphicConfig(render, scissor_test=True, scissor=(32, 32, 32, 32)) texture = ngl.Texture2D(width=64, height=64) rtt = ngl.RenderToTexture(graphic_config, [texture]) program = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('texture')) render = ngl.Render(quad, program) render.update_textures(tex0=texture) return ngl.Group(children=(rtt, render))
def texture_clear_and_scissor(cfg): quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) color = ngl.UniformVec4(COLORS['white']) program = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color')) render = ngl.Render(quad, program) render.update_uniforms(color=color) graphic_config = ngl.GraphicConfig(render, scissor_test=True, scissor=(0, 0, 0, 0)) texture = ngl.Texture2D(width=64, height=64) rtt = ngl.RenderToTexture(ngl.Identity(), [texture], clear_color=COLORS['orange']) program = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('texture')) render = ngl.Render(quad, program) render.update_textures(tex0=texture) return ngl.Group(children=(graphic_config, rtt, render))
def _get_texture_cubemap_from_mrt_scene(cfg, samples=0): glsl_version = '300 es' if cfg.backend == 'gles' else '330' glsl_header = '#version %s\n' % glsl_version render_to_cubemap_vert = glsl_header + _RENDER_TO_CUBEMAP_VERT render_to_cubemap_frag = glsl_header + _RENDER_TO_CUBEMAP_FRAG program = ngl.Program(vertex=render_to_cubemap_vert, fragment=render_to_cubemap_frag) quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) render = ngl.Render(quad, program) cube = ngl.TextureCube(size=64, min_filter="linear", mag_filter="linear") rtt = ngl.RenderToTexture(render, [cube], samples=samples) render_cubemap_vert = glsl_header + _RENDER_CUBEMAP_VERT render_cubemap_frag = glsl_header + _RENDER_CUBEMAP_FRAG program = ngl.Program(vertex=render_cubemap_vert, fragment=render_cubemap_frag) render = ngl.Render(quad, program) render.update_textures(tex0=cube) return ngl.Group(children=(rtt, render))
def histogram(cfg): '''Histogram using compute shaders''' m0 = cfg.medias[0] cfg.duration = m0.duration cfg.aspect_ratio = (m0.width, m0.height) g = ngl.Group() m = ngl.Media(cfg.medias[0].filename) t = ngl.Texture2D(data_src=m) h = ngl.Block(label='histogram_block', layout='std430') h.add_fields( ngl.BufferUInt(256, label='r'), ngl.BufferUInt(256, label='g'), ngl.BufferUInt(256, label='b'), ngl.UniformUInt(label='maximum'), ) q = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) p = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('texture')) p.update_vert_out_vars(var_uvcoord=ngl.IOVec2(), var_tex0_coord=ngl.IOVec2()) r = ngl.Render(q, p) r.update_frag_resources(tex0=t) proxy_size = 128 proxy = ngl.Texture2D(width=proxy_size, height=proxy_size) rtt = ngl.RenderToTexture(r) rtt.add_color_textures(proxy) g.add_children(rtt) compute_program = ngl.ComputeProgram(cfg.get_comp('histogram-clear')) compute = ngl.Compute(256, 1, 1, compute_program, label='histogram-clear') compute.update_resources(hist=h) g.add_children(compute) local_size = 8 group_size = proxy_size / local_size compute_shader = cfg.get_comp('histogram-exec') % { 'local_size': local_size } compute_program = ngl.ComputeProgram(compute_shader) compute = ngl.Compute(group_size, group_size, 1, compute_program, label='histogram-exec') compute.update_resources(hist=h, source=proxy) compute_program.update_properties(source=ngl.ResourceProps(as_image=True)) g.add_children(compute) q = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) p = ngl.Program(vertex=cfg.get_vert('histogram-display'), fragment=cfg.get_frag('histogram-display')) p.update_vert_out_vars(var_uvcoord=ngl.IOVec2(), var_tex0_coord=ngl.IOVec2()) render = ngl.Render(q, p) render.update_frag_resources(tex0=t, hist=h) g.add_children(render) return g
def cube(cfg, display_depth_buffer=False): """ Cube with a common media Texture but a different color tainting on each side. Also includes a depth map visualization. """ cube = ngl.Group(label="cube") vert_data = cfg.get_vert("texture") frag_data = cfg.get_frag("tex-tint") program = ngl.Program(vertex=vert_data, fragment=frag_data) program.update_vert_out_vars(var_uvcoord=ngl.IOVec2(), var_tex0_coord=ngl.IOVec2()) texture = ngl.Texture2D(data_src=ngl.Media(cfg.medias[0].filename)) children = [ _get_cube_side(texture, program, qi[0], qi[1], qi[2], qi[3]) for qi in _get_cube_quads() ] cube.add_children(*children) for i in range(3): rot_animkf = ngl.AnimatedFloat([ ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360 * (i + 1)) ]) axis = tuple(int(i == x) for x in range(3)) cube = ngl.Rotate(cube, axis=axis, angle=rot_animkf) config = ngl.GraphicConfig(cube, depth_test=True) camera = ngl.Camera(config) camera.set_eye(0.0, 0.0, 2.0) camera.set_center(0.0, 0.0, 0.0) camera.set_up(0.0, 1.0, 0.0) camera.set_perspective(45.0, cfg.aspect_ratio_float) camera.set_clipping(1.0, 10.0) if not display_depth_buffer: return camera else: group = ngl.Group() depth_texture = ngl.Texture2D() depth_texture.set_format("auto_depth") depth_texture.set_width(640) depth_texture.set_height(480) texture = ngl.Texture2D() texture.set_width(640) texture.set_height(480) rtt = ngl.RenderToTexture(camera) rtt.add_color_textures(texture) rtt.set_depth_texture(depth_texture) quad = ngl.Quad((-1.0, -1.0, 0), (1, 0, 0), (0, 1, 0)) render = ngl.RenderTexture(texture, geometry=quad) group.add_children(rtt, render) quad = ngl.Quad((0.0, 0.0, 0), (1, 0, 0), (0, 1, 0)) render = ngl.RenderTexture(depth_texture, geometry=quad) group.add_children(rtt, render) return group