Jogamp
GLProfile.initSingleton(false) for webstart compatibility.
[jocl-demos.git] / src / com / jogamp / opencl / demos / julia3d / Julia3d.java
CommitLineData
2c85c416
MB
1package com.jogamp.opencl.demos.julia3d;
2
18d1f903
MB
3import java.awt.event.WindowEvent;
4import java.awt.event.WindowAdapter;
5import java.awt.Canvas;
6import java.awt.Dimension;
7import javax.swing.JFrame;
2c85c416
MB
8import com.jogamp.opencl.CLBuffer;
9import com.jogamp.opencl.CLCommandQueue;
10import com.jogamp.opencl.CLContext;
11import com.jogamp.opencl.CLDevice;
12import com.jogamp.opencl.CLKernel;
13import com.jogamp.opencl.CLPlatform;
14import com.jogamp.opencl.CLProgram;
15import com.jogamp.opencl.demos.julia3d.structs.Camera;
16import com.jogamp.opencl.demos.julia3d.structs.RenderingConfig;
17import com.jogamp.opencl.demos.julia3d.structs.Vec;
f386e227
MB
18import java.io.IOException;
19import java.nio.Buffer;
20import java.nio.ByteBuffer;
21import java.nio.FloatBuffer;
40d4bec7 22import javax.media.opengl.GLProfile;
f386e227
MB
23import javax.swing.SwingUtilities;
24
18d1f903
MB
25import static com.jogamp.opencl.CLDevice.Type.*;
26import static com.jogamp.opencl.util.CLPlatformFilters.*;
2c85c416
MB
27import static com.jogamp.opencl.CLMemory.Mem.*;
28import static com.jogamp.opencl.CLProgram.CompilerOptions.*;
29import static com.jogamp.opencl.demos.julia3d.UserSceneController.*;
f386e227
MB
30
31/**
32 * This sample has been ported from David Buciarelli's juliaGPU v1.2 written in C.
33 * @author Michael Bien
34 */
35public class Julia3d {
36
37 private final CLContext context;
38 private CLBuffer<FloatBuffer> pixelBuffer;
39 private final CLBuffer<ByteBuffer> configBuffer;
40 private final CLCommandQueue commandQueue;
41 private final CLProgram program;
42 private final CLKernel julia;
43 private final CLKernel multiply;
44
45 private final int workGroupSize;
46 private final String kernelFileName = "rendering_kernel.cl";
47
48 final RenderingConfig config;
49
50 private Julia3d(RenderingConfig renderConfig) {
51 this.config = renderConfig;
52 updateCamera();
53
37401c35 54 //setup, prefere GPUs
18d1f903 55 CLDevice device = CLPlatform.getDefault(type(GPU)).getMaxFlopsDevice();
37401c35
MB
56 if(device == null) {
57 device = CLPlatform.getDefault().getMaxFlopsDevice();
58 }
59 context = CLContext.create(device);
f386e227 60
18d1f903 61 workGroupSize = Math.min(256, device.getMaxWorkGroupSize());
f386e227
MB
62
63 //allocate buffers
df1138e7 64 configBuffer = context.createBuffer(config.getBuffer(), READ_ONLY);
37401c35 65 commandQueue = device.createCommandQueue();
f386e227
MB
66// update(true);
67
68 try {
69 program = context.createProgram(Julia3d.class.getResourceAsStream(kernelFileName))
70 .build(FAST_RELAXED_MATH);
71 } catch (IOException ex) {
72 throw new RuntimeException("unable to load program from source", ex);
73 }
74
75 julia = program.createCLKernel("JuliaGPU");
76 multiply = program.createCLKernel("multiply");
37401c35 77 System.out.println(program.getBuildStatus(device));
f386e227
MB
78 System.out.println(program.getBuildLog());
79
80 }
81
82 void update(boolean reallocate) {
83
84 updateCamera();
85
86 int bufferSize = config.getWidth() * config.getHeight() * 3;
87 if(reallocate) {
88 if(pixelBuffer != null) {
89 pixelBuffer.release();
90 }
91
92 pixelBuffer = context.createFloatBuffer(bufferSize, READ_WRITE, USE_BUFFER);
93 }
94
95 commandQueue.putWriteBuffer(configBuffer, true);
96
97 julia.putArg(pixelBuffer)
98 .putArg(configBuffer)
99 .rewind();
100
101 multiply.putArg(pixelBuffer)
102 .putArg(bufferSize)
103 .rewind();
104 }
105
106
107 void compute(boolean fastRendering) {
108
109 // calculate workgroup size
110 int globalThreads = config.getWidth() * config.getHeight();
111 if(globalThreads % workGroupSize != 0)
112 globalThreads = (globalThreads / workGroupSize + 1) * workGroupSize;
113
114 int localThreads = workGroupSize;
115 int superSamplingSize = config.getSuperSamplingSize();
116
117 if (!fastRendering && superSamplingSize > 1) {
118
119 for (int y = 0; y < superSamplingSize; ++y) {
120 for (int x = 0; x < superSamplingSize; ++x) {
121
122 float sampleX = (x + 0.5f) / superSamplingSize;
123 float sampleY = (y + 0.5f) / superSamplingSize;
124
125 if (x == 0 && y == 0) {
126 // First pass
127 julia.setArg(2, 0)
128 .setArg(3, sampleX)
129 .setArg(4, sampleY);
130
131 commandQueue.put1DRangeKernel(julia, 0, globalThreads, localThreads);
132
133 } else if (x == (superSamplingSize - 1) && y == (superSamplingSize - 1)) {
134 // Last pass
135 julia.setArg(2, 1)
136 .setArg(3, sampleX)
137 .setArg(4, sampleY);
138
139 // normalize the values we accumulated
140 multiply.setArg(2, 1.0f/(superSamplingSize*superSamplingSize));
141
142 commandQueue.put1DRangeKernel(julia, 0, globalThreads, localThreads)
143 .put1DRangeKernel(multiply, 0, globalThreads*3, localThreads);
144 } else {
145 julia.setArg(2, 1)
146 .setArg(3, sampleX)
147 .setArg(4, sampleY);
148
149 commandQueue.put1DRangeKernel(julia, 0, globalThreads, localThreads);
150
151 }
152 }
153 }
154
155 }else{
156
157 //fast rendering
158 julia.setArg(2, 0)
159 .setArg(3, 0.0f)
160 .setArg(4, 0.0f);
161
162 commandQueue.put1DRangeKernel(julia, 0, globalThreads, localThreads);
163 }
164
165 commandQueue.putBarrier()
166 .putReadBuffer(pixelBuffer, true);
167
168 }
169
170 private void updateCamera() {
171
172 Camera camera = config.getCamera();
173
174 Vec dir = camera.getDir();
175 Vec target = camera.getTarget();
176 Vec camX = camera.getX();
177 Vec camY = camera.getY();
178 Vec orig = camera.getOrig();
179
180 vsub(dir, target, orig);
181 vnorm(dir);
182
183 Vec up = Vec.create().setX(0).setY(1).setZ(0);
184 vxcross(camX, dir, up);
185 vnorm(camX);
186 vmul(camX, config.getWidth() * .5135f / config.getHeight(), camX);
187
188 vxcross(camY, camX, dir);
189 vnorm(camY);
190 vmul(camY, .5135f, camY);
191 }
192
37401c35
MB
193 CLDevice getDevice() {
194 return commandQueue.getDevice();
195 }
196
f386e227
MB
197
198 public static void main(String[] args) {
18d1f903 199
d068770b
MB
200 //false for webstart compatibility
201 GLProfile.initSingleton(false);
18d1f903
MB
202
203 final RenderingConfig config = RenderingConfig.create()
f386e227
MB
204 .setWidth(640).setHeight(480)
205 .setEnableShadow(1)
206 .setSuperSamplingSize(2)
207 .setActvateFastRendering(1)
208 .setMaxIterations(9)
209 .setEpsilon(0.003f * 0.75f)
210 .setLight(new float[] {5, 10, 15})
211 .setMu(new float[] {-0.2f, 0.4f, -0.4f, -0.4f});
212
213 config.getCamera().getOrig() .setX(1).setY(2).setZ(8);
214 config.getCamera().getTarget().setX(0).setY(0).setZ(0);
215
216 final Julia3d julia3d = new Julia3d(config);
217
218 SwingUtilities.invokeLater(new Runnable() {
219 public void run() {
18d1f903
MB
220
221 Renderer renderer = new Renderer(julia3d);
222 CLDevice device = julia3d.getDevice();
223
224 JFrame frame = new JFrame("Java OpenCL - Julia3D "+device.getType()+" "+device.getName());
225 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
226 frame.addWindowListener(new WindowAdapter() {
227 @Override
228 public void windowClosed(WindowEvent e) {
229 julia3d.release();
230 System.exit(0);
231 }
232 });
233 Canvas canvas = renderer.getCanvas();
234 canvas.setPreferredSize(new Dimension(config.getWidth(), config.getHeight()));
235 frame.add(canvas);
236 frame.pack();
237 frame.setVisible(true);
238
f386e227
MB
239 }
240 });
241 }
242
243 Buffer getPixelBuffer() {
244 return pixelBuffer.getBuffer();
245 }
246
37401c35
MB
247 void release() {
248 context.release();
249 }
250
f386e227
MB
251
252}
http://JogAmp.org git info: FAQ, tutorial and man pages.