aboutsummaryrefslogtreecommitdiff
path: root/engine/src/core/com/jme3/post/HDRRenderer.java
blob: bac8334abf34927e09a351624296dba35c0c39b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (c) 2009-2010 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jme3.post;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.Vector2f;
import com.jme3.renderer.*;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.texture.FrameBuffer;
import com.jme3.texture.Image;
import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.MagFilter;
import com.jme3.texture.Texture.MinFilter;
import com.jme3.texture.Texture2D;
import com.jme3.ui.Picture;
import java.util.Collection;
import java.util.logging.Logger;

public class HDRRenderer implements SceneProcessor {

    private static final int LUMMODE_NONE = 0x1,
                             LUMMODE_ENCODE_LUM = 0x2,
                             LUMMODE_DECODE_LUM = 0x3;

    private Renderer renderer;
    private RenderManager renderManager;
    private ViewPort viewPort;
    private static final Logger logger = Logger.getLogger(HDRRenderer.class.getName());

    private Camera fbCam = new Camera(1, 1);

    private FrameBuffer msFB;

    private FrameBuffer mainSceneFB;
    private Texture2D mainScene;
    private FrameBuffer scene64FB;
    private Texture2D scene64;
    private FrameBuffer scene8FB;
    private Texture2D scene8;
    private FrameBuffer scene1FB[] = new FrameBuffer[2];
    private Texture2D scene1[] = new Texture2D[2];

    private Material hdr64;
    private Material hdr8;
    private Material hdr1;
    private Material tone;

    private Picture fsQuad;
    private float time = 0;
    private int curSrc = -1;
    private int oppSrc = -1;
    private float blendFactor = 0;

    private int numSamples = 0;
    private float exposure = 0.18f;
    private float whiteLevel = 100f;
    private float throttle = -1;
    private int maxIterations = -1;
    private Image.Format bufFormat = Format.RGB16F;

    private MinFilter fbMinFilter = MinFilter.BilinearNoMipMaps;
    private MagFilter fbMagFilter = MagFilter.Bilinear;
    private AssetManager manager;

    private boolean enabled = true;

    public HDRRenderer(AssetManager manager, Renderer renderer){
        this.manager = manager;
        this.renderer = renderer;
        
        Collection<Caps> caps = renderer.getCaps();
        if (caps.contains(Caps.PackedFloatColorBuffer))
            bufFormat = Format.RGB111110F;
        else if (caps.contains(Caps.FloatColorBuffer))
            bufFormat = Format.RGB16F;
        else{
            enabled = false;
            return;
        }
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setSamples(int samples){
        this.numSamples = samples;
    }

    public void setExposure(float exp){
        this.exposure = exp;
    }

    public void setWhiteLevel(float whiteLevel){
        this.whiteLevel = whiteLevel;
    }

    public void setMaxIterations(int maxIterations){
        this.maxIterations = maxIterations;

        // regenerate shaders if needed
        if (hdr64 != null)
            createLumShaders();
    }

    public void setThrottle(float throttle){
        this.throttle = throttle;
    }

    public void setUseFastFilter(boolean fastFilter){
        if (fastFilter){
            fbMagFilter = MagFilter.Nearest;
            fbMinFilter = MinFilter.NearestNoMipMaps;
        }else{
            fbMagFilter = MagFilter.Bilinear;
            fbMinFilter = MinFilter.BilinearNoMipMaps;
        }
    }

    public Picture createDisplayQuad(/*int mode, Texture tex*/){
        if (scene64 == null)
            return null;

        Material mat = new Material(manager, "Common/MatDefs/Hdr/LogLum.j3md");
//        if (mode == LUMMODE_ENCODE_LUM)
//            mat.setBoolean("EncodeLum", true);
//        else if (mode == LUMMODE_DECODE_LUM)
            mat.setBoolean("DecodeLum", true);
            mat.setTexture("Texture", scene64);
//        mat.setTexture("Texture", tex);
        
        Picture dispQuad = new Picture("Luminance Display");
        dispQuad.setMaterial(mat);
        return dispQuad;
    }

    private Material createLumShader(int srcW, int srcH, int bufW, int bufH, int mode,
                                int iters, Texture tex){
        Material mat = new Material(manager, "Common/MatDefs/Hdr/LogLum.j3md");
        
        Vector2f blockSize = new Vector2f(1f / bufW, 1f / bufH);
        Vector2f pixelSize = new Vector2f(1f / srcW, 1f / srcH);
        Vector2f blocks = new Vector2f();
        float numPixels = Float.POSITIVE_INFINITY;
        if (iters != -1){
            do {
                pixelSize.multLocal(2);
                blocks.set(blockSize.x / pixelSize.x,
                           blockSize.y / pixelSize.y);
                numPixels = blocks.x * blocks.y;
            } while (numPixels > iters);
        }else{
            blocks.set(blockSize.x / pixelSize.x,
                       blockSize.y / pixelSize.y);
            numPixels = blocks.x * blocks.y;
        }
        System.out.println(numPixels);

        mat.setBoolean("Blocks", true);
        if (mode == LUMMODE_ENCODE_LUM)
            mat.setBoolean("EncodeLum", true);
        else if (mode == LUMMODE_DECODE_LUM)
            mat.setBoolean("DecodeLum", true);

        mat.setTexture("Texture", tex);
        mat.setVector2("BlockSize", blockSize);
        mat.setVector2("PixelSize", pixelSize);
        mat.setFloat("NumPixels", numPixels);

        return mat;
    }

    private void createLumShaders(){
        int w = mainSceneFB.getWidth();
        int h = mainSceneFB.getHeight();
        hdr64 = createLumShader(w,  h,  64, 64, LUMMODE_ENCODE_LUM, maxIterations, mainScene);
        hdr8  = createLumShader(64, 64, 8,  8,  LUMMODE_NONE,       maxIterations, scene64);
        hdr1  = createLumShader(8,  8,  1,  1,  LUMMODE_NONE,       maxIterations, scene8);
    }

    private int opposite(int i){
        return i == 1 ? 0 : 1;
    }

    private void renderProcessing(Renderer r, FrameBuffer dst, Material mat){
        if (dst == null){
            fsQuad.setWidth(mainSceneFB.getWidth());
            fsQuad.setHeight(mainSceneFB.getHeight());
            fbCam.resize(mainSceneFB.getWidth(), mainSceneFB.getHeight(), true);
        }else{
            fsQuad.setWidth(dst.getWidth());
            fsQuad.setHeight(dst.getHeight());
            fbCam.resize(dst.getWidth(), dst.getHeight(), true);
        }
        fsQuad.setMaterial(mat);
        fsQuad.updateGeometricState();
        renderManager.setCamera(fbCam, true);

        r.setFrameBuffer(dst);
        r.clearBuffers(true, true, true);
        renderManager.renderGeometry(fsQuad);
    }

    private void renderToneMap(Renderer r, FrameBuffer out){
        tone.setFloat("A", exposure);
        tone.setFloat("White", whiteLevel);
        tone.setTexture("Lum", scene1[oppSrc]);
        tone.setTexture("Lum2", scene1[curSrc]);
        tone.setFloat("BlendFactor", blendFactor);
        renderProcessing(r, out, tone);
    }

    private void updateAverageLuminance(Renderer r){
        renderProcessing(r, scene64FB, hdr64);
        renderProcessing(r, scene8FB, hdr8);
        renderProcessing(r, scene1FB[curSrc], hdr1);
    }

    public boolean isInitialized(){
        return viewPort != null;
    }

    public void reshape(ViewPort vp, int w, int h){
        if (mainSceneFB != null){
            renderer.deleteFrameBuffer(mainSceneFB);
        }

        mainSceneFB = new FrameBuffer(w, h, 1);
        mainScene = new Texture2D(w, h, bufFormat);
        mainSceneFB.setDepthBuffer(Format.Depth);
        mainSceneFB.setColorTexture(mainScene);
        mainScene.setMagFilter(fbMagFilter);
        mainScene.setMinFilter(fbMinFilter);

        if (msFB != null){
            renderer.deleteFrameBuffer(msFB);
        }

        tone.setTexture("Texture", mainScene);
        
        Collection<Caps> caps = renderer.getCaps();
        if (numSamples > 1 && caps.contains(Caps.FrameBufferMultisample)){
            msFB = new FrameBuffer(w, h, numSamples);
            msFB.setDepthBuffer(Format.Depth);
            msFB.setColorBuffer(bufFormat);
            vp.setOutputFrameBuffer(msFB);
        }else{
            if (numSamples > 1)
                logger.warning("FBO multisampling not supported on this GPU, request ignored.");

            vp.setOutputFrameBuffer(mainSceneFB);
        }

        createLumShaders();
    }

    public void initialize(RenderManager rm, ViewPort vp){
        if (!enabled)
            return;

        renderer = rm.getRenderer();
        renderManager = rm;
        viewPort = vp;

        // loadInitial()
        fsQuad = new Picture("HDR Fullscreen Quad");

        Format lumFmt = Format.RGB8;
        scene64FB = new FrameBuffer(64, 64, 1);
        scene64 = new Texture2D(64, 64, lumFmt);
        scene64FB.setColorTexture(scene64);
        scene64.setMagFilter(fbMagFilter);
        scene64.setMinFilter(fbMinFilter);

        scene8FB = new FrameBuffer(8, 8, 1);
        scene8 = new Texture2D(8, 8, lumFmt);
        scene8FB.setColorTexture(scene8);
        scene8.setMagFilter(fbMagFilter);
        scene8.setMinFilter(fbMinFilter);

        scene1FB[0] = new FrameBuffer(1, 1, 1);
        scene1[0] = new Texture2D(1, 1, lumFmt);
        scene1FB[0].setColorTexture(scene1[0]);

        scene1FB[1] = new FrameBuffer(1, 1, 1);
        scene1[1] = new Texture2D(1, 1, lumFmt);
        scene1FB[1].setColorTexture(scene1[1]);

        // prepare tonemap shader
        tone = new Material(manager, "Common/MatDefs/Hdr/ToneMap.j3md");
        tone.setFloat("A", 0.18f);
        tone.setFloat("White", 100);

        // load();
        int w = vp.getCamera().getWidth();
        int h = vp.getCamera().getHeight();
        reshape(vp, w, h);

        
    }

    public void preFrame(float tpf) {
        if (!enabled)
            return;

        time += tpf;
        blendFactor = (time / throttle);
    }

    public void postQueue(RenderQueue rq) {
    }

    public void postFrame(FrameBuffer out) {
        if (!enabled)
            return;

        if (msFB != null){
            // first render to multisampled FB
//            renderer.setFrameBuffer(msFB);
//            renderer.clearBuffers(true,true,true);
//
//            renderManager.renderViewPortRaw(viewPort);

            // render back to non-multisampled FB
            renderer.copyFrameBuffer(msFB, mainSceneFB);
        }else{
//            renderer.setFrameBuffer(mainSceneFB);
//            renderer.clearBuffers(true,true,false);
//
//            renderManager.renderViewPortRaw(viewPort);
        }

        // should we update avg lum?
        if (throttle == -1){
            // update every frame
            curSrc = 0;
            oppSrc = 0;
            blendFactor = 0;
            time = 0;
            updateAverageLuminance(renderer);
        }else{
            if (curSrc == -1){
                curSrc = 0;
                oppSrc = 0;

                // initial update
                updateAverageLuminance(renderer);

                blendFactor = 0;
                time = 0;
            }else if (time > throttle){

                // time to switch
                oppSrc = curSrc;
                curSrc = opposite(curSrc);

                updateAverageLuminance(renderer);

                blendFactor = 0;
                time = 0;
            }
        }

        // since out == mainSceneFB, tonemap into the main screen instead
        //renderToneMap(renderer, out);
        renderToneMap(renderer, null);

        renderManager.setCamera(viewPort.getCamera(), false);
    }

    public void cleanup() {
        if (!enabled)
            return;

        if (msFB != null)
            renderer.deleteFrameBuffer(msFB);
        if (mainSceneFB != null)
            renderer.deleteFrameBuffer(mainSceneFB);
        if (scene64FB != null){
            renderer.deleteFrameBuffer(scene64FB);
            renderer.deleteFrameBuffer(scene8FB);
            renderer.deleteFrameBuffer(scene1FB[0]);
            renderer.deleteFrameBuffer(scene1FB[1]);
        }
    }

}