Project

General

Profile

Download (8.29 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / mali / MaliRenderer.java @ 29b5cd0d

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
package org.distorted.examples.mali;
21

    
22
import android.opengl.GLES31;
23
import android.opengl.GLSurfaceView;
24

    
25
import java.nio.ByteBuffer;
26
import java.nio.ByteOrder;
27
import java.nio.FloatBuffer;
28

    
29
import javax.microedition.khronos.egl.EGLConfig;
30
import javax.microedition.khronos.opengles.GL10;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
class MaliRenderer implements GLSurfaceView.Renderer
35
{
36
    private int mWidth, mHeight;
37

    
38
    private static int[] mSSBO = new int[1];
39
    private static int mProgramHandle,mSizeH,mPosH;
40
    private static final FloatBuffer mQuadPositions;
41

    
42
    static
43
      {
44
      mSSBO[0] = -1;
45

    
46
      float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
47
      mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
48
      mQuadPositions.put(positionData).position(0);
49
      }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
    MaliRenderer(GLSurfaceView v)
54
      {
55

    
56
      }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
    public void onDrawFrame(GL10 glUnused) 
61
      {
62
      GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0);
63
      GLES31.glUseProgram(mProgramHandle);
64
      GLES31.glEnableVertexAttribArray(mPosH);
65
      GLES31.glViewport(0, 0, mWidth, mHeight );
66
      GLES31.glUniform2ui(mSizeH, mWidth, mHeight);
67
      GLES31.glVertexAttribPointer(mPosH, 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
68
      GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
69
      }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
    
73
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
74
      {
75
      final String vertSource =
76

    
77
      "#version 310 es                                      \n" +
78
      "precision highp float;                               \n" +
79
      "precision highp int;                                 \n" +
80
      "in vec2 a_Position;                                  \n" +
81
      "out vec2 v_Pixel;                                    \n" +
82
      "uniform uvec2 u_Size;                                \n" +
83

    
84
      "void main()                                          \n" +
85
      "{                                                    \n" +
86
      "v_Pixel         = (a_Position + 0.5) * vec2(u_Size); \n" +
87
      "gl_Position     = vec4(2.0*a_Position,1.0,1.0);      \n" +
88
      "} ";
89

    
90
      final String fragSource =
91

    
92
      "#version 310 es                                      \n" +
93
      "precision highp float;                               \n" +
94
      "precision highp int;                                 \n" +
95
      "out vec4 fragColor;                                  \n" +
96
      "in vec2 v_Pixel;                                     \n" +
97
      "uniform uvec2 u_Size;                                \n" +
98

    
99
      "layout (std430,binding=1) buffer linkedlist          \n" +
100
      " {                                                   \n" +
101
      " uint u_Records[];                                   \n" +
102
      " };                                                  \n" +
103

    
104
      "void main()                                                 \n" +
105
      " {                                                          \n" +
106
      " uint index= uint(v_Pixel.x) + uint(v_Pixel.y) * u_Size.x;  \n" +
107
      " uint sum = 0u;                                             \n" +
108

    
109
      " if( index>=1u )                                            \n" +
110
      "   {                                                        \n" +
111
      "   //for(uint i=index; i>=index-1u; i--) sum += u_Records[i]; \n" +
112
      "   sum += u_Records[index];                               \n" +
113
      "   sum += u_Records[index-1u];                            \n" +
114
      "   }                                                        \n" +
115

    
116
      " u_Records[index] = sum;                                    \n" +
117

    
118
      " fragColor = vec4(0.0,1.0,0.0,1.0);                         \n" +
119
      " }";
120

    
121
      int vertHandle = GLES31.glCreateShader(GLES31.GL_VERTEX_SHADER);
122
      GLES31.glShaderSource(vertHandle, vertSource);
123
      GLES31.glCompileShader(vertHandle);
124

    
125
      final int[] compileStatus = new int[1];
126
      GLES31.glGetShaderiv(vertHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
127

    
128
      if (compileStatus[0] != GLES31.GL_TRUE)
129
        {
130
        String error = GLES31.glGetShaderInfoLog(vertHandle);
131
        android.util.Log.e("Program", "error compiling vert :" + error);
132
        GLES31.glDeleteShader(vertHandle);
133
        }
134

    
135
      int fragHandle = GLES31.glCreateShader(GLES31.GL_FRAGMENT_SHADER);
136
      GLES31.glShaderSource(fragHandle, fragSource);
137
      GLES31.glCompileShader(fragHandle);
138

    
139
      GLES31.glGetShaderiv(fragHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
140

    
141
      if (compileStatus[0] != GLES31.GL_TRUE)
142
        {
143
        String error = GLES31.glGetShaderInfoLog(fragHandle);
144
        android.util.Log.e("Program", "error compiling frag :" + error);
145
        GLES31.glDeleteShader(fragHandle);
146
        }
147

    
148
      mProgramHandle = GLES31.glCreateProgram();
149

    
150
      if (mProgramHandle != 0)
151
        {
152
        GLES31.glAttachShader(mProgramHandle, vertHandle);
153
        GLES31.glAttachShader(mProgramHandle, fragHandle);
154

    
155
        GLES31.glBindAttribLocation(mProgramHandle, 0, "a_Position");
156

    
157
        GLES31.glLinkProgram(mProgramHandle);
158

    
159
        final int[] linkStatus = new int[1];
160
        GLES31.glGetProgramiv(mProgramHandle, GLES31.GL_LINK_STATUS, linkStatus, 0);
161

    
162
        if (linkStatus[0] != GLES31.GL_TRUE)
163
          {
164
          String error = GLES31.glGetProgramInfoLog(mProgramHandle);
165
          GLES31.glDeleteProgram(mProgramHandle);
166
          android.util.Log.e("Program", "error linking :" + error);
167
          }
168
        }
169

    
170
      mPosH = GLES31.glGetAttribLocation ( mProgramHandle, "a_Position");
171
      mSizeH= GLES31.glGetUniformLocation( mProgramHandle, "u_Size");
172

    
173
      if( mSSBO[0]<0 )
174
        {
175
        GLES31.glGenBuffers(1,mSSBO,0);
176
        GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
177
        GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, (1<<23) , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
178
        GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mSSBO[0]);
179
        GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
180
        }
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
186
      {
187
      mWidth = width;
188
      mHeight= height;
189
      }
190
}
(2-2/3)