tinydisplay: Convert Python scripts to Python 3

This commit is contained in:
rdb 2026-01-28 22:54:23 +01:00
parent 08454e3686
commit 712d67414f
2 changed files with 69 additions and 68 deletions

64
panda/src/tinydisplay/store_pixel.py Normal file → Executable file
View File

@ -48,68 +48,68 @@ def getFname(op_a, op_b, mask):
# We write the code that actually instantiates the various
# pixel-storing functions to store_pixel_code.h.
code = open('store_pixel_code.h', 'wb')
print >> code, '/* This file is generated code--do not edit. See store_pixel.py. */'
print >> code, ''
code = open('store_pixel_code.h', 'w')
print('/* This file is generated code--do not edit. See store_pixel.py. */', file=code)
print('', file=code)
# The external reference for the table containing the above function
# pointers gets written here.
table = open('store_pixel_table.h', 'wb')
print >> table, '/* This file is generated code--do not edit. See store_pixel.py. */'
print >> table, ''
table = open('store_pixel_table.h', 'w')
print('/* This file is generated code--do not edit. See store_pixel.py. */', file=table)
print('', file=table)
for op_a in Operands:
for op_b in Operands:
for mask in range(0, 16):
fname = getFname(op_a, op_b, mask)
print >> code, '#define FNAME(name) %s' % (fname)
print('#define FNAME(name) %s' % (fname), file=code)
if mask & (1 | 2 | 3):
print >> code, '#define FNAME_S(name) %s_s' % (fname)
print('#define FNAME_S(name) %s_s' % (fname), file=code)
print >> code, '#define OP_A(f, i) ((unsigned int)(%s))' % (CodeTable[op_a])
print >> code, '#define OP_B(f, i) ((unsigned int)(%s))' % (CodeTable[op_b])
print('#define OP_A(f, i) ((unsigned int)(%s))' % (CodeTable[op_a]), file=code)
print('#define OP_B(f, i) ((unsigned int)(%s))' % (CodeTable[op_b]), file=code)
for b in range(0, 4):
if (mask & (1 << b)):
print >> code, "#define STORE_PIXEL_%s(fr, r) STORE_PIX_CLAMP(r)" % (b)
print("#define STORE_PIXEL_%s(fr, r) STORE_PIX_CLAMP(r)" % (b), file=code)
else:
print >> code, "#define STORE_PIXEL_%s(fr, r) (fr)" % (b)
print >> code, '#include "store_pixel.h"'
print >> code, ''
print("#define STORE_PIXEL_%s(fr, r) (fr)" % (b), file=code)
print('#include "store_pixel.h"', file=code)
print('', file=code)
# Now, generate the table of function pointers.
arraySize = '[%s][%s][16]' % (len(Operands), len(Operands))
print >> table, 'extern const ZB_storePixelFunc store_pixel_funcs%s;' % (arraySize)
print >> code, 'const ZB_storePixelFunc store_pixel_funcs%s = {' % (arraySize)
print('extern const ZB_storePixelFunc store_pixel_funcs%s;' % (arraySize), file=table)
print('const ZB_storePixelFunc store_pixel_funcs%s = {' % (arraySize), file=code)
for op_a in Operands:
print >> code, ' {'
print(' {', file=code)
for op_b in Operands:
print >> code, ' {'
print(' {', file=code)
for mask in range(0, 16):
fname = getFname(op_a, op_b, mask)
print >> code, ' %s,' % (fname)
print >> code, ' },'
print >> code, ' },'
print >> code, '};'
print(' %s,' % (fname), file=code)
print(' },', file=code)
print(' },', file=code)
print('};', file=code)
print >> code
print('', file=code)
# Now do this again, but for the sRGB function pointers.
print >> table, 'extern const ZB_storePixelFunc store_pixel_funcs_sRGB%s;' % (arraySize)
print >> code, 'const ZB_storePixelFunc store_pixel_funcs_sRGB%s = {' % (arraySize)
print('extern const ZB_storePixelFunc store_pixel_funcs_sRGB%s;' % (arraySize), file=table)
print('const ZB_storePixelFunc store_pixel_funcs_sRGB%s = {' % (arraySize), file=code)
for op_a in Operands:
print >> code, ' {'
print(' {', file=code)
for op_b in Operands:
print >> code, ' {'
print(' {', file=code)
for mask in range(0, 16):
fname = getFname(op_a, op_b, mask)
if mask & (1 | 2 | 3):
print >> code, ' %s_s,' % (fname)
print(' %s_s,' % (fname), file=code)
else:
print >> code, ' %s,' % (fname)
print >> code, ' },'
print >> code, ' },'
print >> code, '};'
print(' %s,' % (fname), file=code)
print(' },', file=code)
print(' },', file=code)
print('};', file=code)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
""" This simple Python script can be run to generate
ztriangle_code_*.h, ztriangle_table.*, and ztriangle_*.cxx, which
are a poor man's form of generated code to cover the explosion of
@ -34,6 +34,7 @@ Options = [
]
# The total number of different combinations of the various Options, above.
from functools import reduce
OptionsCount = reduce(lambda a, b: a * b, map(lambda o: len(o), Options))
# The various combinations of these options are explicit within
@ -99,7 +100,7 @@ ZTriangleStub = """
"""
ops = [0] * len(Options)
class DoneException:
class DoneException(Exception):
pass
# We write the code that actually instantiates the various
@ -148,11 +149,11 @@ def getFref(ops):
def closeCode():
""" Close the previously-opened code file. """
if code:
print >> code, ''
print >> code, 'ZB_fillTriangleFunc ztriangle_code_%s[%s] = {' % (codeSeg, len(fnameList))
print('', file=code)
print('ZB_fillTriangleFunc ztriangle_code_%s[%s] = {' % (codeSeg, len(fnameList)), file=code)
for fname in fnameList:
print >> code, ' %s,' % (fname)
print >> code, '};'
print(' %s,' % (fname), file=code)
print('};', file=code)
code.close()
@ -173,13 +174,13 @@ def openCode(count):
fnameList = []
# Open a new file.
code = open('ztriangle_code_%s.h' % (codeSeg), 'wb')
print >> code, '/* This file is generated code--do not edit. See ztriangle.py. */'
print >> code, ''
code = open('ztriangle_code_%s.h' % (codeSeg), 'w')
print('/* This file is generated code--do not edit. See ztriangle.py. */', file=code)
print('', file=code)
# Also generate ztriangle_*.cxx, to include the above file.
zt = open('ztriangle_%s.cxx' % (codeSeg), 'wb')
print >> zt, ZTriangleStub % (codeSeg)
zt = open('ztriangle_%s.cxx' % (codeSeg), 'w')
print(ZTriangleStub % (codeSeg), file=zt)
# First, generate the code.
count = 0
@ -189,14 +190,14 @@ try:
for i in range(len(ops)):
keyword = Options[i][ops[i]]
print >> code, CodeTable[keyword]
print(CodeTable[keyword], file=code)
# This reference gets just the initial fname: omitting the
# ExtraOptions, which are implicit in ztriangle_two.h.
fname = getFname(ops)
print >> code, '#define FNAME(name) %s_ ## name' % (fname)
print >> code, '#include "ztriangle_two.h"'
print >> code, ''
print('#define FNAME(name) %s_ ## name' % (fname), file=code)
print('#include "ztriangle_two.h"', file=code)
print('', file=code)
# We store the full fnames generated by the above lines
# (including the ExtraOptions) in the fnameDict and fnameList
@ -220,22 +221,22 @@ closeCode()
# The external reference for the table containing the above function
# pointers gets written here.
table_decl = open('ztriangle_table.h', 'wb')
print >> table_decl, '/* This file is generated code--do not edit. See ztriangle.py. */'
print >> table_decl, ''
table_decl = open('ztriangle_table.h', 'w')
print('/* This file is generated code--do not edit. See ztriangle.py. */', file=table_decl)
print('', file=table_decl)
# The actual table definition gets written here.
table_def = open('ztriangle_table.cxx', 'wb')
print >> table_def, '/* This file is generated code--do not edit. See ztriangle.py. */'
print >> table_def, ''
print >> table_def, '#include "pandabase.h"'
print >> table_def, '#include "zbuffer.h"'
print >> table_def, '#include "ztriangle_table.h"'
print >> table_def, ''
table_def = open('ztriangle_table.cxx', 'w')
print('/* This file is generated code--do not edit. See ztriangle.py. */', file=table_def)
print('', file=table_def)
print('#include "pandabase.h"', file=table_def)
print('#include "zbuffer.h"', file=table_def)
print('#include "ztriangle_table.h"', file=table_def)
print('', file=table_def)
for i in range(NumSegments):
print >> table_def, 'extern ZB_fillTriangleFunc ztriangle_code_%s[];' % (i + 1)
print >> table_def, ''
print('extern ZB_fillTriangleFunc ztriangle_code_%s[];' % (i + 1), file=table_def)
print('', file=table_def)
def writeTableEntry(ops):
indent = ' ' * (len(ops) + 1)
@ -245,27 +246,27 @@ def writeTableEntry(ops):
if i + 1 == len(FullOptions):
# The last level: write out the actual function names.
for j in range(numOps - 1):
print >> table_def, indent + getFref(ops + [j]) + ','
print >> table_def, indent + getFref(ops + [numOps - 1])
print(indent + getFref(ops + [j]) + ',', file=table_def)
print(indent + getFref(ops + [numOps - 1]), file=table_def)
else:
# Intermediate levels: write out a nested reference.
for j in range(numOps - 1):
print >> table_def, indent + '{'
print(indent + '{', file=table_def)
writeTableEntry(ops + [j])
print >> table_def, indent + '},'
print >> table_def, indent + '{'
print(indent + '},', file=table_def)
print(indent + '{', file=table_def)
writeTableEntry(ops + [numOps - 1])
print >> table_def, indent + '}'
print(indent + '}', file=table_def)
arraySizeList = []
for opList in FullOptions:
arraySizeList.append('[%s]' % (len(opList)))
arraySize = ''.join(arraySizeList)
print >> table_def, 'const ZB_fillTriangleFunc fill_tri_funcs%s = {' % (arraySize)
print >> table_decl, 'extern const ZB_fillTriangleFunc fill_tri_funcs%s;' % (arraySize)
print('const ZB_fillTriangleFunc fill_tri_funcs%s = {' % (arraySize), file=table_def)
print('extern const ZB_fillTriangleFunc fill_tri_funcs%s;' % (arraySize), file=table_decl)
writeTableEntry([])
print >> table_def, '};'
print('};', file=table_def)