Subversion Repositories Kolibri OS

Rev

Rev 4473 | Rev 4495 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/time.h>
  5.  
  6. #define EGL_EGLEXT_PROTOTYPES
  7. #define GL_GLEXT_PROTOTYPES
  8.  
  9. #include "EGL/egl.h"
  10. #include "EGL/eglext.h"
  11. #include "GL/gl.h"
  12. #include "gbm.h"
  13. #include <i915_drm.h>
  14. #include <kos32sys.h>
  15. #include <pixlib2.h>
  16.  
  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);
  20. GLint create_shader(GLenum type, const char *source);
  21.  
  22. int main()
  23. {
  24.     struct gbm_device *gbm;
  25.     struct gbm_surface  *gs;
  26.  
  27.     EGLDisplay dpy;
  28.     EGLint major, minor;
  29.  
  30.     EGLContext context;
  31.     EGLSurface surface;
  32.     EGLImageKHR fb_image;
  33.     EGLConfig config;
  34.  
  35.     EGLint config_attribs[32];
  36.     EGLint num_configs, i;
  37.     GLuint texture, buffer, front;
  38.     GLuint f_tex;
  39.  
  40.     int fd;
  41.  
  42.     fd = get_service("DISPLAY");
  43.     gbm = gbm_create_device(fd);
  44.     if( gbm == NULL){
  45.         printf("failed to initialize GBM device");
  46.         return 1;
  47.     };
  48.  
  49.     init_pixlib(HW_BIT_BLIT);
  50.  
  51.     dpy = eglGetDisplay((EGLNativeDisplayType)gbm);
  52.  
  53.     if (!eglInitialize(dpy, &major, &minor))
  54.         printf("failed to initialize EGL display");
  55.  
  56.     printf("EGL_VERSION = %s\n", eglQueryString(dpy, EGL_VERSION));
  57.     printf("EGL_VENDOR = %s\n", eglQueryString(dpy, EGL_VENDOR));
  58.     printf("EGL_EXTENSIONS = %s\n", eglQueryString(dpy, EGL_EXTENSIONS));
  59.     printf("EGL_CLIENT_APIS = %s\n",eglQueryString(dpy, EGL_CLIENT_APIS));
  60.  
  61.     i = 0;
  62.     config_attribs[i++] = EGL_RED_SIZE;
  63.     config_attribs[i++] = 1;
  64.     config_attribs[i++] = EGL_GREEN_SIZE;
  65.     config_attribs[i++] = 1;
  66.     config_attribs[i++] = EGL_BLUE_SIZE;
  67.     config_attribs[i++] = 1;
  68.     config_attribs[i++] = EGL_DEPTH_SIZE;
  69.     config_attribs[i++] = 1;
  70.  
  71.     config_attribs[i++] = EGL_SURFACE_TYPE;
  72.     config_attribs[i++] = EGL_WINDOW_BIT;
  73.  
  74.     config_attribs[i++] = EGL_RENDERABLE_TYPE;
  75.     config_attribs[i++] = EGL_OPENGL_BIT;
  76.     config_attribs[i] = EGL_NONE;
  77.  
  78.     if (!eglChooseConfig(dpy,config_attribs, &config, 1, &num_configs) || !num_configs)
  79.         printf("failed to choose a config");
  80.  
  81.     eglBindAPI(EGL_OPENGL_API);
  82.     context = eglCreateContext(dpy, config, EGL_NO_CONTEXT, NULL);
  83.     if (!context)
  84.         printf("failed to create context");
  85.  
  86. //    gs = gbm_surface_create(gbm, 1024, 768, GBM_BO_FORMAT_ARGB8888, GBM_BO_USE_RENDERING);
  87.  
  88.  
  89.     BeginDraw();
  90.     DrawWindow(20, 20, 400+9, 300+24, "gl-render", 0x000000, 0x74);
  91.     EndDraw();
  92.  
  93.     sna_create_mask();
  94.  
  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);
  124.     glColor3f(1,0,1);
  125.     glVertex3f( 0.1, -0.9, -30.0);
  126.     glEnd();
  127.  
  128.     glFlush();
  129.  
  130.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  131.  
  132.     if(fd)
  133.     {
  134.         int ret;
  135.         GLenum status;
  136.         struct drm_i915_fb_info fb;
  137.  
  138.         memset(&fb, 0, sizeof(fb));
  139.         ret = drmIoctl(fd, SRV_FBINFO, &fb);
  140.         if( ret != 0 )
  141.             printf("failed to get framebuffer info\n");
  142.  
  143.                 fb_image = px_create_image(dpy,context,fb.width,fb.height,
  144.                                    fb.pitch,fb.name);
  145.  
  146.         printf("fb_image %p\n", fb_image);
  147.  
  148.         glGenTextures(1, &texture);
  149.         glBindTexture(GL_TEXTURE_2D, texture);
  150.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
  151.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
  152.  
  153.         glEGLImageTargetTexture2DOES(GL_TEXTURE_2D,fb_image);
  154.         glBindTexture(GL_TEXTURE_2D, 0);
  155.  
  156.             glGenFramebuffers(1, &buffer);
  157.         glBindFramebuffer(GL_FRAMEBUFFER, buffer);
  158.         glFramebufferTexture2D(GL_FRAMEBUFFER,
  159.                                          GL_COLOR_ATTACHMENT0,
  160.                                          GL_TEXTURE_2D, texture,0);
  161.         status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  162.         if (status != GL_FRAMEBUFFER_COMPLETE)
  163.         {
  164.             const char *str;
  165.             switch (status)
  166.             {
  167.                 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
  168.                     str = "incomplete attachment";
  169.                     break;
  170.                 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  171.                     str = "incomplete/missing attachment";
  172.                     break;
  173.                 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
  174.                     str = "incomplete draw buffer";
  175.                     break;
  176.                 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
  177.                     str = "incomplete read buffer";
  178.                     break;
  179.                 case GL_FRAMEBUFFER_UNSUPPORTED:
  180.                     str = "unsupported";
  181.                     break;
  182.                 case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
  183.                     str = "incomplete multiple";
  184.                     break;
  185.                 default:
  186.                     str = "unknown error";
  187.                     break;
  188.             }
  189.  
  190.             printf("destination is framebuffer incomplete: %s [%#x]\n",
  191.                            str, status);
  192.         }
  193.     }
  194.  
  195.         glViewport(0, 0, 1024, 768);
  196.     glMatrixMode(GL_PROJECTION);
  197.         glLoadIdentity();
  198.         glMatrixMode(GL_MODELVIEW);
  199.         glLoadIdentity();
  200.  
  201.  
  202.     const char *vs_src =
  203.             "attribute vec4 v_position;\n"
  204.             "attribute vec4 v_texcoord0;\n"
  205.             "varying vec2 source_texture;\n"
  206.             "void main()\n"
  207.             "{\n"
  208.             "   gl_Position = v_position;\n"
  209.             "   source_texture = v_texcoord0.xy;\n"
  210.             "}\n";
  211.  
  212.         const char *fs_src =
  213. //          "precision mediump float;\n"
  214.             "varying vec2 source_texture;\n"
  215.             "uniform sampler2D sampler;\n"
  216.             "void main()\n"
  217.             "{\n"
  218.             "   vec3 cg = texture2D(sampler, source_texture).rgb;\n"
  219.             "   gl_FragColor = vec4(cg.r,cg.g,cg.b,1.0);\n"
  220.             "}\n";
  221.  
  222.         GLuint blit_prog;
  223.         GLint  vs_shader, fs_shader;
  224.  
  225.   asm volatile ("int3");
  226.  
  227.         blit_prog = glCreateProgram();
  228.     vs_shader = create_shader(GL_VERTEX_SHADER,vs_src);
  229.     fs_shader = create_shader(GL_FRAGMENT_SHADER, fs_src);
  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.  
  235.         GLint ok;
  236.  
  237.         glLinkProgram(blit_prog);
  238.         glGetProgramiv(blit_prog, GL_LINK_STATUS, &ok);
  239.         if (!ok) {
  240.                 GLchar *info;
  241.                 GLint size;
  242.  
  243.                 glGetProgramiv(blit_prog, GL_INFO_LOG_LENGTH, &size);
  244.                 info = malloc(size);
  245.  
  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;
  307.     texcoords[3*2+1]= 1.0;
  308.  
  309.     glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  310.  
  311.         glDisableVertexAttribArray(0);
  312.         glDisableVertexAttribArray(1);
  313.         glDisable(GL_TEXTURE_2D);
  314.         glUseProgram(0);
  315.  
  316.     glFinish();
  317.     eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  318. //    eglDestroySurface(dpy, surface);
  319.   //  gbm_surface_destroy(gs);
  320.     eglDestroyContext(dpy, context);
  321.     eglTerminate(dpy);
  322.  
  323.     while(1)
  324.     {
  325.         delay(1);
  326.     }
  327.  
  328.     return 0;
  329. }
  330.  
  331. int drmIoctl(int fd, unsigned long request, void *arg)
  332. {
  333.     ioctl_t  io;
  334.  
  335.     io.handle   = fd;
  336.     io.io_code  = request;
  337.     io.input    = arg;
  338.     io.inp_size = 64;
  339.     io.output   = NULL;
  340.     io.out_size = 0;
  341.  
  342.     return call_service(&io);
  343. }
  344.  
  345. EGLImageKHR px_create_image(EGLDisplay display, EGLContext context,
  346.                          int width, int height, int stride, int name)
  347. {
  348.         EGLImageKHR image;
  349.         EGLint attribs[] = {
  350.                 EGL_WIDTH, 0,
  351.                 EGL_HEIGHT, 0,
  352.                 EGL_DRM_BUFFER_STRIDE_MESA, 0,
  353.                 EGL_DRM_BUFFER_FORMAT_MESA,
  354.                 EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
  355.                 EGL_DRM_BUFFER_USE_MESA,
  356.                 EGL_DRM_BUFFER_USE_SHARE_MESA |
  357.                     EGL_DRM_BUFFER_USE_SCANOUT_MESA,
  358.                 EGL_NONE
  359.         };
  360.         attribs[1] = width;
  361.         attribs[3] = height;
  362.         attribs[5] = stride/4;
  363.  
  364.     printf("%s w:%d :%d pitch:%d handle %d\n", __FUNCTION__,
  365.            width, height, stride, name);
  366.  
  367.         image = eglCreateImageKHR(display, context, EGL_DRM_BUFFER_MESA,
  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,
  416.                                          GL_TEXTURE_2D, *tex,0);
  417.    return buffer;
  418. }
  419.