diff options
author | Adrian Kummerlaender | 2020-01-20 22:40:44 +0100 |
---|---|---|
committer | Adrian Kummerlaender | 2020-01-20 22:40:44 +0100 |
commit | abbdbe64d12af732983bc11aac772d3d32682bb5 (patch) | |
tree | fb89bd9818e33588cf295036c6feb681b92d0ff4 | |
parent | b0b52925fb96e417802f4b390fafc15c8536df2d (diff) | |
download | firmament-abbdbe64d12af732983bc11aac772d3d32682bb5.tar firmament-abbdbe64d12af732983bc11aac772d3d32682bb5.tar.gz firmament-abbdbe64d12af732983bc11aac772d3d32682bb5.tar.bz2 firmament-abbdbe64d12af732983bc11aac772d3d32682bb5.tar.lz firmament-abbdbe64d12af732983bc11aac772d3d32682bb5.tar.xz firmament-abbdbe64d12af732983bc11aac772d3d32682bb5.tar.zst firmament-abbdbe64d12af732983bc11aac772d3d32682bb5.zip |
Extract OpenCL setup into Renderer
-rw-r--r-- | bluedot.py | 32 | ||||
-rw-r--r-- | fancy_local_sunrise.py | 36 | ||||
-rw-r--r-- | firmament/__init__.py | 3 | ||||
-rw-r--r-- | firmament/planets.py (renamed from planets.py) | 0 | ||||
-rw-r--r-- | firmament/raymarch.cl (renamed from raymarch.cl) | 0 | ||||
-rw-r--r-- | firmament/renderer.py | 51 | ||||
-rw-r--r-- | firmament/sun.py (renamed from sun.py) | 0 | ||||
-rw-r--r-- | local_sunrise.py | 36 | ||||
-rw-r--r-- | sunrise.py | 34 |
9 files changed, 78 insertions, 114 deletions
@@ -1,13 +1,8 @@ import numpy import matplotlib.pyplot as plt -from string import Template -import pyopencl as cl -from pyopencl.cltypes import make_double3 - -mf = cl.mem_flags - -from planets import earth +from firmament import Renderer +from firmament.planets import earth config = { 'size_x': 1000, @@ -25,29 +20,12 @@ config = { sun_range = (0, 360, 10) -cl_platform = cl.get_platforms()[0] -cl_context = cl.Context(properties=[(cl.context_properties.PLATFORM, cl_platform)]) -cl_queue = cl.CommandQueue(cl_context) - -cl_picture = cl.Buffer(cl_context, mf.WRITE_ONLY, size=config['size_x']*config['size_y']*3*numpy.float64(0).nbytes) -program = None - -with open('raymarch.cl') as f: - program = cl.Program(cl_context, Template(f.read()).substitute( - {**config, **earth} - )).build() +renderer = Renderer(config, earth) for i in numpy.arange(*sun_range): - sun = make_double3(numpy.cos(i*2*numpy.pi/360),numpy.sin(i*2*numpy.pi/360),0) + sun = (numpy.cos(i*2*numpy.pi/360), numpy.sin(i*2*numpy.pi/360), 0) print(sun) - program.render_pinhole( - cl_queue, (config['size_x'], config['size_y']), None, cl_picture, - make_double3(*(config['eye_pos'] * earth['earth_radius'])), - make_double3(*(config['eye_dir'] * earth['earth_radius'])), - sun) - - np_picture = numpy.ndarray(shape=(config['size_y'], config['size_x'], 3), dtype=numpy.float64) - cl.enqueue_copy(cl_queue, np_picture, cl_picture).wait(); + np_picture = renderer.render_pinhole(sun) plt.imsave("bluedot_%05.1f.png" % i, np_picture, origin='lower') diff --git a/fancy_local_sunrise.py b/fancy_local_sunrise.py index 39cb292..bdb23b4 100644 --- a/fancy_local_sunrise.py +++ b/fancy_local_sunrise.py @@ -1,17 +1,12 @@ import numpy as np import matplotlib.pyplot as plt -from string import Template -import pyopencl as cl -from pyopencl.cltypes import make_double3 - -mf = cl.mem_flags - -from planets import earth - -from sun import sun_direction from datetime import datetime +from firmament import Renderer +from firmament.planets import earth +from firmament.sun import sun_direction + config = { 'size_x': 1000, 'size_y': 1000, @@ -35,37 +30,20 @@ config = { time_range = (6, 20, 1) -cl_platform = cl.get_platforms()[0] -cl_context = cl.Context(properties=[(cl.context_properties.PLATFORM, cl_platform)]) -cl_queue = cl.CommandQueue(cl_context) - -cl_picture = cl.Buffer(cl_context, mf.WRITE_ONLY, size=config['size_x']*config['size_y']*3*np.float64(0).nbytes) -program = None - -with open('raymarch.cl') as f: - program = cl.Program(cl_context, Template(f.read()).substitute( - {**config, **earth} - )).build() +renderer = Renderer(config, earth) for time in np.arange(*time_range): pit = datetime(*config['date'], int(np.floor(time)), int((time-np.floor(time))*60), 0) sun_dir = sun_direction(config['latitude'], config['longitude'], pit, config['timezone'], 1.0 if config['summertime'] else 0.0) - sun = make_double3( + sun = ( np.cos(sun_dir[0])*np.sin(sun_dir[1]), np.cos(sun_dir[0])*np.cos(sun_dir[1]), np.sin(sun_dir[0]) ) print(sun_dir) - program.render_fisheye( - cl_queue, (config['size_x'], config['size_y']), None, cl_picture, - make_double3(*(config['eye_pos'] * earth['earth_radius'])), - make_double3(*(config['eye_dir'] * earth['earth_radius'])), - sun) - - np_picture = np.ndarray(shape=(config['size_y'], config['size_x'], 3), dtype=np.float64) - cl.enqueue_copy(cl_queue, np_picture, cl_picture).wait(); + np_picture = renderer.render_fisheye(sun) fig = plt.gcf() diff --git a/firmament/__init__.py b/firmament/__init__.py new file mode 100644 index 0000000..26d56ec --- /dev/null +++ b/firmament/__init__.py @@ -0,0 +1,3 @@ +from . import renderer + +Renderer = renderer.Renderer diff --git a/planets.py b/firmament/planets.py index 4570019..4570019 100644 --- a/planets.py +++ b/firmament/planets.py diff --git a/raymarch.cl b/firmament/raymarch.cl index d0092d9..d0092d9 100644 --- a/raymarch.cl +++ b/firmament/raymarch.cl diff --git a/firmament/renderer.py b/firmament/renderer.py new file mode 100644 index 0000000..d020cdb --- /dev/null +++ b/firmament/renderer.py @@ -0,0 +1,51 @@ +import numpy +from string import Template + +import pyopencl as cl +from pyopencl.cltypes import make_double3 + +mf = cl.mem_flags + +class Renderer: + def __init__(self, config, planet): + self.config = config + self.planet = planet + + self.cl_platform = cl.get_platforms()[0] + self.cl_context = cl.Context(properties=[(cl.context_properties.PLATFORM, self.cl_platform)]) + self.cl_queue = cl.CommandQueue(self.cl_context) + + self.cl_picture = cl.Buffer(self.cl_context, mf.WRITE_ONLY, size=self.config['size_x']*self.config['size_y']*3*numpy.float64(0).nbytes) + + with open('firmament/raymarch.cl') as f: + self.program = cl.Program(self.cl_context, Template(f.read()).substitute( + {**self.config, **self.planet} + )).build() + + def render_pinhole(self, sun): + self.program.render_pinhole( + self.cl_queue, + (self.config['size_x'], self.config['size_y']), + None, + self.cl_picture, + make_double3(*(self.config['eye_pos'] * self.planet['earth_radius'])), + make_double3(*(self.config['eye_dir'] * self.planet['earth_radius'])), + make_double3(*sun)) + + np_picture = numpy.ndarray(shape=(self.config['size_y'], self.config['size_x'], 3), dtype=numpy.float64) + cl.enqueue_copy(self.cl_queue, np_picture, self.cl_picture).wait(); + return np_picture + + def render_fisheye(self, sun): + self.program.render_fisheye( + self.cl_queue, + (self.config['size_x'], self.config['size_y']), + None, + self.cl_picture, + make_double3(*(self.config['eye_pos'] * self.planet['earth_radius'])), + make_double3(*(self.config['eye_dir'] * self.planet['earth_radius'])), + make_double3(*sun)) + + np_picture = numpy.ndarray(shape=(self.config['size_y'], self.config['size_x'], 3), dtype=numpy.float64) + cl.enqueue_copy(self.cl_queue, np_picture, self.cl_picture).wait(); + return np_picture diff --git a/sun.py b/firmament/sun.py index 7ceec5d..7ceec5d 100644 --- a/sun.py +++ b/firmament/sun.py diff --git a/local_sunrise.py b/local_sunrise.py index 7032847..b413579 100644 --- a/local_sunrise.py +++ b/local_sunrise.py @@ -1,17 +1,12 @@ import numpy as np import matplotlib.pyplot as plt -from string import Template -import pyopencl as cl -from pyopencl.cltypes import make_double3 - -mf = cl.mem_flags - -from planets import earth - -from sun import sun_direction from datetime import datetime +from firmament import Renderer +from firmament.planets import earth +from firmament.sun import sun_direction + fish_eye = False config = { @@ -37,36 +32,19 @@ config = { time_range = (6, 20, 0.5) -cl_platform = cl.get_platforms()[0] -cl_context = cl.Context(properties=[(cl.context_properties.PLATFORM, cl_platform)]) -cl_queue = cl.CommandQueue(cl_context) - -cl_picture = cl.Buffer(cl_context, mf.WRITE_ONLY, size=config['size_x']*config['size_y']*3*np.float64(0).nbytes) -program = None - -with open('raymarch.cl') as f: - program = cl.Program(cl_context, Template(f.read()).substitute( - {**config, **earth} - )).build() +renderer = Renderer(config, earth) for time in np.arange(*time_range): pit = datetime(*config['date'], int(np.floor(time)), int((time-np.floor(time))*60), 0) sun_dir = sun_direction(config['latitude'], config['longitude'], pit, config['timezone'], 1.0 if config['summertime'] else 0.0) - sun = make_double3( + sun =( np.cos(sun_dir[0])*np.sin(sun_dir[1]), np.cos(sun_dir[0])*np.cos(sun_dir[1]), np.sin(sun_dir[0]) ) print(sun_dir) - (program.render_fisheye if fish_eye else program.render_pinhole)( - cl_queue, (config['size_x'], config['size_y']), None, cl_picture, - make_double3(*(config['eye_pos'] * earth['earth_radius'])), - make_double3(*(config['eye_dir'] * earth['earth_radius'])), - sun) - - np_picture = np.ndarray(shape=(config['size_y'], config['size_x'], 3), dtype=np.float64) - cl.enqueue_copy(cl_queue, np_picture, cl_picture).wait(); + np_picture = (renderer.render_fisheye if fish_eye else renderer.render_pinhole)(sun) plt.imsave("sky_%05.1f.png" % time, np_picture, origin='lower') @@ -1,13 +1,8 @@ import numpy import matplotlib.pyplot as plt -from string import Template -import pyopencl as cl -from pyopencl.cltypes import make_double3 - -mf = cl.mem_flags - -from planets import earth +from firmament import Renderer +from firmament.planets import earth config = { 'size_x': 1920//4, @@ -25,31 +20,12 @@ config = { sun_range = (-10, 90, 10) -cl_platform = cl.get_platforms()[0] -cl_context = cl.Context(properties=[(cl.context_properties.PLATFORM, cl_platform)]) -cl_queue = cl.CommandQueue(cl_context) - -cl_picture = cl.Buffer(cl_context, mf.WRITE_ONLY, size=config['size_x']*config['size_y']*3*numpy.float64(0).nbytes) -program = None - -print('height: %d' % (earth['earth_radius']*config['eye_pos'][2] - earth['earth_radius'])) - -with open('raymarch.cl') as f: - program = cl.Program(cl_context, Template(f.read()).substitute( - {**config, **earth} - )).build() +renderer = Renderer(config, earth) for i in numpy.arange(*sun_range): - sun = make_double3(0.0,numpy.cos(i*2*numpy.pi/360),numpy.sin(i*2*numpy.pi/360)) + sun = (0.0, numpy.cos(i*2*numpy.pi/360), numpy.sin(i*2*numpy.pi/360)) print(sun) - program.render_pinhole( - cl_queue, (config['size_x'], config['size_y']), None, cl_picture, - make_double3(*(config['eye_pos'] * earth['earth_radius'])), - make_double3(*(config['eye_dir'] * earth['earth_radius'])), - sun) - - np_picture = numpy.ndarray(shape=(config['size_y'], config['size_x'], 3), dtype=numpy.float64) - cl.enqueue_copy(cl_queue, np_picture, cl_picture).wait(); + np_picture = renderer.render_pinhole(sun) plt.imsave("sky_%05.1f.png" % (i-sun_range[0]), np_picture, origin='lower') |