Project

General

Profile

« Previous | Next » 

Revision a13dde77

Added by Leszek Koltunski over 6 years ago

Progress with a more generic 'preprocess' stage of Postprocessing Effects.

View differences:

src/main/java/org/distorted/library/main/Distorted.java
89 89
   */
90 90
  public static final int FBO_QUEUE_SIZE = 4;
91 91
  /**
92
   * Number of Main program variants (ATM 2: main and main OIT)
92
   * Number of Main program variants (ATM 3: MAIN, MAIN OIT, PREPROCESS)
93 93
   */
94
  public static final int MAIN_VARIANTS = 2;
94
  public static final int MAIN_VARIANTS = 3;
95 95

  
96 96
  private static boolean mInitialized=false;
97 97

  
......
189 189
      exception = ex;
190 190
      }
191 191

  
192
    try
193
      {
194
      EffectQueuePostprocess.createPrograms(resources);
195
      }
196
    catch(Exception ex)
197
      {
198
      exception = ex;
199
      }
200

  
192 201
    try
193 202
      {
194 203
      PostprocessEffect.createPrograms();
src/main/java/org/distorted/library/main/DistortedEffects.java
443 443

  
444 444
///////////////////////////////////////////////////////////////////////////////////////////////////
445 445

  
446
  void drawPrivOIT(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
446
  void send(float halfW, float halfH, float halfZ, float margin, DistortedOutputSurface surface, int variant)
447
    {
448
    mM.send(surface,halfW,halfH,halfZ,margin,variant);
449
    mV.send(variant);
450
    }
451

  
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453

  
454
  void drawPrivOIT(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
447 455
    {
448 456
    float halfZ = halfW*mesh.zFactor;
449 457

  
......
465 473
    GLES31.glVertexAttribPointer(mMainOITProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
466 474
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
467 475

  
468
    mM.send(surface,halfW,halfH,halfZ,marginInPixels,1);
476
    mM.send(surface,halfW,halfH,halfZ,0,1);
469 477
    mV.send(1);
470 478
    mF.send(1);
471 479

  
......
476 484

  
477 485
///////////////////////////////////////////////////////////////////////////////////////////////////
478 486

  
479
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
487
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
480 488
    {
481 489
    float halfZ = halfW*mesh.zFactor;
482 490

  
......
496 504
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
497 505
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
498 506

  
499
    mM.send(surface,halfW,halfH,halfZ,marginInPixels,0);
507
    mM.send(surface,halfW,halfH,halfZ,0,0);
500 508
    mV.send(0);
501 509
    mF.send(0);
502 510

  
src/main/java/org/distorted/library/main/DistortedNode.java
239 239
    if( mParent!=null ) mParent.adjustIsomorphism();
240 240
    }
241 241

  
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

  
244
  int markStencilAndDepth(long currTime, DistortedOutputSurface surface, EffectQueuePostprocess queue)
245
    {
246
    DistortedSurface input = mNumChildren[0]==0 ? mSurface : mData.mFBO;
247

  
248
    if( input.setAsInput() )
249
      {
250
      DistortedRenderState.setUpStencilMark();
251
      mEffects.drawPriv(mSurface.getWidth() /2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, queue.getHalo()*surface.mMipmap);
252
      DistortedRenderState.unsetUpStencilMark();
253

  
254
      return 1;
255
      }
256
    return 0;
257
    }
258

  
259 242
///////////////////////////////////////////////////////////////////////////////////////////////////
260 243
// return the total number of render calls issued
261 244

  
......
267 250
      {
268 251
      mState.apply();
269 252
      GLES31.glDisable(GLES31.GL_BLEND);
270
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
253
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
271 254
      GLES31.glEnable(GLES31.GL_BLEND);
272 255
      return 1;
273 256
      }
......
285 268
    if( input.setAsInput() )
286 269
      {
287 270
      mState.apply();
288
      mEffects.drawPrivOIT(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
271
      mEffects.drawPrivOIT(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
289 272
      return 1;
290 273
      }
291 274

  
......
302 285
    if( input.setAsInput() )
303 286
      {
304 287
      mState.apply();
305
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime, 0);
288
      mEffects.drawPriv(mSurface.getWidth()/2.0f, mSurface.getHeight()/2.0f, mMesh, surface, currTime);
306 289
      return 1;
307 290
      }
308 291

  
......
737 720

  
738 721
///////////////////////////////////////////////////////////////////////////////////////////////////
739 722
/**
740
 * Returns the DistortedInputSurface object that's in the Node.
723
 * Returns the DistortedSurface object that's in the Node.
741 724
 *
742
 * @return The DistortedInputSurface contained in the Node.
725
 * @return The DistortedSurface contained in the Node.
743 726
 */
744 727
  public DistortedSurface getSurface()
745 728
    {
746 729
    return mSurface;
747 730
    }
748 731

  
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733
  /**
734
   * Returns the DistortedSurface object that's in the Node.
735
   *
736
   * @return The DistortedSurface contained in the Node (if a leaf), or the FBO (if an internal Node)
737
   */
738
  public DistortedSurface getInternalSurface()
739
    {
740
    return mNumChildren[0]==0 ? mSurface : mData.mFBO;
741
    }
742

  
749 743
///////////////////////////////////////////////////////////////////////////////////////////////////
750 744
/**
751 745
 * Returns the Mesh object that's in the Node.
src/main/java/org/distorted/library/main/DistortedOutputSurface.java
460 460
            }
461 461
          else
462 462
            {
463
            for(int j=bucketChange; j<i; j++) numRenders += lastQueue.preprocess( mBuffer,children.get(j), time);
463
            for(int j=bucketChange; j<i; j++) numRenders += lastQueue.preprocess( mBuffer,children.get(j) );
464 464
            numRenders += lastQueue.postprocess(mBuffer);
465 465

  
466 466
            if( oit )
......
484 484

  
485 485
        if( i==numChildren-1 )
486 486
          {
487
          for(int j=bucketChange; j<numChildren; j++) numRenders += currQueue.preprocess(mBuffer,children.get(j), time);
487
          for(int j=bucketChange; j<numChildren; j++) numRenders += currQueue.preprocess( mBuffer,children.get(j) );
488 488
          numRenders += currQueue.postprocess(mBuffer);
489 489

  
490 490
          if( oit )
src/main/java/org/distorted/library/main/DistortedRenderState.java
459 459

  
460 460
///////////////////////////////////////////////////////////////////////////////////////////////////
461 461

  
462
  static void setUpStencilMark()
462
  static void setUpStencilMark(boolean color)
463 463
    {
464 464
    sState.stencilTest = cState.stencilTest;
465 465

  
......
501 501
    sState.colorMaskB = cState.colorMaskB;
502 502
    sState.colorMaskA = cState.colorMaskA;
503 503

  
504
    if( cState.colorMaskR!=0 || cState.colorMaskG!=0 || cState.colorMaskB!=0 || cState.colorMaskA!=0 )
504
    int clr = color ? 1:0;
505

  
506
    if( cState.colorMaskR!=clr || cState.colorMaskG!=clr || cState.colorMaskB!=clr || cState.colorMaskA!=clr )
505 507
      {
506
      cState.colorMaskR = 0;
507
      cState.colorMaskG = 0;
508
      cState.colorMaskB = 0;
509
      cState.colorMaskA = 0;
508
      cState.colorMaskR = clr;
509
      cState.colorMaskG = clr;
510
      cState.colorMaskB = clr;
511
      cState.colorMaskA = clr;
510 512
      //android.util.Log.d("State", "switch off color writing");
511
      GLES31.glColorMask(false,false,false,false);
513
      GLES31.glColorMask(color,color,color,color);
512 514
      }
513 515

  
514 516
    sState.depthMask = cState.depthMask;
src/main/java/org/distorted/library/main/DistortedScreen.java
128 128

  
129 129
    if( mShowFPS && fpsTexture.setAsInput())
130 130
      {
131
      fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time, 0);
131
      fpsEffects.drawPriv(fpsW / 2.0f, fpsH / 2.0f, fpsMesh, this, time);
132 132
      }
133 133

  
134 134
    if( ++mCurRenderedFBO>=Distorted.FBO_QUEUE_SIZE )
src/main/java/org/distorted/library/main/EffectQueuePostprocess.java
19 19

  
20 20
package org.distorted.library.main;
21 21

  
22
import android.content.res.Resources;
22 23
import android.opengl.GLES31;
24
import android.util.Log;
23 25

  
26
import org.distorted.library.R;
24 27
import org.distorted.library.effect.EffectType;
25 28
import org.distorted.library.effect.PostprocessEffect;
29
import org.distorted.library.effect.VertexEffect;
26 30
import org.distorted.library.message.EffectMessage;
31
import org.distorted.library.program.DistortedProgram;
32

  
33
import java.io.InputStream;
27 34

  
28 35
///////////////////////////////////////////////////////////////////////////////////////////////////
29 36

  
......
34 41

  
35 42
  private int mHalo;
36 43

  
44
  static DistortedProgram mPreProgram;
45
  static int mPreColorH;
46

  
37 47
///////////////////////////////////////////////////////////////////////////////////////////////////
38 48

  
39 49
  EffectQueuePostprocess(long id)
......
78 88
    }
79 89

  
80 90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
  /**
92
   * Only for use by the library itself.
93
   *
94
   * @y.exclude
95
   */
96
  public static void createPrograms(Resources resources)
97
    {
98
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
99
    final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
100

  
101
    int numV = VertexEffect.getNumEnabled();
102

  
103
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? DistortedEffects.getMax(EffectType.VERTEX  ) : 0 ) + "\n");
104
    String mainFragHeader= Distorted.GLSL_VERSION + "\n";
105

  
106
    String enabledEffectV= VertexEffect.getGLSL();
107

  
108
    try
109
      {
110
      mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
111
                                         enabledEffectV, null, Distorted.GLSL, null);
112
      }
113
    catch(Exception e)
114
      {
115
      Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
116
      throw new RuntimeException(e.getMessage());
117
      }
118

  
119
    int preProgramH = mPreProgram.getProgramHandle();
120
    EffectQueueVertex.getUniforms( preProgramH,2 );
121
    EffectQueueMatrix.getUniforms( preProgramH,2 );
122
    mPreColorH= GLES31.glGetUniformLocation( preProgramH, "u_Color");
123
    }
124

  
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
// TODO
81 127

  
82
  int getHalo()
128
  private boolean getWriteColor()
83 129
    {
84
    return mNumEffects>0 ? mHalo : 0;
130
    return false;
85 131
    }
86 132

  
87 133
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// TODO  (now only really works in case of 1 effect!)
89 134

  
90
  int getQuality()
135
  private int getHalo()
91 136
    {
92
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
137
    return mNumEffects>0 ? mHalo : 0;
93 138
    }
94 139

  
95 140
///////////////////////////////////////////////////////////////////////////////////////////////////
......
101 146
    }
102 147

  
103 148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// TODO  (now only really works in case of 1 effect!)
104 150

  
105
  int preprocess(DistortedOutputSurface[] buffers, DistortedNode node, long time)
151
  int getQuality()
152
    {
153
    return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
154
    }
155

  
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

  
158
  int preprocess(DistortedOutputSurface[] buffers, DistortedNode node)
106 159
    {
107
    int numRenders = 0;
108 160
    int quality = getInternalQuality();
161
    DistortedOutputSurface surface = buffers[quality];
162

  
163
    surface.setAsOutput();
164
    DistortedSurface input = node.getInternalSurface();
109 165

  
110
    buffers[quality].setAsOutput();
166
    if( input.setAsInput() )
167
      {
168
      MeshObject    mesh = node.getMesh();
169
      boolean writeColor = getWriteColor();
170
      float       margin = getHalo()*surface.mMipmap;
111 171

  
112
    numRenders += node.markStencilAndDepth(time,buffers[quality],this);
172
      float halfW = input.getWidth() / 2.0f;
173
      float halfH = input.getHeight()/ 2.0f;
174
      float halfZ = halfW*mesh.zFactor;
113 175

  
114
    return numRenders;
176
      DistortedRenderState.setUpStencilMark(writeColor);
177

  
178
      GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
179

  
180
      mPreProgram.useProgram();
181

  
182
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
183
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
184
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
185
      GLES31.glVertexAttribPointer(mPreProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
186
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
187

  
188
      node.getEffects().send(halfW, halfH, halfZ, margin, surface, 2);
189

  
190
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
191

  
192
      DistortedRenderState.unsetUpStencilMark();
193

  
194
      return 1;
195
      }
196
    return 0;
115 197
    }
116 198

  
117 199
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/library/program/DistortedProgram.java
348 348
 *
349 349
 * @y.exclude
350 350
 */
351
  public DistortedProgram(final InputStream vertex, final InputStream fragment, final String vertexHeader, final String fragmentHeader, int glslVersion, final String[] feedback )
352
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
351
  public DistortedProgram(final InputStream vert, final InputStream frag, final String vertHeader, final String fragHeader,
352
                          int glslVersion, final String[] feedback )
353
  throws FragmentCompilationException,VertexCompilationException,LinkingException
353 354
    {
354 355
    init(glslVersion);
355 356

  
356
    final String vertexShader   = readTextFileFromRawResource(vertex  , true );
357
    final String fragmentShader = readTextFileFromRawResource(fragment, false);
357
    final String vertShader = readTextFileFromRawResource(vert, true );
358
    final String fragShader = readTextFileFromRawResource(frag, false);
358 359

  
359
    final int vertexShaderHandle   = compileShader(GLES31.GL_VERTEX_SHADER  , vertexHeader   + vertexShader  );
360
    final int fragmentShaderHandle = compileShader(GLES31.GL_FRAGMENT_SHADER, fragmentHeader + fragmentShader);
360
    final int vertShaderHandle = compileShader(GLES31.GL_VERTEX_SHADER  , vertHeader + vertShader);
361
    final int fragShaderHandle = compileShader(GLES31.GL_FRAGMENT_SHADER, fragHeader + fragShader);
361 362

  
362
    mProgramHandle = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, mAttributeName, glslVersion>= 300 ? feedback:null );
363
    mProgramHandle = createAndLinkProgram(vertShaderHandle, fragShaderHandle, mAttributeName, glslVersion>= 300 ? feedback:null );
363 364

  
364 365
    mAttribute = new int[mNumAttributes];
365 366

  
......
387 388
 *
388 389
 * @y.exclude
389 390
 */
390
  public DistortedProgram(final InputStream vertex, final InputStream fragment, final String vertexHeader, final String fragmentHeader,
391
                          final String enabledVertex, final String enabledFragment, int glslVersion, final String[] feedback )
392
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
391
  public DistortedProgram(final InputStream vert, final InputStream frag, final String vertHeader, final String fragHeader,
392
                          final String enabledVert, final String enabledFrag, int glslVersion, final String[] feedback )
393
  throws FragmentCompilationException,VertexCompilationException,LinkingException
393 394
    {
394 395
    init(glslVersion);
395 396

  
396
    String vertexShader   = readTextFileFromRawResource(vertex  , true );
397
    String fragmentShader = readTextFileFromRawResource(fragment, false);
397
    String vertShader = readTextFileFromRawResource( vert, true );
398
    String fragShader = readTextFileFromRawResource( frag, false);
398 399

  
399
    vertexShader   = insertEnabledEffects(vertexShader  ,enabledVertex  );
400
    fragmentShader = insertEnabledEffects(fragmentShader,enabledFragment);
400
    if( enabledVert!=null ) vertShader = insertEnabledEffects(vertShader,enabledVert);
401
    if( enabledFrag!=null ) fragShader = insertEnabledEffects(fragShader,enabledFrag);
401 402

  
402
    final int vertexShaderHandle   = compileShader(GLES31.GL_VERTEX_SHADER  , vertexHeader   + vertexShader  );
403
    final int fragmentShaderHandle = compileShader(GLES31.GL_FRAGMENT_SHADER, fragmentHeader + fragmentShader);
403
    final int vertShaderHandle = compileShader(GLES31.GL_VERTEX_SHADER  , vertHeader + vertShader);
404
    final int fragShaderHandle = compileShader(GLES31.GL_FRAGMENT_SHADER, fragHeader + fragShader);
404 405

  
405
    mProgramHandle = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, mAttributeName, glslVersion>= 300 ? feedback:null );
406
    mProgramHandle = createAndLinkProgram(vertShaderHandle, fragShaderHandle, mAttributeName, glslVersion>= 300 ? feedback:null );
406 407

  
407 408
    mAttribute = new int[mNumAttributes];
408 409

  
src/main/res/raw/preprocess_fragment_shader.glsl
1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4
// This file is part of Distorted.                                                          //
5
//                                                                                          //
6
// Distorted is free software: you can redistribute it and/or modify                        //
7
// it under the terms of the GNU General Public License as published by                     //
8
// the Free Software Foundation, either version 2 of the License, or                        //
9
// (at your option) any later version.                                                      //
10
//                                                                                          //
11
// Distorted is distributed in the hope that it will be useful,                             //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                           //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            //
14
// GNU General Public License for more details.                                             //
15
//                                                                                          //
16
// You should have received a copy of the GNU General Public License                        //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18
//////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
precision highp float;
21

  
22
out vec4 fragColor;
23
uniform vec4 u_Color;
24

  
25
//////////////////////////////////////////////////////////////////////////////////////////////
26

  
27
void main()
28
  {
29
  fragColor = u_Color;
30
  }

Also available in: Unified diff