| 1 | unit MethodOpenGLPBO;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, DrawMethod, FastBitmap, Controls, Graphics
|
|---|
| 7 | {$IFDEF OPENGL}, GL, GLExt, OpenGLContext{$ENDIF};
|
|---|
| 8 |
|
|---|
| 9 | {$IFDEF OPENGL}
|
|---|
| 10 | type
|
|---|
| 11 | { TMethodOpenGLPBO }
|
|---|
| 12 |
|
|---|
| 13 | TMethodOpenGLPBO = class(TDrawMethodOpenGL)
|
|---|
| 14 | pboIds: array[0..1] of GLuint;
|
|---|
| 15 | Index, NextIndex: Integer;
|
|---|
| 16 | procedure Init(AParent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat); override;
|
|---|
| 17 | constructor Create; override;
|
|---|
| 18 | destructor Destroy; override;
|
|---|
| 19 | procedure DrawFrame(FastBitmap: TFastBitmap); override;
|
|---|
| 20 | end;
|
|---|
| 21 | {$ENDIF}
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 | implementation
|
|---|
| 25 |
|
|---|
| 26 | {$IFDEF OPENGL}
|
|---|
| 27 | { TMethodOpenGLPBO }
|
|---|
| 28 |
|
|---|
| 29 | //procedure glGenBuffersARB2 : procedure(n : GLsizei; buffers : PGLuint); extdecl;
|
|---|
| 30 |
|
|---|
| 31 | procedure TMethodOpenGLPBO.Init(AParent: TWinControl; Size: TPoint; PixelFormat: TPixelFormat);
|
|---|
| 32 | var
|
|---|
| 33 | DataSize: Integer;
|
|---|
| 34 | glExtensions: string;
|
|---|
| 35 | begin
|
|---|
| 36 | inherited;
|
|---|
| 37 |
|
|---|
| 38 | OpenGLControl.MakeCurrent;
|
|---|
| 39 | DataSize := OpenGLControl.Width * OpenGLControl.Height * SizeOf(Integer);
|
|---|
| 40 | // glGenBuffersARB(Length(pboIds), PGLuint(pboIds));
|
|---|
| 41 | //if glext_LoadExtension('GL_ARB_pixel_buffer_object') then
|
|---|
| 42 | if Load_GL_ARB_vertex_buffer_object then begin
|
|---|
| 43 | glGenBuffersARB(2, @pboIds);
|
|---|
| 44 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[0]);
|
|---|
| 45 | glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DataSize, Pointer(0), GL_STREAM_READ_ARB);
|
|---|
| 46 | glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[1]);
|
|---|
| 47 | glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DataSize, Pointer(0), GL_STREAM_READ_ARB);
|
|---|
| 48 |
|
|---|
| 49 | end else raise Exception.Create('GL_ARB_pixel_buffer_object not supported');
|
|---|
| 50 |
|
|---|
| 51 | glEnable(GL_TEXTURE_2D);
|
|---|
| 52 | glBindTexture(GL_TEXTURE_2D, TextureId);
|
|---|
| 53 | //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|---|
| 54 | //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|---|
| 55 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|---|
| 56 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|---|
| 57 | glTexImage2D(GL_TEXTURE_2D, 0, 4, OpenGLControl.Width, OpenGLControl.Height,
|
|---|
| 58 | 0, GL_RGBA, GL_UNSIGNED_BYTE, OpenGLBitmap);
|
|---|
| 59 | end;
|
|---|
| 60 |
|
|---|
| 61 | constructor TMethodOpenGLPBO.Create;
|
|---|
| 62 | begin
|
|---|
| 63 | inherited;
|
|---|
| 64 | Caption := 'OpenGL PBO';
|
|---|
| 65 | PaintObject := poOpenGL;
|
|---|
| 66 | // SetLength(pboIds, 2);
|
|---|
| 67 | Index := 0;
|
|---|
| 68 | NextIndex := 1;
|
|---|
| 69 | Description.Add('This method use OpenGL acceleration same like other OpenGL method but ' +
|
|---|
| 70 | 'use DMA(Direct Memory Access) for faster texture data transfer without use of CPU.');
|
|---|
| 71 | end;
|
|---|
| 72 |
|
|---|
| 73 | destructor TMethodOpenGLPBO.Destroy;
|
|---|
| 74 | begin
|
|---|
| 75 | inherited;
|
|---|
| 76 | end;
|
|---|
| 77 |
|
|---|
| 78 | procedure TMethodOpenGLPBO.DrawFrame(FastBitmap: TFastBitmap);
|
|---|
| 79 | var
|
|---|
| 80 | X, Y: Integer;
|
|---|
| 81 | P: PCardinal;
|
|---|
| 82 | R: PCardinal;
|
|---|
| 83 | Ptr: ^GLubyte;
|
|---|
| 84 | TextureShift: TPoint;
|
|---|
| 85 | TextureShift2: TPoint;
|
|---|
| 86 | const
|
|---|
| 87 | GL_CLAMP_TO_EDGE = $812F;
|
|---|
| 88 | begin
|
|---|
| 89 | // "index" is used to read pixels from framebuffer to a PBO
|
|---|
| 90 | // "nextIndex" is used to update pixels in the other PBO
|
|---|
| 91 | Index := (Index + 1) mod 2;
|
|---|
| 92 | NextIndex := (Index + 1) mod 2;
|
|---|
| 93 |
|
|---|
| 94 | glLoadIdentity;
|
|---|
| 95 |
|
|---|
| 96 | glBindTexture(GL_TEXTURE_2D, TextureId);
|
|---|
| 97 | //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|---|
| 98 | //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|---|
| 99 | //glTexImage2D(GL_TEXTURE_2D, 0, 4, OpenGLControl.Width, OpenGLControl.Height,
|
|---|
| 100 | // 0, GL_RGBA, GL_UNSIGNED_BYTE, OpenGLBitmap);
|
|---|
| 101 | //glTexImage2D(GL_TEXTURE_2D, 0, 4, 512, 256,
|
|---|
| 102 | //0, GL_RGBA, GL_UNSIGNED_BYTE, OpenGLBitmap);
|
|---|
| 103 |
|
|---|
| 104 | // bind the texture and PBO
|
|---|
| 105 | //glBindTexture(GL_TEXTURE_2D, textureId);
|
|---|
| 106 | glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pboIds[index]);
|
|---|
| 107 |
|
|---|
| 108 | // copy pixels from PBO to texture object
|
|---|
| 109 | // Use offset instead of ponter.
|
|---|
| 110 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, OpenGLControl.Width, OpenGLControl.Height,
|
|---|
| 111 | GL_BGRA, GL_UNSIGNED_BYTE, Pointer(0));
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | // bind PBO to update texture source
|
|---|
| 115 | glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pboIds[nextIndex]);
|
|---|
| 116 |
|
|---|
| 117 | // Note that glMapBufferARB() causes sync issue.
|
|---|
| 118 | // If GPU is working with this buffer, glMapBufferARB() will wait(stall)
|
|---|
| 119 | // until GPU to finish its job. To avoid waiting (idle), you can call
|
|---|
| 120 | // first glBufferDataARB() with NULL pointer before glMapBufferARB().
|
|---|
| 121 | // If you do that, the previous data in PBO will be discarded and
|
|---|
| 122 | // glMapBufferARB() returns a new allocated pointer immediately
|
|---|
| 123 | // even if GPU is still working with the previous data.
|
|---|
| 124 | glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, OpenGLControl.Width * OpenGLControl.Height * SizeOf(Integer), Pointer(0), GL_STREAM_DRAW_ARB);
|
|---|
| 125 |
|
|---|
| 126 | // map the buffer object into client's memory
|
|---|
| 127 | ptr := glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB);
|
|---|
| 128 | if Assigned(ptr) then begin
|
|---|
| 129 | // update data directly on the mapped buffer
|
|---|
| 130 | P := PCardinal(Ptr);
|
|---|
| 131 | with FastBitmap do
|
|---|
| 132 | for Y := 0 to Size.Y - 2 do begin
|
|---|
| 133 | R := P;
|
|---|
| 134 | for X := 0 to Size.X - 1 do begin
|
|---|
| 135 | R^ := NoSwapBRComponent(Pixels[X, Y]) or $ff000000;
|
|---|
| 136 | Inc(R);
|
|---|
| 137 | end;
|
|---|
| 138 | Inc(P, Size.X);
|
|---|
| 139 | end;
|
|---|
| 140 | glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB);
|
|---|
| 141 | end;
|
|---|
| 142 |
|
|---|
| 143 | // it is good idea to release PBOs with ID 0 after use.
|
|---|
| 144 | // Once bound with 0, all pixel operations are back to normal ways.
|
|---|
| 145 | glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
|
|---|
| 146 |
|
|---|
| 147 | glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
|
|---|
| 148 | //glRotatef(30.0, 0, 0, 1.0);
|
|---|
| 149 | glTranslatef(-OpenGLControl.Width / 2, -OpenGLControl.Height / 2, 0.0);
|
|---|
| 150 | glBindTexture(GL_TEXTURE_2D, TextureId);
|
|---|
| 151 |
|
|---|
| 152 | TextureShift := Point(0, 0);
|
|---|
| 153 | TextureShift2 := Point(0, 0);
|
|---|
| 154 | glBegin(GL_QUADS);
|
|---|
| 155 | glColor3ub(255, 255, 255);
|
|---|
| 156 | glTexCoord2f(TextureShift.X, TextureShift.Y);
|
|---|
| 157 | glVertex3f(TextureShift2.X, TextureShift2.Y, 0);
|
|---|
| 158 | glTexCoord2f(TextureShift.X + OpenGLControl.Width div 2, TextureShift.Y);
|
|---|
| 159 | glVertex3f(TextureShift2.X + OpenGLControl.Width, TextureShift2.Y, 0);
|
|---|
| 160 | glTexCoord2f(TextureShift.X + OpenGLControl.Width div 2, TextureShift.Y + OpenGLControl.Height div 2);
|
|---|
| 161 | glVertex3f(TextureShift2.X + OpenGLControl.Width, TextureShift2.Y + OpenGLControl.Height, 0);
|
|---|
| 162 | glTexCoord2f(TextureShift.X, TextureShift.Y + OpenGLControl.Height div 2);
|
|---|
| 163 | glVertex3f(TextureShift2.X, TextureShift2.Y + OpenGLControl.Height, 0);
|
|---|
| 164 | glEnd();
|
|---|
| 165 |
|
|---|
| 166 | OpenGLControl.SwapBuffers;
|
|---|
| 167 | end;
|
|---|
| 168 |
|
|---|
| 169 | {$ENDIF}
|
|---|
| 170 |
|
|---|
| 171 | end.
|
|---|
| 172 |
|
|---|