Subversion Repositories Kolibri OS

Rev

Rev 4473 | Rev 4495 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4473 Rev 4485
Line 14... Line 14...
14
#include 
14
#include 
15
#include 
15
#include 
Line 16... Line 16...
16
 
16
 
17
EGLImageKHR px_create_image(EGLDisplay display, EGLContext context,
17
EGLImageKHR px_create_image(EGLDisplay display, EGLContext context,
-
 
18
			 int width, int height, int stride, int name);
-
 
19
GLuint create_framebuffer(int width, int height, GLuint *tex);
Line 18... Line 20...
18
			 int width, int height, int stride, int name);
20
GLint create_shader(GLenum type, const char *source);
19
 
21
 
20
int main()
22
int main()
21
{
23
{
Line 30... Line 32...
30
    EGLImageKHR fb_image;
32
    EGLImageKHR fb_image;
31
    EGLConfig config;
33
    EGLConfig config;
Line 32... Line 34...
32
 
34
 
33
    EGLint config_attribs[32];
35
    EGLint config_attribs[32];
34
    EGLint num_configs, i;
36
    EGLint num_configs, i;
35
    GLint list;
37
    GLuint texture, buffer, front;
36
    GLuint texture, buffer;
-
 
Line 37... Line 38...
37
 
38
    GLuint f_tex;
Line 38... Line 39...
38
 
39
 
39
    int fd;
40
    int fd;
Line 80... Line 81...
80
    eglBindAPI(EGL_OPENGL_API);
81
    eglBindAPI(EGL_OPENGL_API);
81
    context = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL);
82
    context = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL);
82
    if (!context)
83
    if (!context)
83
        printf("failed to create context");
84
        printf("failed to create context");
Line 84... Line 85...
84
 
85
 
Line 85... Line 86...
85
    gs = gbm_surface_create(gbm, 1024, 768, GBM_BO_FORMAT_ARGB8888, GBM_BO_USE_RENDERING);
86
//    gs = gbm_surface_create(gbm, 1024, 768, GBM_BO_FORMAT_ARGB8888, GBM_BO_USE_RENDERING);
86
 
87
 
87
 
88
 
Line 88... Line 89...
88
    BeginDraw();
89
    BeginDraw();
Line 89... Line 90...
89
    DrawWindow(20, 20, 400+9, 300+24, "gl-render", 0x000000, 0x74);
90
    DrawWindow(20, 20, 400+9, 300+24, "gl-render", 0x000000, 0x74);
90
    EndDraw();
91
    EndDraw();
91
 
92
 
Line 92... Line 93...
92
    sna_create_mask();
93
    sna_create_mask();
93
 
94
 
Line -... Line 95...
-
 
95
  //  surface = eglCreateWindowSurface(dpy,config, (EGLNativeWindowType)gs, NULL);
-
 
96
  //  if (surface == EGL_NO_SURFACE)
-
 
97
  //      printf("failed to create surface");
-
 
98
 
-
 
99
    if (!eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, context))
-
 
100
        printf("failed to make window current");
-
 
101
 
-
 
102
 
-
 
103
    front = create_framebuffer(400,300,&f_tex);
-
 
104
    glMatrixMode(GL_PROJECTION);
-
 
105
	glLoadIdentity();
-
 
106
    glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
-
 
107
	glMatrixMode(GL_MODELVIEW);
-
 
108
	glLoadIdentity();
-
 
109
 
-
 
110
	glViewport(0, 0, 400, 300);
-
 
111
 
-
 
112
    glClearColor( 0, 0, 0, 1);
-
 
113
 
-
 
114
    glClear(GL_COLOR_BUFFER_BIT);
-
 
115
 
-
 
116
    glBegin(GL_QUADS);
-
 
117
    glColor3f(1,0,0);
-
 
118
    glVertex3f( 0.9, -0.9, -30.0);
-
 
119
    glColor3f(1,1,0);
-
 
120
    glVertex3f( 0.9,  0.9, -30.0);
-
 
121
 
-
 
122
    glColor3f(1,1,1);
-
 
123
    glVertex3f( 0.1,  0.9, -30.0);
94
    surface = eglCreateWindowSurface(dpy,config, (EGLNativeWindowType)gs, NULL);
124
    glColor3f(1,0,1);
95
    if (surface == EGL_NO_SURFACE)
125
    glVertex3f( 0.1, -0.9, -30.0);
96
        printf("failed to create surface");
126
    glEnd();
97
 
127
 
98
    if (!eglMakeCurrent(dpy, surface, surface, context))
128
    glFlush();
Line 113... Line 143...
113
		fb_image = px_create_image(dpy,context,fb.width,fb.height,
143
		fb_image = px_create_image(dpy,context,fb.width,fb.height,
114
                                   fb.pitch,fb.name);
144
                                   fb.pitch,fb.name);
Line 115... Line 145...
115
 
145
 
Line 116... Line -...
116
        printf("fb_image %p\n", fb_image);
-
 
117
 
-
 
118
  asm volatile ("int3");
146
        printf("fb_image %p\n", fb_image);
119
 
147
 
120
        glGenTextures(1, &texture);
148
        glGenTextures(1, &texture);
121
        glBindTexture(GL_TEXTURE_2D, texture);
149
        glBindTexture(GL_TEXTURE_2D, texture);
Line 160... Line 188...
160
            }
188
            }
Line 161... Line 189...
161
 
189
 
162
            printf("destination is framebuffer incomplete: %s [%#x]\n",
190
            printf("destination is framebuffer incomplete: %s [%#x]\n",
163
			   str, status);
191
			   str, status);
-
 
192
        }
Line 164... Line 193...
164
        }
193
    }
165
 
-
 
166
        glViewport(0, 0,fb.width, fb.height);
194
 
167
 
195
	glViewport(0, 0, 1024, 768);
168
        glMatrixMode(GL_PROJECTION);
-
 
169
        glLoadIdentity();
196
    glMatrixMode(GL_PROJECTION);
170
        glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 100.0);
197
	glLoadIdentity();
171
        glMatrixMode(GL_MODELVIEW);
-
 
172
        glLoadIdentity();
-
 
173
    }
-
 
Line 174... Line -...
174
 
-
 
175
    glClearColor( 0, 0, 0, 1);
-
 
Line 176... Line 198...
176
 
198
	glMatrixMode(GL_MODELVIEW);
177
    list = glGenLists(1);
199
	glLoadIdentity();
178
    glNewList(list, GL_COMPILE);
-
 
179
 
200
 
180
   /* XXX: this state-change will only be executed if list is called
201
 
181
    * from outside a begin/end pair:
202
    const char *vs_src =
182
    */
-
 
183
    glShadeModel( GL_FLAT );
203
	    "attribute vec4 v_position;\n"
184
    glBegin(GL_TRIANGLES);
204
	    "attribute vec4 v_texcoord0;\n"
185
    glColor3f(0,0,.7);
-
 
186
    glVertex3f( -0.9,  0.9, -30.0);
205
	    "varying vec2 source_texture;\n"
187
    glColor3f(0,.9,0);
206
	    "void main()\n"
188
    glVertex3f( -0.9, -0.9, -30.0);
207
	    "{\n"
189
    glColor3f(.8,0,0);
-
 
190
    glVertex3f(  0.9,  0.0, -30.0);
-
 
191
    glEnd();
208
	    "	gl_Position = v_position;\n"
192
 
209
	    "	source_texture = v_texcoord0.xy;\n"
193
   /* This statechange is potentially NOT redundant:
210
	    "}\n";
194
    */
211
 
195
    glShadeModel( GL_FLAT );
212
	const char *fs_src =
196
    glBegin(GL_TRIANGLES);
213
//	    "precision mediump float;\n"
197
    glColor3f(0,1,0);
214
	    "varying vec2 source_texture;\n"
198
    glVertex3f( -0.5,  0.5, -30.0);
215
	    "uniform sampler2D sampler;\n"
199
    glColor3f(0,0,1);
216
	    "void main()\n"
Line 200... Line 217...
200
    glVertex3f( -0.5, -0.5, -30.0);
217
	    "{\n"
-
 
218
	    "   vec3 cg = texture2D(sampler, source_texture).rgb;\n"
Line 201... Line 219...
201
    glColor3f(1,0,0);
219
	    "   gl_FragColor = vec4(cg.r,cg.g,cg.b,1.0);\n"
Line 202... Line 220...
202
    glVertex3f(  0.5,  0.0, -30.0);
220
	    "}\n";
-
 
221
 
-
 
222
	GLuint blit_prog;
-
 
223
	GLint  vs_shader, fs_shader;
-
 
224
 
-
 
225
  asm volatile ("int3");
-
 
226
 
203
    glEnd();
227
	blit_prog = glCreateProgram();
204
 
228
    vs_shader = create_shader(GL_VERTEX_SHADER,vs_src);
205
    glEndList();
229
    fs_shader = create_shader(GL_FRAGMENT_SHADER, fs_src);
206
 
230
 	glAttachShader(blit_prog, vs_shader);
-
 
231
 	glAttachShader(blit_prog, fs_shader);
-
 
232
 	glBindAttribLocation(blit_prog, 0, "v_position");
-
 
233
	glBindAttribLocation(blit_prog, 1, "v_texcoord0");
-
 
234
 
207
  asm volatile ("int3");
235
	GLint ok;
-
 
236
 
208
 
237
	glLinkProgram(blit_prog);
209
    glDrawBuffer(GL_BACK);
238
	glGetProgramiv(blit_prog, GL_LINK_STATUS, &ok);
210
 
239
	if (!ok) {
211
    glClear(GL_COLOR_BUFFER_BIT);
240
		GLchar *info;
212
 
-
 
213
    glShadeModel( GL_SMOOTH );
241
		GLint size;
214
 
-
 
215
    glBegin(GL_TRIANGLES);
242
 
Line -... Line 243...
-
 
243
		glGetProgramiv(blit_prog, GL_INFO_LOG_LENGTH, &size);
-
 
244
		info = malloc(size);
-
 
245
 
216
 
246
		glGetProgramInfoLog(blit_prog, size, NULL, info);
-
 
247
		printf("Failed to link: %s\n", info);
-
 
248
		printf("GLSL link failure\n");
-
 
249
	}
-
 
250
 
-
 
251
    GLint sampler;
-
 
252
	float vertices[8], texcoords[8];
-
 
253
    GLfloat dst_xscale, dst_yscale; //, src_xscale, src_yscale;
-
 
254
    int l, t, r, b, stride;
-
 
255
 
-
 
256
	sampler = glGetUniformLocation(blit_prog,"sampler");
-
 
257
	glUseProgram(blit_prog);
-
 
258
	glUniform1i(sampler, 0);
-
 
259
 
-
 
260
	glVertexAttribPointer(0, 2, GL_FLOAT,GL_FALSE, 2 * sizeof(float),vertices);
-
 
261
	glEnableVertexAttribArray(0);
-
 
262
 
-
 
263
	glActiveTexture(GL_TEXTURE0);
-
 
264
	glBindTexture(GL_TEXTURE_2D, f_tex);
-
 
265
	glTexParameteri(GL_TEXTURE_2D,
-
 
266
				  GL_TEXTURE_MIN_FILTER,
-
 
267
				  GL_NEAREST);
-
 
268
	glTexParameteri(GL_TEXTURE_2D,
-
 
269
				  GL_TEXTURE_MAG_FILTER,
-
 
270
				  GL_NEAREST);
-
 
271
 
-
 
272
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float),texcoords);
-
 
273
	glEnableVertexAttribArray(1);
-
 
274
 
-
 
275
	dst_xscale = 1.0/1024;
-
 
276
	dst_yscale = 1.0/768;
-
 
277
//	src_xscale = 1.0/400;
-
 
278
//	src_yscale = 1.0/300;
-
 
279
 
-
 
280
    stride = 2;
-
 
281
 
-
 
282
	l = 20;
-
 
283
	t = 20;
-
 
284
	r = l+400;
-
 
285
	b = t+300;
-
 
286
 
-
 
287
    float t0, t1, t2, t5;
-
 
288
 
-
 
289
    vertices[0]     = t0 = 2*l*dst_xscale - 1.0;
-
 
290
    vertices[1 * 2] = t2 = 2*r*dst_xscale - 1.0;
-
 
291
 
-
 
292
    vertices[2 * 2] = t2;
-
 
293
    vertices[3 * 2] = t0;
-
 
294
 
-
 
295
    vertices[1]     = t1 = 2*t*dst_yscale - 1.0;
-
 
296
    vertices[2*2+1] = t5 = 2*b*dst_yscale - 1.0;
-
 
297
    vertices[1*2+1] = t1;
-
 
298
    vertices[3*2+1] = t5;
-
 
299
 
-
 
300
    texcoords[0]    = 0.0;
-
 
301
    texcoords[1]    = 0.0;
-
 
302
    texcoords[1*2]  = 1.0;
-
 
303
    texcoords[1*2+1]= 0.0;
-
 
304
    texcoords[2*2]  = 1.0;
-
 
305
    texcoords[2*2+1]= 1.0;
-
 
306
    texcoords[3*2]  = 0.0;
Line 217... Line 307...
217
   /* Note: call the list from inside a begin/end pair.  The end is
307
    texcoords[3*2+1]= 1.0;
218
    * provided by the display list...
308
 
219
    */
309
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
220
    glCallList(list);
310
 
Line 267... Line 357...
267
		    EGL_DRM_BUFFER_USE_SCANOUT_MESA,
357
		    EGL_DRM_BUFFER_USE_SCANOUT_MESA,
268
		EGL_NONE
358
		EGL_NONE
269
	};
359
	};
270
	attribs[1] = width;
360
	attribs[1] = width;
271
	attribs[3] = height;
361
	attribs[3] = height;
272
	attribs[5] = stride;
362
	attribs[5] = stride/4;
Line 273... Line 363...
273
 
363
 
274
    printf("%s w:%d :%d pitch:%d handle %d\n", __FUNCTION__,
364
    printf("%s w:%d :%d pitch:%d handle %d\n", __FUNCTION__,
Line 275... Line 365...
275
           width, height, stride, name);
365
           width, height, stride, name);
276
 
366
 
Line 277... Line 367...
277
	image = eglCreateImageKHR(display, context, EGL_DRM_BUFFER_MESA,
367
	image = eglCreateImageKHR(display, context, EGL_DRM_BUFFER_MESA,
278
						 (void *) (uintptr_t)name, attribs);
368
						 (void *) (uintptr_t)name, attribs);
-
 
369
 
-
 
370
	return image;
-
 
371
}
-
 
372
 
-
 
373
GLint create_shader(GLenum type, const char *source)
-
 
374
{
-
 
375
	GLint ok;
-
 
376
	GLint prog;
-
 
377
 
-
 
378
	prog = glCreateShader(type);
-
 
379
	glShaderSource(prog, 1, (const GLchar **) &source, NULL);
-
 
380
	glCompileShader(prog);
-
 
381
	glGetShaderiv(prog, GL_COMPILE_STATUS, &ok);
-
 
382
	if (!ok) {
-
 
383
		GLchar *info;
-
 
384
		GLint size;
-
 
385
 
-
 
386
		glGetShaderiv(prog, GL_INFO_LOG_LENGTH, &size);
-
 
387
		info = malloc(size);
-
 
388
 
-
 
389
		glGetShaderInfoLog(prog, size, NULL, info);
-
 
390
		printf("Failed to compile %s: %s\n",
-
 
391
                type == GL_FRAGMENT_SHADER ? "FS" : "VS",info);
-
 
392
		printf("Program source:\n%s", source);
-
 
393
		printf("GLSL compile failure\n");
-
 
394
	}
-
 
395
 
-
 
396
	return prog;
-
 
397
}
-
 
398
 
-
 
399
GLuint create_framebuffer(int width, int height, GLuint *tex)
-
 
400
{
-
 
401
    GLuint buffer;
-
 
402
 
-
 
403
    glGenTextures(1, tex);
-
 
404
    glBindTexture(GL_TEXTURE_2D, *tex);
-
 
405
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
-
 
406
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
-
 
407
 
-
 
408
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
-
 
409
			       GL_UNSIGNED_BYTE, NULL);
-
 
410
    glBindTexture(GL_TEXTURE_2D, 0);
-
 
411
 
-
 
412
    glGenFramebuffers(1, &buffer);
-
 
413
    glBindFramebuffer(GL_FRAMEBUFFER, buffer);
-
 
414
    glFramebufferTexture2D(GL_FRAMEBUFFER,
-
 
415
					 GL_COLOR_ATTACHMENT0,