Project

General

Profile

« Previous | Next » 

Revision 07206c71

Added by Leszek Koltunski over 4 years ago

First attempt at Deferred Mesh Jobs.
Only apply(VertexEffect) supported for now.

View differences:

src/main/java/org/distorted/library/main/DistortedLibrary.java
35 35
import org.distorted.library.effect.FragmentEffect;
36 36
import org.distorted.library.effect.PostprocessEffect;
37 37
import org.distorted.library.effect.VertexEffect;
38
import org.distorted.library.effectqueue.EffectQueueVertex;
39
import org.distorted.library.mesh.DeferredJobs;
38 40
import org.distorted.library.mesh.MeshBase;
39 41
import org.distorted.library.message.EffectMessageSender;
40 42
import org.distorted.library.program.DistortedProgram;
......
475 477
///////////////////////////////////////////////////////////////////////////////////////////////////
476 478
// execute all VertexEffects and adjust all vertices
477 479

  
478
  public static void adjustVertices(MeshBase mesh)
480
  public static void adjustVertices(MeshBase mesh, EffectQueueVertex queue)
479 481
    {
480 482
    if( mFullProgram!=null )
481 483
      {
......
486 488

  
487 489
      mFullProgram.useProgram();
488 490
      mesh.bindVertexAttribs(mFullProgram);
489
      mesh.computeQueue();
490
      mesh.sendQueue();
491
      queue.compute(1);
492
      queue.send(0.0f,3);
491 493
      mesh.send(3);
492 494

  
493 495
      GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, tfo );
......
979 981
    DistortedEffects.onDestroy();
980 982
    EffectQueue.onDestroy();
981 983
    Effect.onDestroy();
984
    DeferredJobs.onDestroy();
982 985
    EffectMessageSender.stopSending();
983 986

  
984 987
    mInitialized = false;
src/main/java/org/distorted/library/mesh/DeferredJobs.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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
package org.distorted.library.mesh;
21

  
22
import org.distorted.library.effect.VertexEffect;
23
import org.distorted.library.effectqueue.EffectQueueVertex;
24
import org.distorted.library.main.DistortedLibrary;
25

  
26
import java.util.ArrayList;
27

  
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * Not part of public API, do not document (public only because has to be cleaned from the main package)
31
 *
32
 * @y.exclude
33
 */
34
public class DeferredJobs
35
  {
36
  private static final int JOB_TYPE_VERTEX = 0;
37
  private static final int JOB_TYPE_MERGE  = 1;
38
  private static final int JOB_TYPE_JOIN   = 2;
39
  private static final int JOB_TYPE_COPY   = 3;
40

  
41
  private static ArrayList<JobNode> mJobs = new ArrayList<>();
42

  
43
  //////////////////////////////////////////////////////////////////////////
44

  
45
  private static class Job
46
    {
47
    private int mType;
48
    private MeshBase mTarget;
49
    private MeshBase[] mSource;
50
    private EffectQueueVertex mEffects;
51

  
52
    Job(int type, MeshBase target, MeshBase[] source, VertexEffect effect)
53
      {
54
      mType   = type;
55
      mTarget = target;
56
      mSource = source;
57
      mEffects= new EffectQueueVertex();
58
      mEffects.add(effect);
59
      }
60

  
61
    Job(Job job)
62
      {
63
      mType   = job.mType;
64
      mTarget = job.mTarget;
65
      mSource = job.mSource;
66
      mEffects= new EffectQueueVertex(job.mEffects);
67
      }
68

  
69
    void addEffect(VertexEffect effect)
70
      {
71
      mEffects.add(effect);
72
      }
73

  
74
    void execute()
75
      {
76
      switch(mType)
77
        {
78
        case JOB_TYPE_VERTEX: DistortedLibrary.adjustVertices(mTarget, mEffects);
79
                              break;
80
        case JOB_TYPE_MERGE : break;
81
        case JOB_TYPE_JOIN  : break;
82
        case JOB_TYPE_COPY  : break;
83
        }
84
      }
85

  
86
    void clear()
87
      {
88
      mEffects.removeAll(false);
89
      }
90
    }
91

  
92
  //////////////////////////////////////////////////////////////////////////
93

  
94
  static class JobNode
95
    {
96
    private ArrayList<JobNode> mPrevJobs;
97
    private ArrayList<JobNode> mNextJobs;
98
    private Job mJob;
99

  
100
    JobNode(Job job)
101
      {
102
      mPrevJobs = new ArrayList<>();
103
      mNextJobs = new ArrayList<>();
104
      mJob      = job;
105
      }
106

  
107
    JobNode(JobNode node)
108
      {
109
      mPrevJobs = new ArrayList<>();
110
      mNextJobs = new ArrayList<>();
111
      mJob      = new Job(node.mJob);
112

  
113
      int numPrev = node.mPrevJobs.size();
114

  
115
      for(int i=0; i<numPrev; i++)
116
        {
117
        mPrevJobs.add(node.mPrevJobs.get(i));
118
        }
119

  
120
      int numNext = node.mNextJobs.size();
121

  
122
      for(int i=0; i<numNext; i++)
123
        {
124
        mNextJobs.add(node.mNextJobs.get(i));
125
        }
126
      }
127

  
128
    void execute()
129
      {
130
      JobNode node;
131
      int numPrev = mPrevJobs.size();
132

  
133
      for(int i=0; i<numPrev; i++)
134
        {
135
        node = mPrevJobs.get(i);
136
        node.execute();
137
        }
138

  
139
      removeNode(this);
140
      mJob.execute();
141
      }
142

  
143
    void clear()
144
      {
145
      mPrevJobs.clear();
146
      mPrevJobs = null;
147
      mNextJobs.clear();
148
      mNextJobs = null;
149

  
150
      mJob.clear();
151
      mJob = null;
152
      }
153
    }
154

  
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

  
157
  private static void removeNode(JobNode node)
158
    {
159
    mJobs.remove(node);
160
    JobNode jn;
161

  
162
    int numPrev = node.mPrevJobs.size();
163

  
164
    for(int i=0; i<numPrev; i++)
165
      {
166
      jn = node.mPrevJobs.get(i);
167
      jn.mNextJobs.remove(node);
168
      }
169

  
170
    int numNext = node.mNextJobs.size();
171

  
172
    for(int i=0; i<numNext; i++)
173
      {
174
      jn = node.mNextJobs.get(i);
175
      jn.mPrevJobs.remove(node);
176
      }
177

  
178
    node.mJob.mTarget.mJobNode = null;
179
    }
180

  
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

  
183
  static JobNode vertex(MeshBase target, VertexEffect effect)
184
    {
185
    JobNode jn = target.mJobNode;
186

  
187
    if( jn==null )
188
      {
189
      Job job = new Job(JOB_TYPE_VERTEX,target,null,effect);
190
      JobNode node = new JobNode(job);
191
      mJobs.add(node);
192
      return node;
193
      }
194
    else
195
      {
196
      if( jn.mJob.mType==JOB_TYPE_VERTEX )
197
        {
198
        jn.mJob.addEffect(effect);
199
        return jn;
200
        }
201
      else
202
        {
203
        Job job = new Job(JOB_TYPE_VERTEX,target,null,effect);
204
        JobNode node = new JobNode(job);
205
        node.mPrevJobs.add(jn);
206
        jn.mNextJobs.add(node);
207
        mJobs.add(node);
208
        return node;
209
        }
210
      }
211
    }
212

  
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

  
215
  static JobNode merge(MeshBase target)
216
    {
217
    return null;
218
    }
219

  
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

  
222
  static JobNode join(MeshBase target, MeshBase[] meshes)
223
    {
224
    return null;
225
    }
226

  
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

  
229
  static JobNode copy(MeshBase target, MeshBase mesh)
230
    {
231
    return null;
232
    }
233

  
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
/**
236
 * Only for use by the library itself.
237
 *
238
 * @y.exclude
239
 */
240
  public static void onDestroy()
241
    {
242
    int num = mJobs.size();
243

  
244
    for(int i=0; i<num; i++)
245
      {
246
      mJobs.get(i).clear();
247
      }
248

  
249
    mJobs.clear();
250
    }
251
  }
src/main/java/org/distorted/library/mesh/MeshBase.java
24 24

  
25 25
import org.distorted.library.effect.VertexEffect;
26 26
import org.distorted.library.effectqueue.EffectQueue;
27
import org.distorted.library.effectqueue.EffectQueueVertex;
28
import org.distorted.library.main.DistortedLibrary;
29 27
import org.distorted.library.main.InternalBuffer;
30 28
import org.distorted.library.program.DistortedProgram;
31 29
import org.distorted.library.type.Static4D;
32 30

  
33
import java.io.InputStream;
34
import java.io.OutputStream;
35 31
import java.nio.ByteBuffer;
36 32
import java.nio.ByteOrder;
37 33
import java.nio.FloatBuffer;
......
47 43
public abstract class MeshBase
48 44
   {
49 45
   private static final int MAX_COMPONENTS= 100;
50
   static final int DEFAULT_ASSOCIATION = 0xffffffff;
46
   private static final int DEFAULT_ASSOCIATION = 0xffffffff;
51 47

  
52 48
   // sizes of attributes of an individual vertex.
53 49
   private static final int POS_DATA_SIZE= 3; // vertex coordinates: x,y,z
......
84 80
   private float[] mVertAttribs1;             // packed: PosX,PosY,PosZ, NorX,NorY,NorZ, InfX,InfY,InfZ
85 81
   private float[] mVertAttribs2;             // packed: TexS,TexT, Component
86 82
   private float mInflate;
87
   private boolean[] mNeedsAdjustment;
88 83
   private int[] mAssociation;
89 84

  
85
   DeferredJobs.JobNode mJobNode;
86

  
90 87
   private static int[] mComponentAssociationH = new int[EffectQueue.MAIN_VARIANTS];
91
   private EffectQueueVertex mQueue;
92 88

  
93 89
   private static class Component
94 90
     {
......
130 126

  
131 127
     for(int i=0; i<MAX_COMPONENTS; i++) mAssociation[i] = DEFAULT_ASSOCIATION;
132 128

  
133
     mNeedsAdjustment = new boolean[1];
134
     mQueue = new EffectQueueVertex();
135

  
136 129
     mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
137 130
     mVBO2= new InternalBuffer(GLES30.GL_ARRAY_BUFFER             , GLES30.GL_STATIC_READ);
138 131
     mTFO = new InternalBuffer(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, GLES30.GL_STATIC_READ);
......
149 142

  
150 143
     int size = original.mComponent.size();
151 144
     mComponent = new ArrayList<>();
152
     mQueue = new EffectQueueVertex(original.mQueue);
153 145

  
154 146
     for(int i=0; i<size; i++)
155 147
       {
......
162 154

  
163 155
     if( deep )
164 156
       {
165
       mNeedsAdjustment = new boolean[1];
166
       mNeedsAdjustment[0] = original.mNeedsAdjustment[0];
157
       if( original.mJobNode!=null )
158
         {
159
         mJobNode = new DeferredJobs.JobNode(original.mJobNode);
160
         }
167 161

  
168 162
       mVBO1= new InternalBuffer(GLES30.GL_ARRAY_BUFFER, GLES30.GL_STATIC_READ);
169 163
       mVertAttribs1= new float[mNumVertices*VERT1_ATTRIBS];
......
172 166
       }
173 167
     else
174 168
       {
175
       mNeedsAdjustment = original.mNeedsAdjustment;
169
       mJobNode         = original.mJobNode;
176 170
       mVBO1            = original.mVBO1;
177 171
       mVertAttribs1    = original.mVertAttribs1;
178 172
       }
......
362 356
       }
363 357

  
364 358
     GLES30.glUnmapBuffer(GLES30.GL_TRANSFORM_FEEDBACK);
365

  
366
     mQueue.removeAll(false);
367
     }
368

  
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
/**
371
 * Not part of public API, do not document (public only because has to be used from the main package)
372
 *
373
 * @y.exclude
374
 */
375
   public void computeQueue()
376
     {
377
     mQueue.compute(1);
378
     }
379

  
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381
/**
382
 * Not part of public API, do not document (public only because has to be used from the main package)
383
 *
384
 * @y.exclude
385
 */
386
   public void sendQueue()
387
     {
388
     mQueue.send(0.0f,3);
389 359
     }
390 360

  
391 361
///////////////////////////////////////////////////////////////////////////////////////////////////
......
429 399
 */
430 400
   public void bindVertexAttribs(DistortedProgram program)
431 401
     {
432
     if( mNeedsAdjustment[0] )
402
     if( mJobNode!=null )
433 403
       {
434
       mNeedsAdjustment[0] = false;
435
       DistortedLibrary.adjustVertices(this);
404
       mJobNode.execute();  // this will set itself to null
436 405
       }
437 406

  
438 407
     int index1 = mVBO1.createImmediately(mNumVertices*VERT1_SIZE, mVertAttribs1);
......
485 454
     return mInflate;
486 455
     }
487 456

  
488
///////////////////////////////////////////////////////////////////////////////////////////////////
489
/**
490
 * Read this mesh from a InputStream.
491
 */
492
  void read(final InputStream stream)
493
    {
494

  
495
    }
496

  
497 457
///////////////////////////////////////////////////////////////////////////////////////////////////
498 458
// PUBLIC API
499 459
///////////////////////////////////////////////////////////////////////////////////////////////////
500
/**
501
 * Save the Mesh to a OutputStream.
502
 */
503
  public void save(final OutputStream stream)
504
    {
505

  
506
    }
507

  
508
///////////////////////////////////////////////////////////////////////////////////////////////////
509

  
510 460
/**
511 461
 * When rendering this Mesh, do we want to render the Normal vectors as well?
512 462
 * <p>
......
558 508
 */
559 509
   public void apply(VertexEffect effect)
560 510
     {
561
     mQueue.add(effect);
562
     mNeedsAdjustment[0] = true;
511
     mJobNode = DeferredJobs.vertex(this,effect);
563 512
     }
564 513

  
565 514
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff