Project

General

Profile

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

examples / src / main / java / org / distorted / examples / mali / MaliRenderer.java @ 58ce3a2f

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
      " for(uint i=index+1u; i>=index; i--) sum += u_Records[i];   \n" +
110
      " //sum += u_Records[index+1u];                              \n" +
111
      " //sum += u_Records[index];                                 \n" +
112
      " u_Records[index] = sum;                                    \n" +
113

    
114
      " fragColor = vec4(0.0,1.0,0.0,1.0);                         \n" +
115
      " }";
116

    
117
      int vertHandle = GLES31.glCreateShader(GLES31.GL_VERTEX_SHADER);
118
      GLES31.glShaderSource(vertHandle, vertSource);
119
      GLES31.glCompileShader(vertHandle);
120

    
121
      final int[] compileStatus = new int[1];
122
      GLES31.glGetShaderiv(vertHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
123

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

    
131
      int fragHandle = GLES31.glCreateShader(GLES31.GL_FRAGMENT_SHADER);
132
      GLES31.glShaderSource(fragHandle, fragSource);
133
      GLES31.glCompileShader(fragHandle);
134

    
135
      GLES31.glGetShaderiv(fragHandle, GLES31.GL_COMPILE_STATUS, compileStatus, 0);
136

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

    
144
      mProgramHandle = GLES31.glCreateProgram();
145

    
146
      if (mProgramHandle != 0)
147
        {
148
        GLES31.glAttachShader(mProgramHandle, vertHandle);
149
        GLES31.glAttachShader(mProgramHandle, fragHandle);
150

    
151
        GLES31.glBindAttribLocation(mProgramHandle, 0, "a_Position");
152

    
153
        GLES31.glLinkProgram(mProgramHandle);
154

    
155
        final int[] linkStatus = new int[1];
156
        GLES31.glGetProgramiv(mProgramHandle, GLES31.GL_LINK_STATUS, linkStatus, 0);
157

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

    
166
      mPosH = GLES31.glGetAttribLocation ( mProgramHandle, "a_Position");
167
      mSizeH= GLES31.glGetUniformLocation( mProgramHandle, "u_Size");
168

    
169
      if( mSSBO[0]<0 )
170
        {
171
        GLES31.glGenBuffers(1,mSSBO,0);
172
        GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, mSSBO[0]);
173
        GLES31.glBufferData(GLES31.GL_SHADER_STORAGE_BUFFER, (1<<23) , null, GLES31.GL_DYNAMIC_READ|GLES31.GL_DYNAMIC_DRAW);
174
        GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, mSSBO[0]);
175
        GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0);
176
        }
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
182
      {
183
      mWidth = width;
184
      mHeight= height;
185
      }
186
}
(2-2/3)