Advertisement
Guest User

RasPi

a guest
Jan 18th, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. //Get DC
  2.                 RaspberryPi.bcm_host_init();
  3.                 handle = window.Handle;
  4.                 GL.GetError();//NOTE: THIS MUST BE HERE SO THAT libGLES LOADS BEFORE libEGL
  5.                 dc = EGL.GetDisplay(IntPtr.Zero);
  6.                
  7.                 int major, minor;
  8.                 if (!EGL.Initialize(dc, out major, out minor))
  9.                 {
  10.                     Debug.ThrowError("Video", string.Format("Failed to initialize display connection, Error {0}", EGL.GetError()));
  11.                 }
  12.                 if (minor != EGL.OPENGL_ES2_BIT) Debug.ThrowError("Video", "GLES2 is not supported");
  13.                
  14.                 int[] pixelFormat = new int[]
  15.                 {
  16.                     //EGL.RENDERABLE_TYPE, EGL.OPENGL_ES2_BIT,
  17.                    
  18.                     EGL.SURFACE_TYPE, EGL.WINDOW_BIT,
  19.                     EGL.RED_SIZE, 8,
  20.                     EGL.GREEN_SIZE, 8,
  21.                     EGL.BLUE_SIZE, 8,
  22.                     EGL.ALPHA_SIZE, 8,
  23.                    
  24.                     EGL.DEPTH_SIZE, 16,
  25.                     //EGL.STENCIL_SIZE, 0,
  26.                    
  27.                     //Egl.SAMPLE_BUFFERS, samples > 0 ? 1 : 0,
  28.                     //EGL.SAMPLES, 0,
  29.                    
  30.                     //EGL.MIN_SWAP_INTERVAL, 0,
  31.                     //EGL.MAX_SWAP_INTERVAL, 1,
  32.                    
  33.                     EGL.NONE,
  34.                 };
  35.                
  36.                 int num_configs;
  37.                 var configs = new IntPtr[1];
  38.                 if (!EGL.ChooseConfig(dc, pixelFormat, configs, configs.Length, out num_configs) || num_configs == 0)
  39.                 {
  40.                     Debug.ThrowError("Video", string.Format("Failed to retrieve GraphicsMode, error {0}", EGL.GetError()));
  41.                 }
  42.                
  43.                 int[] attrib_list = new int[]
  44.                 {
  45.                     EGL.CONTEXT_CLIENT_VERSION, 2,
  46.                     EGL.NONE
  47.                 };
  48.                
  49.                 ctx = EGL.CreateContext(dc, configs[0], IntPtr.Zero, attrib_list);
  50.                 if (ctx == IntPtr.Zero) Debug.ThrowError("Video", "Failed to create context");
  51.                
  52.                 // Raspberry Pi stuff >>>>>>>>>>>>>>>>>>>>>>>>>
  53.                 unsafe
  54.                 {
  55.                     const int piDisplay = 0;
  56.                
  57.                     uint piWidth = 0, piHeight = 0;
  58.                     if (RaspberryPi.graphics_get_display_size(piDisplay, &piWidth, &piHeight) < 0) Debug.ThrowError("Video", "Failed to get display size");
  59.                     Console.WriteLine("piWidth - " + piWidth);
  60.                     Console.WriteLine("piHeight - " + piHeight);
  61.                    
  62.                     IntPtr dispman_display = RaspberryPi.vc_dispmanx_display_open(piDisplay);
  63.                     if (dispman_display == IntPtr.Zero) Debug.ThrowError("Video", "Failed: vc_dispmanx_display_open");
  64.                    
  65.                     IntPtr dispman_update = RaspberryPi.vc_dispmanx_update_start(0);
  66.                     if (dispman_update == IntPtr.Zero) Debug.ThrowError("Video", "Failed: vc_dispmanx_update_start");
  67.                    
  68.                     RaspberryPi.VC_RECT_T dstRect = new RaspberryPi.VC_RECT_T()
  69.                     {
  70.                         x = 0,
  71.                         y = 0,
  72.                         width = (int)piWidth,
  73.                         height = (int)piHeight
  74.                     };
  75.                     RaspberryPi.VC_RECT_T srcRect = new RaspberryPi.VC_RECT_T()
  76.                     {
  77.                         x = 0,
  78.                         y = 0,
  79.                         //width = (int)piWidth,
  80.                         //height = (int)piHeight,
  81.                         width = (int)(piWidth << 16),
  82.                         height = (int)(piHeight << 16)
  83.                     };
  84.                     IntPtr dispman_element = RaspberryPi.vc_dispmanx_element_add(dispman_update, dispman_display, 0, &dstRect, IntPtr.Zero, &srcRect, RaspberryPi.DISPMANX_PROTECTION_NONE, IntPtr.Zero, IntPtr.Zero, 0);
  85.                     if (dispman_element == IntPtr.Zero) Debug.ThrowError("Video", "Failed: vc_dispmanx_element_add");
  86.                    
  87.                     RaspberryPi.vc_dispmanx_update_submit_sync(dispman_update);
  88.                    
  89.                     EGL.DISPMANX_WINDOW_T nativeWindow = new EGL.DISPMANX_WINDOW_T()
  90.                     {
  91.                         element = dispman_element,
  92.                         width = (int)piWidth,
  93.                         height = (int)piHeight
  94.                     };
  95.                     surface = EGL.CreateWindowSurface(dc, configs[0], new IntPtr(&nativeWindow), null);
  96.                 }
  97.                 // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  98.                
  99.                 //surface = EGL.CreateWindowSurface(dc, configs[0], handle, null);// <<<<<<<<<<<<<<<<<<<<<<<<<<<< x86 x64
  100.                 if (surface == IntPtr.Zero) Debug.ThrowError("Video", "Failed to create window surface");
  101.                
  102.                 if (!EGL.MakeCurrent(dc, surface, surface, ctx)) Debug.ThrowError("Video", "Failed to make EGL context current");
  103.                 //if (!EGL.SwapInterval(dc, vSync ? 1 : 0)) Debug.ThrowError("Video", "Failed to set vSync");// crashes the pi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement