• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-cqtcocoa誰得pythonphprubygameguibathyscaphec翻訳計画中(planning stage)omegatframeworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

A categorical programming language


Commit MetaInfo

修订版760c2afb3a71d7855552fd8472af14402510c3cc (tree)
时间2021-10-26 05:46:26
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

cammy-run: JIT-compile per-pixel drawings.

更改概述

差异

--- a/cammy-run/default.nix
+++ b/cammy-run/default.nix
@@ -1,7 +1,8 @@
11 { nixpkgs ? import <nixpkgs> {} }:
22 let
33 inherit (nixpkgs.pkgs)
4- fetchFromGitLab stdenv pypy pypyPackages pkg-config stb;
4+ fetchFromGitLab stdenv
5+ pypy pypyPackages pkg-config libffi stb;
56 # https://foss.heptapod.net/pypy/pypy/
67 pypySrc = fetchFromGitLab {
78 domain = "foss.heptapod.net";
@@ -17,7 +18,11 @@ stdenv.mkDerivation {
1718
1819 src = ./.;
1920
20- buildInputs = [ pypy pypyPackages.py pypySrc pkg-config stb ];
21+ buildInputs = [
22+ # always required
23+ pypy pypyPackages.py pypySrc pkg-config stb
24+ # only required for JIT
25+ pypyPackages.pytest libffi ];
2126
2227 buildPhase = ''
2328 source $stdenv/setup
@@ -25,7 +30,7 @@ stdenv.mkDerivation {
2530 cp -r ${pypySrc}/rpython .
2631 chmod -R u+w rpython/
2732 # Do the actual translation.
28- ${pypy}/bin/pypy -mrpython -O2 main.py
33+ ${pypy}/bin/pypy -mrpython -Ojit main.py
2934 '';
3035
3136 installPhase = ''
--- a/cammy-run/main.py
+++ b/cammy-run/main.py
@@ -1,5 +1,16 @@
1+# Monkey-patch the stdlib. Borrowed from Typhon.
2+from ctypes import util
3+fl = util.find_library
4+def patched_find_library(name):
5+ if name == "c":
6+ return "libc.so.6"
7+ else:
8+ return fl(name)
9+util.find_library = patched_find_library
10+
111 import math, sys
212
13+from rpython.rlib.jit import JitDriver, set_param
314 from rpython.rlib.parsing.main import make_parser_from_file
415 from rpython.rlib.parsing.tree import RPythonVisitor
516 from rpython.rlib.rfile import create_stdio
@@ -342,30 +353,55 @@ def scale(bot, top, x):
342353 def finishChannel(c):
343354 return int(255 * max(0.0, min(1.0, c)))
344355
345-def drawPNG(program, filename, window, width, height):
346- pixelRadius = 0.000001
347- size = width * height
348- iw = 1.0 / width
349- dw = 0.5 * iw
350- ih = 1.0 / height
351- dh = 0.5 * ih
352- channels = 3
356+class Window(object):
357+ _immutable_ = True
358+
359+ def __init__(self, corners, width, height):
360+ self._corners = corners[0], corners[1], corners[2], corners[3]
361+ self._w = width
362+ self._h = height
363+
364+ def coordsForPixel(self, i):
365+ w = i % self._w
366+ h = i // self._w
367+ iw = 1.0 / self._w
368+ dw = 0.5 * iw
369+ ih = 1.0 / self._h
370+ dh = 0.5 * ih
371+ c1 = scale(self._corners[0], self._corners[2], dw + iw * w)
372+ c2 = scale(self._corners[1], self._corners[3], dh + ih * h)
373+ return c1, c2
374+
375+driver = JitDriver(greens=["size", "program", "window"],
376+ reds=["pixel", "stringbuilder"])
377+
378+def drawPixels(size, program, window):
353379 sb = StringBuilder()
354- for i in range(size):
355- w = i % width
356- h = i // width
357- c1 = scale(window[0], window[2], dw + iw * w)
358- c2 = scale(window[0], window[2], dh + ih * h)
380+ i = 0
381+ while i < size:
382+ driver.jit_merge_point(size=size, program=program, window=window,
383+ pixel=i, stringbuilder=sb)
384+ c1, c2 = window.coordsForPixel(i)
359385 rgb = program.run(P(F(c1), F(c2)))
360386 r = finishChannel(rgb.first().f())
361387 g = finishChannel(rgb.second().first().f())
362388 b = finishChannel(rgb.second().second().f())
363389 sb.append(chr(r) + chr(g) + chr(b))
364- buf = sb.build()
390+ i += 1
391+ return sb.build()
392+
393+def drawPNG(program, filename, corners, width, height):
394+ pixelRadius = 0.000001
395+ window = Window(corners, width, height)
396+ size = width * height
397+ buf = drawPixels(size, program, window)
398+ channels = 3
365399 stb.i_write_png(filename, width, height, channels, buf, width * channels)
366400
367401
368402 def main(argv):
403+ set_param(driver, "trace_limit", 50001)
404+
369405 prog = argv[1]
370406 window = [string_to_float(s) for s in split(argv[2])]
371407 width = int(argv[3])
--- a/shell.nix
+++ b/shell.nix
@@ -25,5 +25,7 @@ in pkgs.stdenv.mkDerivation {
2525 keychain mktorrent
2626 # experimenting with GLSL
2727 mesa-demos
28+ # comparing prototypes
29+ sloccount
2830 ];
2931 }