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), single_uvec2=lambda data: ngl.UniformUIVec2(data), single_uvec3=lambda data: ngl.UniformUIVec3(data), single_uvec4=lambda data: ngl.UniformUIVec4(data), single_mat4=lambda data: ngl.UniformMat4(data), single_quat_mat4=lambda data: ngl.UniformQuat(data, as_mat4=True), single_quat_vec4=lambda data: ngl.UniformQuat(data, as_mat4=False), single_vec2=lambda data: ngl.UniformVec2(data), single_vec3=lambda data: ngl.UniformVec3(data), single_vec4=lambda data: ngl.UniformVec4(data), ) def _get_field_decl(layout, f): t = f['type'] t = t.split('_')[1] if t.startswith('quat') else t return '{}{:<5} {}{}{}'.format('uniform ' if layout == 'uniform' else '', t, 'field_' if layout == 'uniform' else '',
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 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