Skip to content

Commit

Permalink
2to3 with various manual fixes (cg C functions wants ascii bytes, not…
Browse files Browse the repository at this point in the history
… strings; // instead of / where only integers were meant, ...)
  • Loading branch information
benma committed Jan 17, 2013
1 parent 7a28c3a commit c4d01cf
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 84 deletions.
6 changes: 3 additions & 3 deletions src/cg/__init__.py
@@ -1,6 +1,6 @@
"""
Quick and easy bindings to the Cg libraries.
"""
from cg_shader import CGVertexShader, CGFragmentShader, CGVertexFragmentShader, CGDefaultShader
from platform import cg_platform, cg_gl_platform
import cg, cg_gl
from .cg_shader import CGVertexShader, CGFragmentShader, CGVertexFragmentShader, CGDefaultShader
from .platform import cg_platform, cg_gl_platform
from . import cg, cg_gl
14 changes: 7 additions & 7 deletions src/cg/cg_gl.py
Expand Up @@ -938,7 +938,7 @@ def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname) # macro
GL_COMPRESSED_RGBA = 34030 # Variable c_int '34030'
GL_TEXTURE21_ARB = 34005 # Variable c_int '34005'
GL_NV_framebuffer_multisample_coverage = 1 # Variable c_int '1'
GL_TIMEOUT_IGNORED = 18446744073709551615L # Variable c_ulonglong '-1ull'
GL_TIMEOUT_IGNORED = 18446744073709551615 # Variable c_ulonglong '-1ull'
GL_SAMPLER_1D_ARRAY_EXT = 36288 # Variable c_int '36288'
GL_PROXY_HISTOGRAM = 32805 # Variable c_int '32805'
GL_PGI_misc_hints = 1 # Variable c_int '1'
Expand Down Expand Up @@ -2070,7 +2070,7 @@ def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname) # macro
GL_BLEND_DST_RGB_EXT = 32968 # Variable c_int '32968'
GL_INTENSITY16_SNORM = 36891 # Variable c_int '36891'
GL_MATRIX24_ARB = 35032 # Variable c_int '35032'
GL_ALL_SHADER_BITS = 4294967295L # Variable c_uint '4294967295u'
GL_ALL_SHADER_BITS = 4294967295 # Variable c_uint '4294967295u'
GL_DRAW_BUFFER14_ARB = 34867 # Variable c_int '34867'
GL_ARB_map_buffer_range = 1 # Variable c_int '1'
GL_SAMPLER_2D_MULTISAMPLE_ARRAY = 37131 # Variable c_int '37131'
Expand Down Expand Up @@ -2337,7 +2337,7 @@ def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname) # macro
GL_DS_BIAS_NV = 34582 # Variable c_int '34582'
GL_MAX_PROGRAM_NATIVE_ATTRIBS_ARB = 34991 # Variable c_int '34991'
GL_POST_COLOR_MATRIX_BLUE_BIAS = 32954 # Variable c_int '32954'
GL_TEXCOORD4_BIT_PGI = 2147483648L # Variable c_uint '2147483648u'
GL_TEXCOORD4_BIT_PGI = 2147483648 # Variable c_uint '2147483648u'
GL_VERTEX_ARRAY = 32884 # Variable c_int '32884'
GL_MIRRORED_REPEAT_IBM = 33648 # Variable c_int '33648'
GL_COMPRESSED_SRGB_S3TC_DXT1_EXT = 35916 # Variable c_int '35916'
Expand Down Expand Up @@ -2788,7 +2788,7 @@ def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname) # macro
GL_DEBUG_SOURCE_OTHER_ARB = 33355 # Variable c_int '33355'
GL_MAP1_VERTEX_ATTRIB8_4_NV = 34408 # Variable c_int '34408'
GL_STENCIL_INDEX16 = 36169 # Variable c_int '36169'
GL_CLIENT_ALL_ATTRIB_BITS = 4294967295L # Variable c_uint '4294967295u'
GL_CLIENT_ALL_ATTRIB_BITS = 4294967295 # Variable c_uint '4294967295u'
GL_VALIDATE_STATUS = 35715 # Variable c_int '35715'
GL_RG16 = 33324 # Variable c_int '33324'
GL_LIGHT7 = 16391 # Variable c_int '16391'
Expand Down Expand Up @@ -2935,7 +2935,7 @@ def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname) # macro
GL_ARB_imaging = 1 # Variable c_int '1'
GL_SGIX_fog_offset = 1 # Variable c_int '1'
GL_RG32UI = 33340 # Variable c_int '33340'
GL_ALL_CLIENT_ATTRIB_BITS = 4294967295L # Variable c_uint '4294967295u'
GL_ALL_CLIENT_ATTRIB_BITS = 4294967295 # Variable c_uint '4294967295u'
GL_NORMALIZE = 2977 # Variable c_int '2977'
GL_MAX_CONVOLUTION_WIDTH_EXT = 32794 # Variable c_int '32794'
GL_TEXTURE_LO_SIZE_NV = 34588 # Variable c_int '34588'
Expand Down Expand Up @@ -3263,7 +3263,7 @@ def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname) # macro
GL_SHADE_MODEL = 2900 # Variable c_int '2900'
GL_GENERATE_MIPMAP_HINT_SGIS = 33170 # Variable c_int '33170'
GL_APPLE_row_bytes = 1 # Variable c_int '1'
GL_ALL_BARRIER_BITS_EXT = 4294967295L # Variable c_uint '4294967295u'
GL_ALL_BARRIER_BITS_EXT = 4294967295 # Variable c_uint '4294967295u'
GL_COMPRESSED_SLUMINANCE = 35914 # Variable c_int '35914'
GL_AMD_performance_monitor = 1 # Variable c_int '1'
GL_EXT_stencil_clear_tag = 1 # Variable c_int '1'
Expand Down Expand Up @@ -4955,7 +4955,7 @@ def __ASMNAME(cname): return __ASMNAME2 (__USER_LABEL_PREFIX__, cname) # macro
GL_FRAMEBUFFER_ATTACHMENT_LAYERED_ARB = 36263 # Variable c_int '36263'
GL_STENCIL_BACK_PASS_DEPTH_FAIL_ATI = 34818 # Variable c_int '34818'
GL_VERTEX_PROGRAM_CALLBACK_MESA = 35765 # Variable c_int '35765'
GL_INVALID_INDEX = 4294967295L # Variable c_uint '4294967295u'
GL_INVALID_INDEX = 4294967295 # Variable c_uint '4294967295u'
GL_POST_CONVOLUTION_GREEN_SCALE_EXT = 32797 # Variable c_int '32797'
GL_STENCIL_BUFFER_BIT = 1024 # Variable c_int '1024'
GL_ALPHA16I_EXT = 36234 # Variable c_int '36234'
Expand Down
18 changes: 8 additions & 10 deletions src/cg/cg_shader.py
Expand Up @@ -4,13 +4,14 @@
import sys
from ctypes import *

from cg import *
from cg_gl import *
from .cg import *
from .cg_gl import *

from platform import cg_platform as cg, cg_gl_platform as cg_gl
from .platform import cg_platform as cg, cg_gl_platform as cg_gl

cg_gl.cgGLSetDebugMode(CG_FALSE)
cg.cgCreateProgram.restype = CGprogram
cg.cgCombinePrograms2.restype = CGprogram
cg.cgGetParameterName.restype = c_char_p
cg.cgGetProfileString.restype = c_char_p
cg.cgGetLastErrorString.restype = c_char_p
Expand Down Expand Up @@ -61,7 +62,7 @@ def create_profile(shader_type):
# this will never return glsl
profile = cg_gl.cgGLGetLatestProfile(shader_type)

print "profile: ", cg.cgGetProfileString(profile)
print("profile: ", cg.cgGetProfileString(profile))
cg_gl.cgGLSetOptimalOptions(profile)
check_for_cg_error(context, "selecting profile")

Expand Down Expand Up @@ -93,20 +94,17 @@ class _CGShader(object):
def __init__(self, code, entry=None):
# for now: vertex and fragment shader in same source
assert self.vertex ^ self.fragment, "Use one of CGVertexShader, CGFragmentShader or CGVertexFragmentShader."

self.entry = entry or self.default_entry
self.context = _create_context()
self.profile = create_profile(CG_GL_VERTEX if self.vertex else CG_GL_FRAGMENT)

program = cg.cgCreateProgram(
self.context, # Cg runtime context
CG_SOURCE, # Program in human-readable form
code, # string of source code
code.encode('ascii'), # string of source code
self.profile, # Profile: OpenGL ARB vertex program
entry, # Entry function name
entry.encode('ascii'), # Entry function name
None # No extra compiler options
)

self.error_prefix = '%s shader, entry = %s' % ("vertex" if self.vertex else "fragment", entry)

self.check_error("creating program from string")
Expand Down Expand Up @@ -192,7 +190,7 @@ def unbind(self):
cg_gl.cgGLUnbindProgram(self.fragment_profile)

def get_parameter(self, name):
p = cg.cgGetNamedParameter(self.program, name)
p = cg.cgGetNamedParameter(self.program, name.encode('ascii'))
self.check_error("could not get %r parameter" % name)
return CGParameter(name, p)

Expand Down
2 changes: 1 addition & 1 deletion src/fluid_rendering/__init__.py
@@ -1,2 +1,2 @@
from simple import render_particles
from .simple import render_particles
#from fluid_renderer import FluidRenderer
2 changes: 1 addition & 1 deletion src/fluid_rendering/blur_shader.py
Expand Up @@ -2,7 +2,7 @@
import os

class BlurShader(CGDefaultShader):
VERTICAL, HORIZONTAL = range(2)
VERTICAL, HORIZONTAL = list(range(2))

def __init__(self, size=(800,800), radius=10, sigma=3):
self.width, self.height = size
Expand Down
12 changes: 6 additions & 6 deletions src/fluid_rendering/fluid_renderer.py
Expand Up @@ -5,9 +5,9 @@
import os

from cg import CGDefaultShader, cg_gl_platform, cg_gl
import simple
from . import simple

from blur_shader import BlurShader
from .blur_shader import BlurShader


class FluidShader(CGDefaultShader):
Expand Down Expand Up @@ -42,7 +42,7 @@ def set_default_parameters(self):

class FluidRenderer(object):
number_of_render_modes = 3
RENDERMODE_POINTS, RENDERMODE_BALLS, RENDERMODE_ADVANCED = range(number_of_render_modes)
RENDERMODE_POINTS, RENDERMODE_BALLS, RENDERMODE_ADVANCED = list(range(number_of_render_modes))
render_mode = RENDERMODE_ADVANCED
# render_mode = RENDERMODE_BALLS

Expand Down Expand Up @@ -179,8 +179,8 @@ def render_point_sprites(self, shader, enable_depth_test=True):
else:
glDisable(GL_DEPTH_TEST)

# this lets use define the size of the point in the shader
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE)
# this lets use define the size of the point in the shader
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE)

# this enables rendering of small squares
glEnable(GL_POINT_SPRITE)
Expand Down Expand Up @@ -232,7 +232,7 @@ def render_advanced(self):
if self.smooth_depth:
cl.enqueue_acquire_gl_objects(self.queue, self.cl_gl_objects)
local_size = self.cl_local_size
for i in xrange(self.smoothing_iterations):
for i in range(self.smoothing_iterations):
# alternate between writing to depth2_target and depth1_target
# (can't read from and write to the same texture at the same time).
args = (np.float32(self.smoothing_dt),
Expand Down
16 changes: 8 additions & 8 deletions src/main.py
Expand Up @@ -5,12 +5,12 @@

if __name__ == "__main__":
if '--help' in sys.argv:
print "Run with: python main.py [--disable-advanced-rendering] [--cg-arb] [--cg-glsl] N"
print "\t--disable-advanced-rendering: disables the use of Cg shaders"
print "\tChange the vertex/fragment profiles that Cg will use:"
print "\t\t--cg-arb:\tuse arb profiles."
print "\t\t--cg-glsl:\tuse glsl profiles."
print "\tN: number of particles. Start with 8000 and slowly increase"
print("Run with: python main.py [--disable-advanced-rendering] [--cg-arb] [--cg-glsl] N")
print("\t--disable-advanced-rendering: disables the use of Cg shaders")
print("\tChange the vertex/fragment profiles that Cg will use:")
print("\t\t--cg-arb:\tuse arb profiles.")
print("\t\t--cg-glsl:\tuse glsl profiles.")
print("\tN: number of particles. Start with 8000 and slowly increase")
sys.exit(0)

def excepthook(type, value, traceback):
Expand All @@ -27,8 +27,8 @@ def excepthook(type, value, traceback):
try:
N = int(sys.argv[-1]) #20**3
except (IndexError, ValueError):
print "Invalid number of particles. Use 'python main.py 8000', for example"
print "See also: python main.py --help"
print("Invalid number of particles. Use 'python main.py 8000', for example")
print("See also: python main.py --help")
exit(1)
window = MainWindow(N)
window.show()
Expand Down
8 changes: 4 additions & 4 deletions src/main_window.py
Expand Up @@ -65,7 +65,7 @@ def callback(p):
ui.paused.toggled.connect(callback)

if self.sph_demo.enable_advanced_rendering:
from fluid_rendering.fluid_renderer import FluidRenderer
#from fluid_rendering.fluid_renderer import FluidRenderer

def callback(p):
self.sph_demo.params.blur_thickness_map = p
Expand All @@ -75,13 +75,13 @@ def callback(p):
ui.render_mean_curvature.toggled.connect(callback)

def callback():
self.sph_demo.params.render_mode = FluidRenderer.RENDERMODE_POINTS
self.sph_demo.params.render_mode = 0
ui.rm_points.pressed.connect(callback)
def callback():
self.sph_demo.params.render_mode = FluidRenderer.RENDERMODE_BALLS
self.sph_demo.params.render_mode = 1
ui.rm_balls.pressed.connect(callback)
def callback():
self.sph_demo.params.render_mode = FluidRenderer.RENDERMODE_ADVANCED
self.sph_demo.params.render_mode = 2
ui.rm_advanced.pressed.connect(callback)
ui.rm_advanced.toggled.connect(ui.advanced.setEnabled)

Expand Down
2 changes: 1 addition & 1 deletion src/sph/__init__.py
@@ -1,4 +1,4 @@
"""
A module for simulating fluids using particles.
"""
from sph import FluidSimulator
from .sph import FluidSimulator
6 changes: 3 additions & 3 deletions src/sph/bitonic_sort/bitonic_sort.py
Expand Up @@ -152,10 +152,10 @@ def int_log2(L):
from time import time
t = time()
s.sort_in_place(d_keys, d_vals, N, 1)
print "%ims" % ((time()-t)*1000)
print("%ims" % ((time()-t)*1000))

cl.enqueue_copy(queue, keys, d_keys)
cl.enqueue_copy(queue, vals, d_vals)
queue.finish()
print np.linalg.norm(keys-sorted_keys)
print np.linalg.norm(vals-sorted_vals)
print(np.linalg.norm(keys-sorted_keys))
print(np.linalg.norm(vals-sorted_vals))
2 changes: 1 addition & 1 deletion src/sph/radix_sort/__init__.py
@@ -1 +1 @@
from radix_sort import RadixSort
from .radix_sort import RadixSort
34 changes: 17 additions & 17 deletions src/sph/radix_sort/radix_sort.py
Expand Up @@ -26,9 +26,9 @@ def __init__(self, ctx, queue, max_elements, dtype):
self.radix_prg = cl.Program(self.ctx, f.read()).build()

if (max_elements % (cta_size * 4)) == 0:
num_blocks = max_elements / (cta_size * 4)
num_blocks = max_elements // (cta_size * 4)
else:
num_blocks = max_elements / (cta_size * 4) + 1
num_blocks = max_elements // (cta_size * 4) + 1

self.d_temp_keys = cl.Buffer(self.ctx, mf.READ_WRITE, size=self.dtype_size * max_elements)
self.d_temp_values = cl.Buffer(self.ctx, mf.READ_WRITE, size=self.dtype_size * max_elements)
Expand All @@ -37,10 +37,10 @@ def __init__(self, ctx, queue, max_elements, dtype):
self.d_counters_sum = cl.Buffer(self.ctx, mf.READ_WRITE, size=self.dtype_size * self.WARP_SIZE * num_blocks)
self.d_block_offsets = cl.Buffer(self.ctx, mf.READ_WRITE, size=self.dtype_size * self.WARP_SIZE * num_blocks)

numscan = max_elements/2/cta_size*16
numscan = max_elements//2//cta_size*16
if numscan >= self.MIN_LARGE_ARRAY_SIZE:
#MAX_WORKGROUP_INCLUSIVE_SCAN_SIZE 1024
self.scan_buffer = cl.Buffer(self.ctx, mf.READ_WRITE, size = self.dtype_size * numscan / 1024)
self.scan_buffer = cl.Buffer(self.ctx, mf.READ_WRITE, size = self.dtype_size * numscan // 1024)

def sort(self, d_key, d_val, N):
key_bits = self.dtype_size * 8
Expand All @@ -57,7 +57,7 @@ def step(self, d_key, d_val, nbits, startbit, num):
self.find_offsets(startbit, num)
self.queue.finish()

array_length = num/2/self.cta_size*16
array_length = num//2//self.cta_size*16
if array_length < self.MIN_LARGE_ARRAY_SIZE:
self.naive_scan(num)
else:
Expand All @@ -69,7 +69,7 @@ def step(self, d_key, d_val, nbits, startbit, num):


def blocks(self, d_key, d_val, nbits, startbit, num):
totalBlocks = num/4/self.cta_size
totalBlocks = num//4//self.cta_size
global_size = (self.cta_size*totalBlocks,)
local_size = (self.cta_size,)
blocks_args = (d_key,
Expand All @@ -87,7 +87,7 @@ def blocks(self, d_key, d_val, nbits, startbit, num):


def find_offsets(self, startbit, num):
totalBlocks = num/2/self.cta_size
totalBlocks = num//2//self.cta_size
global_size = (self.cta_size*totalBlocks,)
local_size = (self.cta_size,)
offsets_args = (self.d_temp_keys,
Expand All @@ -103,10 +103,10 @@ def find_offsets(self, startbit, num):


def naive_scan(self, num):
nhist = num/2/self.cta_size*16
nhist = num//2//self.cta_size*16
global_size = (nhist,)
local_size = (nhist,)
extra_space = nhist / 16 #NUM_BANKS defined as 16 in RadixSort.cpp
extra_space = nhist // 16 #NUM_BANKS defined as 16 in RadixSort.cpp
shared_mem_size = self.dtype_size * (nhist + extra_space)
scan_args = (self.d_counters_sum,
self.d_counters,
Expand All @@ -119,21 +119,21 @@ def naive_scan(self, num):
def scan(self, dst, src, batch_size, array_length):
self.scan_local1(dst,
src,
batch_size * array_length / (4 * self.WORKGROUP_SIZE),
batch_size * array_length // (4 * self.WORKGROUP_SIZE),
4 * self.WORKGROUP_SIZE)
self.queue.finish()
self.scan_local2(dst,
src,
batch_size,
array_length / (4 * self.WORKGROUP_SIZE)
array_length // (4 * self.WORKGROUP_SIZE)
)
self.queue.finish()
self.scan_update(dst, batch_size * array_length / (4 * self.WORKGROUP_SIZE))
self.scan_update(dst, batch_size * array_length // (4 * self.WORKGROUP_SIZE))
self.queue.finish()


def scan_local1(self, dst, src, n, size):
global_size = (n * size / 4,)
global_size = (n * size // 4,)
local_size = (self.WORKGROUP_SIZE,)
scan_args = (dst,
src,
Expand Down Expand Up @@ -170,7 +170,7 @@ def scan_update(self, dst, n):
self.scan_prg.uniformUpdate(self.queue, global_size, local_size, *scan_args)

def reorder(self, d_key, d_val, startbit, num):
totalBlocks = num/2/self.cta_size
totalBlocks = num//2//self.cta_size
global_size = (self.cta_size*totalBlocks,)
local_size = (self.cta_size,)
reorder_args = (d_key,
Expand Down Expand Up @@ -209,11 +209,11 @@ def reorder(self, d_key, d_val, startbit, num):
from time import time
t = time()
s.sort(d_keys, d_vals, N)
print "%is" % ((time()-t)*1000)
print("%is" % ((time()-t)*1000))

cl.enqueue_copy(queue, keys, d_keys)
cl.enqueue_copy(queue, vals, d_vals)
queue.finish()

print np.linalg.norm(keys-sorted_keys)
print np.linalg.norm(vals-sorted_vals)
print(np.linalg.norm(keys-sorted_keys))
print(np.linalg.norm(vals-sorted_vals))

0 comments on commit c4d01cf

Please sign in to comment.