Skip to content

Commit

Permalink
Use more efficient set_shader_inputs call
Browse files Browse the repository at this point in the history
  • Loading branch information
rdb committed Feb 6, 2017
1 parent da5410a commit 7eca36c
Show file tree
Hide file tree
Showing 41 changed files with 259 additions and 154 deletions.
11 changes: 6 additions & 5 deletions data/default_cubemap/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ def __init__(self):

for i in range(6):
node.set_shader(cshader)
node.set_shader_input("SourceTex", cubemap)
node.set_shader_input("DestTex", dest_cubemap)
node.set_shader_input("currentSize", size)
node.set_shader_input("currentMip", mipmap)
node.set_shader_input("currentFace", i)
node.set_shader_inputs(
SourceTex=cubemap,
DestTex=dest_cubemap,
currentSize=size,
currentMip=mipmap,
currentFace=i)
attr = node.get_attrib(ShaderAttrib)
self.graphicsEngine.dispatch_compute(
( (size + 15) // 16, (size+15) // 16, 1), attr, self.win.gsg)
Expand Down
14 changes: 14 additions & 0 deletions rpcore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@
# This file includes all classes from the pipeline which are exposed
from rpcore.render_pipeline import RenderPipeline
from rpcore.native import SpotLight, PointLight

# Polyfill a set_shader_inputs function for older versions of Panda.
from panda3d.core import NodePath
from direct.extensions_native.extension_native_helpers import Dtool_funcToMethod
from rplibs.six import iteritems

if not hasattr(NodePath, 'set_shader_inputs'):
def set_shader_inputs(self, **inputs):
set_shader_input = self.set_shader_input
for args in iteritems(inputs):
set_shader_input(*args)

Dtool_funcToMethod(set_shader_inputs, NodePath)
del set_shader_inputs
5 changes: 3 additions & 2 deletions rpcore/gpu_command_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ def _create_command_target(self):
self._command_target = RenderTarget("ExecCommandTarget")
self._command_target.size = 1, 1
self._command_target.prepare_buffer()
self._command_target.set_shader_input("CommandQueue", self._data_texture)
self._command_target.set_shader_input("commandCount", self._pta_num_commands)
self._command_target.set_shader_inputs(
CommandQueue=self._data_texture,
commandCount=self._pta_num_commands)
9 changes: 5 additions & 4 deletions rpcore/gui/buffer_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,11 @@ def _render_stages(self):
image=stage_tex, w=scale_factor * w, h=scale_factor * h,
any_filter=False, parent=node, x=7, y=40, transparent=False)

preview.set_shader_input("mipmap", 0)
preview.set_shader_input("slice", 0)
preview.set_shader_input("brightness", 1)
preview.set_shader_input("tonemap", False)
preview.set_shader_inputs(
mipmap=0,
slice=0,
brightness=1,
tonemap=False)

preview_shader = DisplayShaderBuilder.build(
stage_tex, scale_factor * w, scale_factor * h)
Expand Down
5 changes: 3 additions & 2 deletions rpcore/gui/exposure_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def _late_init(self, task):
exposure_tex = stage_mgr.pipes["Exposure"]
self._cshader = RPLoader.load_shader("/$$rp/shader/visualize_exposure.compute.glsl")
self._cshader_np.set_shader(self._cshader)
self._cshader_np.set_shader_input("DestTex", self._storage_tex)
self._cshader_np.set_shader_input("ExposureTex", exposure_tex)
self._cshader_np.set_shader_inputs(
DestTex=self._storage_tex,
ExposureTex=exposure_tex)

return task.done
16 changes: 9 additions & 7 deletions rpcore/gui/fps_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,21 @@ def _late_init(self, task):

self._cshader = RPLoader.load_shader("/$$rp/shader/fps_chart.compute.glsl")
self._cshader_np.set_shader(self._cshader)
self._cshader_np.set_shader_input("DestTex", self._display_tex)
self._cshader_np.set_shader_input("FPSValues", self._storage_buffer)
self._cshader_np.set_shader_input("index", self._store_index)
self._cshader_np.set_shader_input("maxMs", self._chart_ms_max)
self._cshader_np.set_shader_inputs(
DestTex=self._display_tex,
FPSValues=self._storage_buffer,
index=self._store_index,
maxMs=self._chart_ms_max)

self._update_shader_node = ComputeNode("FPSChartUpdateValues")
self._update_shader_node.add_dispatch(1, 1, 1)
self._update_shader_np = self._node.attach_new_node(self._update_shader_node)
self._ushader = RPLoader.load_shader("/$$rp/shader/fps_chart_update.compute.glsl")
self._update_shader_np.set_shader(self._ushader)
self._update_shader_np.set_shader_input("DestTex", self._storage_buffer)
self._update_shader_np.set_shader_input("index", self._store_index)
self._update_shader_np.set_shader_input("currentData", self._current_ftime)
self._update_shader_np.set_shader_inputs(
DestTex=self._storage_buffer,
index=self._store_index,
currentData=self._current_ftime)

Globals.base.addTask(self._update, "UpdateFPSChart", sort=-50)

Expand Down
9 changes: 5 additions & 4 deletions rpcore/gui/pipe_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,11 @@ def _populate_content(self): # pylint: disable=too-many-branches,too-many-state
preview_shader = DisplayShaderBuilder.build(pipe_tex, int(w), int(h))
preview.set_shader(preview_shader)

preview.set_shader_input("mipmap", 0)
preview.set_shader_input("slice", 0)
preview.set_shader_input("brightness", 1)
preview.set_shader_input("tonemap", False)
preview.set_shader_inputs(
mipmap=0,
slice=0,
brightness=1,
tonemap=False)

if icon_file:
Sprite(image=icon_file, parent=node,
Expand Down
4 changes: 4 additions & 0 deletions rpcore/gui/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ def set_shader_input(self, *args):
""" Sets a shader input on the image """
self.node.set_shader_input(*args)

def set_shader_inputs(self, **kwargs):
""" Sets multiple shader inputs on the image """
self.node.set_shader_inputs(**kwargs)

def remove(self):
""" Removes the image """
self.node.remove()
Expand Down
9 changes: 5 additions & 4 deletions rpcore/gui/texture_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,11 @@ def present(self, tex):
text_size=18, expand_width=90)
x_pos += 90 + 30

image.set_shader_input("slice", 0)
image.set_shader_input("mipmap", 0)
image.set_shader_input("brightness", 1)
image.set_shader_input("tonemap", False)
image.set_shader_inputs(
slice=0,
mipmap=0,
brightness=1,
tonemap=False)

preview_shader = DisplayShaderBuilder.build(tex, display_w, display_h)
image.set_shader(preview_shader)
Expand Down
6 changes: 6 additions & 0 deletions rpcore/render_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def set_shader_input(self, *args):
for target in itervalues(self._targets):
target.set_shader_input(*args)

def set_shader_inputs(self, **kwargs):
""" This method sets shader inputs on all stages, which is mainly used
by the stage manager """
for target in itervalues(self._targets):
target.set_shader_inputs(**kwargs)

def update(self):
""" This method gets called every frame, and can be overridden by render
stages to perform custom updates """
Expand Down
5 changes: 5 additions & 0 deletions rpcore/render_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ def set_shader_input(self, *args, **kwargs):
if self.create_default_region:
self._source_region.set_shader_input(*args, **kwargs)

def set_shader_inputs(self, **kwargs):
""" Sets shader inputs available to the target """
if self.create_default_region:
self._source_region.set_shader_inputs(**kwargs)

@setter
def shader(self, shader_obj):
""" Sets a shader on the target """
Expand Down
5 changes: 3 additions & 2 deletions rpcore/stages/collect_used_cells_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ def create(self):
self.cell_list_buffer = Image.create_buffer("CellList", 0, "R32I")
self.cell_index_buffer = Image.create_2d_array("CellIndices", 0, 0, 0, "R32I")

self.target.set_shader_input("CellListBuffer", self.cell_list_buffer)
self.target.set_shader_input("CellListIndices", self.cell_index_buffer)
self.target.set_shader_inputs(
CellListBuffer=self.cell_list_buffer,
CellListIndices=self.cell_index_buffer)

def update(self):
self.cell_list_buffer.clear_image()
Expand Down
30 changes: 17 additions & 13 deletions rpcore/stages/cull_lights_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,23 @@ def create(self):
self.grouped_cell_lights_counts = Image.create_buffer(
"GroupedPerCellLightsCount", 0, "R16UI")

self.target_visible.set_shader_input("FrustumLights", self.frustum_lights)
self.target_visible.set_shader_input("FrustumLightsCount", self.frustum_lights_ctr)
self.target_cull.set_shader_input("PerCellLightsBuffer", self.per_cell_lights)
self.target_cull.set_shader_input("PerCellLightCountsBuffer", self.per_cell_light_counts)
self.target_cull.set_shader_input("FrustumLights", self.frustum_lights)
self.target_cull.set_shader_input("FrustumLightsCount", self.frustum_lights_ctr)
self.target_group.set_shader_input("PerCellLightsBuffer", self.per_cell_lights)
self.target_group.set_shader_input("PerCellLightCountsBuffer", self.per_cell_light_counts)
self.target_group.set_shader_input("GroupedCellLightsBuffer", self.grouped_cell_lights)
self.target_group.set_shader_input("GroupedPerCellLightsCountBuffer", self.grouped_cell_lights_counts)

self.target_cull.set_shader_input("threadCount", self.cull_threads)
self.target_group.set_shader_input("threadCount", 1)
self.target_visible.set_shader_inputs(
FrustumLights=self.frustum_lights,
FrustumLightsCount=self.frustum_lights_ctr)

self.target_cull.set_shader_inputs(
PerCellLightsBuffer=self.per_cell_lights,
PerCellLightCountsBuffer=self.per_cell_light_counts,
FrustumLights=self.frustum_lights,
FrustumLightsCount=self.frustum_lights_ctr,
threadCount=self.cull_threads)

self.target_group.set_shader_inputs(
PerCellLightsBuffer=self.per_cell_lights,
PerCellLightCountsBuffer=self.per_cell_light_counts,
GroupedCellLightsBuffer=self.grouped_cell_lights,
GroupedPerCellLightsCountBuffer=self.grouped_cell_lights_counts,
threadCount=1)

def reload_shaders(self):
self.target_cull.shader = self.load_shader(
Expand Down
3 changes: 3 additions & 0 deletions rpcore/stages/gbuffer_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ def create(self):

def set_shader_input(self, *args):
Globals.render.set_shader_input(*args)

def set_shader_inputs(self, **kwargs):
Globals.render.set_shader_inputs(**kwargs)
3 changes: 3 additions & 0 deletions rpcore/stages/shadow_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,6 @@ def create(self):

def set_shader_input(self, *args):
Globals.render.set_shader_input(*args)

def set_shader_inputs(self, **kwargs):
Globals.render.set_shader_inputs(**kwargs)
19 changes: 11 additions & 8 deletions rpcore/util/cubemap_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ def _make_specular_targets(self):
target_filter = self._stage.create_target("CF:SpecIBL-PostFilter-" + str(mipsize))
target_filter.size = mipsize * 6, mipsize
target_filter.prepare_buffer()
target_filter.set_shader_input("currentMip", mip)
target_filter.set_shader_input("SourceTex", self._spec_pref_map)
target_filter.set_shader_inputs(
currentMip=mip,
SourceTex=self._spec_pref_map)
target_filter.set_shader_input(
"DestMipmap", self._specular_map, False, True, -1, mip, 0)

Expand All @@ -152,9 +153,10 @@ def _make_diffuse_target(self):
CubemapFilter.PREFILTER_CUBEMAP_SIZE)
self._diffuse_target.prepare_buffer()

self._diffuse_target.set_shader_input("SourceCubemap", self._specular_map)
self._diffuse_target.set_shader_input("DestCubemap", self._prefilter_map)
self._diffuse_target.set_shader_input("cubeSize", CubemapFilter.PREFILTER_CUBEMAP_SIZE)
self._diffuse_target.set_shader_inputs(
SourceCubemap=self._specular_map,
DestCubemap=self._prefilter_map,
cubeSize=CubemapFilter.PREFILTER_CUBEMAP_SIZE)

# Create the target which removes the noise from the previous target,
# which is introduced with importance sampling
Expand All @@ -164,9 +166,10 @@ def _make_diffuse_target(self):
CubemapFilter.DIFFUSE_CUBEMAP_SIZE)
self._diff_filter_target.prepare_buffer()

self._diff_filter_target.set_shader_input("SourceCubemap", self._prefilter_map)
self._diff_filter_target.set_shader_input("DestCubemap", self._diffuse_map)
self._diff_filter_target.set_shader_input("cubeSize", CubemapFilter.DIFFUSE_CUBEMAP_SIZE)
self._diff_filter_target.set_shader_inputs(
SourceCubemap=self._prefilter_map,
DestCubemap=self._diffuse_map,
cubeSize=CubemapFilter.DIFFUSE_CUBEMAP_SIZE)

def reload_shaders(self):
""" Sets all required shaders on the filter. """
Expand Down
3 changes: 3 additions & 0 deletions rpcore/util/post_process_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def set_shader_input(self, *args, **kwargs):
else:
self._tri.set_shader_input(*args)

def set_shader_inputs(self, **kwargs):
self._tri.set_shader_inputs(**kwargs)

def _make_fullscreen_cam(self):
""" Creates an orthographic camera for the buffer """
buffer_cam = Camera("BufferCamera")
Expand Down
21 changes: 11 additions & 10 deletions rpcore/util/shader_input_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ class SimpleInputBlock(RPObject):
def __init__(self, name):
""" Creates the ubo with the given name """
RPObject.__init__(self)
self.inputs = {}
self._inputs = {}
self.name = name

def add_input(self, name, value):
""" Adds a new input to the UBO """
self.inputs[name] = value
self._inputs[self.name + "." + name] = value

def bind_to(self, target):
""" Binds the UBO to a target """
for key, val in iteritems(self.inputs):
target.set_shader_input(self.name + "." + key, val)
target.set_shader_inputs(**self._inputs)


class GroupedInputBlock(RPObject):
Expand Down Expand Up @@ -79,6 +78,7 @@ def __init__(self, name):
""" Constructs the input block with a given name """
RPObject.__init__(self)
self.ptas = {}
self._inputs = {}
self.name = name
self.use_ubo = bool(TypeRegistry.ptr().find_type("GLUniformBufferContext"))

Expand All @@ -92,7 +92,12 @@ def __init__(self, name):

def register_pta(self, name, input_type):
""" Registers a new input, type should be a glsl type """
self.ptas[name] = self.glsl_type_to_pta(input_type).empty_array(1)
pta = self.glsl_type_to_pta(input_type).empty_array(1)
self.ptas[name] = pta
if self.use_ubo:
self._inputs[self.name + "_UBO." + name] = pta
else:
self._inputs[self.name + "." + name] = pta

def pta_to_glsl_type(self, pta_handle):
""" Converts a PtaXXX to a glsl type """
Expand All @@ -112,11 +117,7 @@ def bind_to(self, target):
""" Binds all inputs of this UBO to the given target, which may be
either a RenderTarget or a NodePath """

for pta_name, pta_handle in iteritems(self.ptas):
if self.use_ubo:
target.set_shader_input(self.name + "_UBO." + pta_name, pta_handle)
else:
target.set_shader_input(self.name + "." + pta_name, pta_handle)
target.set_shader_inputs(**self._inputs)

def update_input(self, name, value):
""" Updates an existing input """
Expand Down
20 changes: 11 additions & 9 deletions rpplugins/ao/ao_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def create(self):
self.target_upscale.add_color_attachment(bits=(8, 0, 0, 0))
self.target_upscale.prepare_buffer()

self.target_upscale.set_shader_input("SourceTex", self.target.color_tex)
self.target_upscale.set_shader_input("upscaleWeights", Vec2(0.001, 0.001))
self.target_upscale.set_shader_inputs(
SourceTex=self.target.color_tex,
upscaleWeights=Vec2(0.001, 0.001))

self.tarrget_detail_ao = self.create_target("DetailAO")
self.tarrget_detail_ao.add_color_attachment(bits=(8, 0, 0, 0))
Expand Down Expand Up @@ -85,14 +86,15 @@ def create(self):
target_blur_h.add_color_attachment(bits=(8, 0, 0, 0))
target_blur_h.prepare_buffer()

target_blur_v.set_shader_input("SourceTex", current_tex)
target_blur_h.set_shader_input("SourceTex", target_blur_v.color_tex)
target_blur_v.set_shader_inputs(
SourceTex=current_tex,
blur_direction=LVecBase2i(0, 1),
pixel_stretch=pixel_stretch)

target_blur_v.set_shader_input("blur_direction", LVecBase2i(0, 1))
target_blur_h.set_shader_input("blur_direction", LVecBase2i(1, 0))

target_blur_v.set_shader_input("pixel_stretch", pixel_stretch)
target_blur_h.set_shader_input("pixel_stretch", pixel_stretch)
target_blur_h.set_shader_inputs(
SourceTex=target_blur_v.color_tex,
blur_direction=LVecBase2i(1, 0),
pixel_stretch=pixel_stretch)

current_tex = target_blur_h.color_tex
self.blur_targets += [target_blur_v, target_blur_h]
Expand Down
13 changes: 7 additions & 6 deletions rpplugins/bloom/bloom_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ def create(self):
target = self.create_target("Downsample:Step-" + str(i))
target.size = -scale_multiplier, -scale_multiplier
target.prepare_buffer()
target.set_shader_input("sourceMip", i)
target.set_shader_input("SourceTex", self.scene_target_img)
target.set_shader_inputs(
sourceMip=i,
SourceTex=self.scene_target_img)
target.set_shader_input("DestTex", self.scene_target_img, False, True, -1, i + 1)
self.downsample_targets.append(target)

Expand All @@ -89,10 +90,10 @@ def create(self):
target = self.create_target("Upsample:Step-" + str(i))
target.size = -scale_multiplier, -scale_multiplier
target.prepare_buffer()
target.set_shader_input("FirstUpsamplePass", i == 0)

target.set_shader_input("sourceMip", self.num_mips - i)
target.set_shader_input("SourceTex", self.scene_target_img)
target.set_shader_inputs(
FirstUpsamplePass=(i==0),
sourceMip=(self.num_mips - i),
SourceTex=self.scene_target_img)
target.set_shader_input("DestTex", self.scene_target_img,
False, True, -1, self.num_mips - i - 1)
self.upsample_targets.append(target)
Expand Down
Loading

0 comments on commit 7eca36c

Please sign in to comment.