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 compute_histogram(cfg, show_dbg_points=False): cfg.duration = 10 cfg.aspect_ratio = (1, 1) hsize, size, local_size = _N * _N, _N, _N // 2 data = array.array("f") for i in range(size * size): data.extend(( cfg.rng.uniform(0.0, 0.5), cfg.rng.uniform(0.25, 0.75), cfg.rng.uniform(0.5, 1.0), 1.0, )) texture_buffer = ngl.BufferVec4(data=data) texture = ngl.Texture2D(width=size, height=size, data_src=texture_buffer) texture.set_format("r32g32b32a32_sfloat") histogram_block = ngl.Block(layout="std140", label="histogram") histogram_block.add_fields( ngl.BufferUInt(hsize, label="r"), ngl.BufferUInt(hsize, label="g"), ngl.BufferUInt(hsize, label="b"), ngl.UniformUIVec3(label="max"), ) shader_params = dict(hsize=hsize, size=size, local_size=local_size) group_size = hsize // local_size clear_histogram_shader = _COMPUTE_HISTOGRAM_CLEAR % shader_params clear_histogram_program = ngl.ComputeProgram(clear_histogram_shader, workgroup_size=(local_size, 1, 1)) clear_histogram_program.update_properties(hist=ngl.ResourceProps( writable=True)) clear_histogram = ngl.Compute( workgroup_count=(group_size, 1, 1), program=clear_histogram_program, label="clear_histogram", ) clear_histogram.update_resources(hist=histogram_block) group_size = size // local_size exec_histogram_shader = _COMPUTE_HISTOGRAM_EXEC % shader_params exec_histogram_program = ngl.ComputeProgram(exec_histogram_shader, workgroup_size=(local_size, local_size, 1)) exec_histogram_program.update_properties(hist=ngl.ResourceProps( writable=True)) exec_histogram = ngl.Compute(workgroup_count=(group_size, group_size, 1), program=exec_histogram_program, label="compute_histogram") exec_histogram.update_resources(hist=histogram_block, source=texture) exec_histogram_program.update_properties(source=ngl.ResourceProps( as_image=True)) quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) program = ngl.Program( vertex=_RENDER_HISTOGRAM_VERT, fragment=_RENDER_HISTOGRAM_FRAG % shader_params, ) program.update_vert_out_vars(var_uvcoord=ngl.IOVec2()) render = ngl.Render(quad, program, label="render_histogram") render.update_frag_resources(hist=histogram_block) group = ngl.Group(children=(clear_histogram, exec_histogram, render)) if show_dbg_points: cuepoints = _get_compute_histogram_cuepoints() group.add_children(get_debug_points(cfg, cuepoints)) return group
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 compute_histogram(cfg, show_dbg_points=False): random.seed(0) cfg.duration = 10 cfg.aspect_ratio = (1, 1) hsize, size, local_size = _N * _N, _N, _N // 2 data = array.array('f') for i in range(size * size): data.extend(( random.uniform(0.0, 0.5), random.uniform(0.25, 0.75), random.uniform(0.5, 1.0), 1.0, )) texture_buffer = ngl.BufferVec4(data=data) texture = ngl.Texture2D(width=size, height=size, data_src=texture_buffer) histogram_block = ngl.Block(layout='std430', label='histogram') histogram_block.add_fields( ngl.BufferUInt(hsize), ngl.BufferUInt(hsize), ngl.BufferUInt(hsize), ngl.UniformUIVec3(), ) 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' shader_params = dict(hsize=hsize, size=size, local_size=local_size) group_size = hsize // local_size clear_histogram_shader = _COMPUTE_HISTOGRAM_CLEAR % shader_params clear_histogram_program = ngl.ComputeProgram(shader_header + clear_histogram_shader) clear_histogram = ngl.Compute( group_size, 1, 1, clear_histogram_program, label='clear_histogram', ) clear_histogram.update_blocks(histogram=histogram_block) group_size = size // local_size exec_histogram_shader = _COMPUTE_HISTOGRAM_EXEC % shader_params exec_histogram_program = ngl.ComputeProgram(shader_header + exec_histogram_shader) exec_histogram = ngl.Compute( group_size, group_size, 1, exec_histogram_program, label='compute_histogram' ) exec_histogram.update_blocks(histogram=histogram_block) exec_histogram.update_textures(source=texture) quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) program = ngl.Program( vertex=shader_header + _RENDER_HISTOGRAM_VERT, fragment=shader_header + _RENDER_HISTOGRAM_FRAG % shader_params, ) render = ngl.Render(quad, program, label='render_histogram') render.update_blocks(histogram=histogram_block) group = ngl.Group(children=(clear_histogram, exec_histogram, render,)) if show_dbg_points: cuepoints = _get_compute_histogram_cuepoints() group.add_children(get_debug_points(cfg, cuepoints)) return group