Skip to content

Instantly share code, notes, and snippets.

@tobspr
Created June 27, 2014 14:11
Show Gist options
  • Save tobspr/4b658f2922b7f2b855ea to your computer and use it in GitHub Desktop.
Save tobspr/4b658f2922b7f2b855ea to your computer and use it in GitHub Desktop.
from panda3d.core import *
from direct.directbase import DirectStart
import math
# Shader
compute_source = """#version 430
layout (rgba8) uniform image2D destTex;
layout (local_size_x = 32, local_size_y = 32) in;
void main() {
vec4 result = vec4(0, 0, 0, 1);
int abc = 0;
for (int i = 0; i < 2000; i ++) {
abc += i;
}
result.x = float(abc) / 200000.0;
ivec2 storePos = ivec2(gl_GlobalInvocationID.xy);
imageStore(destTex, storePos, result);
}"""
# Create shader / compute node
shader = Shader.make_compute(Shader.SL_GLSL, compute_source)
compute_node = ComputeNode('compute')
compute_node.add_dispatch(1600/32, 960/32, 1)
tex = Texture()
# Create destination tex
tex.setup_2d_texture(1600, 960, Texture.TFloat, Texture.FRgba8)
tex.set_format(Texture.F_rgba8)
tex.set_minfilter(Texture.FT_nearest)
tex.set_magfilter(Texture.FT_nearest)
# Attach compute node and set shader inputs
np = render.attach_new_node(compute_node)
np.set_shader(shader)
np.set_shader_input('destTex', tex)
# Create card to show texture
cm = CardMaker('card')
cm.set_frame_fullscreen_quad()
card = render2d.attach_new_node(cm.generate())
card.set_texture(tex)
# Done!
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment