Graphics Abstraction Layer - Suggestions?

BlitzMax Forums/BlitzMax Programming/Graphics Abstraction Layer - Suggestions?

N(Posted 2005) [#1]
I was wondering if anyone would mind taking a look over this and tell me if that have any suggestions on areas that would need another look-over for the abstraction layer of my graphics engine (not game engine -- this will not know what entities are or that they even exist).

So, here's the code, if nobody minds taking a look-see at it:
Rem
   Cower General Public License
   
   This license is based off of the zlib/libpng license,
   I do not take credit for the creation of it as such.
   
   This software is 'as-is', without any express or implied
   warranty.  In no event will the author(s) be held liable
   for damages arising from the use of this software.

   You are granted to use this software for any purpose,
   including commercial applications of the software, and
   to alter, redistribute, and copy it freely, subject to
   the following terms:

   1. You are not to misrepresent the origin of the software;
      you must not claim that you created the original software.
      If you use this software, an acknowledgement in the product
      documentation (or elsewhere) would be appreciated, but it is
      not required.

   2. Altered source versions must be plainly marked as such, and must
      not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source distribution.

   4. Any modifications made to the source code must be made freely available
      upon request and under the Cower General Public License.

   5. Usage of the software and license can be terminated at any time
      by the author of the software.

   If you disagree with any of the terms of the license, you are not permitted
   to use the software and should delete it immediately.
EndRem

Strict

Private

Global __glob_activeDevice:GraphicsDevice=Null

Public

Type GraphicsDevice Abstract
  Method New()
    __glob_activeDevice=Self
  End Method

  Function GetActiveDevice:GraphicsDevice( )
    Return __glob_activeDevice
  End Function

  Method Initialize( gmode:GraphicsMode ) Abstract
  Method Reset( gmode:GraphicsMode ) Abstract

  Method SetTextureUnit( idx:Int ) Abstract
  Method SetTexture( t:Texture ) Abstract
  Method GetTexture:Texture( ) Abstract

  Method SetWorldMatrix( mat:Matrix, idx:Int = 0 ) Abstract
  Method SetTextureMatrix( mat:Matrix, idx:Int = 0 ) Abstract
  Method SetProjectionMatrix( mat:Matrix ) Abstract
  Method SetViewMatrix( mat:Matrix ) Abstract
  Method GetWorldMatrix:Matrix( idx:Int = 0 ) Abstract
  Method GetTextureMatrix:Matrix( idx:Int = 0 ) Abstract
  Method GetProjectionMatrix:Matrix( ) Abstract
  Method GetViewMatrix:Matrix( ) Abstract

  Method SetGraphicsState( s:GraphicsState ) Abstract
  Method GetGraphicsState:GraphicsState( ) Abstract

  Method SetClearMode( cm:ClearMode ) Abstract
  Method GetClearMode( cm:ClearMode ) Abstract

  Method Clear( ) Abstract
  Method Present( ) Abstract

  Method GetBackBuffer:Buffer( ) Abstract

  Method SetShader( s:Shader ) Abstract
  Method GetShader( s:Shader ) Abstract

  Method AddRenderTarget( t:RenderTarget ) Abstract
  Method RemoveRenderTarget( t:RenderTarget ) Abstract

  Method Render( op:RenderOp ) Abstract

  Method BeginScene( ) Abstract
  Method EndScene( ) Abstract

  Method CreateTexture:Texture( flags:TextureFlags ) Abstract
End Type

Type GraphicsMode
  Field Width:Int
  Field Height:Int
  Field BPP:Int
  Field Hertz:Int
  Field FullScreen:Int

  Field BackBuffer:Int
  Field StencilBuffer:Int
  Field DepthBuffer:Int
End Type

Type VertexData
  Field _data:Vertex[]

  Method AddVertex( x#=0, y#=0, z#=0,..
                    u#=0, v#=0, w#=0,..
                    r#=1, g#=1, b#=1, a#=1 )
    Local idx:Int=_data.Length
    _data[idx]=Vertex.Create(x, y, z,..
                             u, v, w,..
                             r, g, b, a)
    Return idx
  End Method
End Type

Type VertexBufferFactory
  Method CreateVertexBuffer:VertexBuffer( vd:VertexData ) Abstract
End Type

Type VertexBuffer Extends Buffer
  Method Bind( ) Abstract
End Type

Type IndexData
  Field Data:Int[]
End Type

Type IndexBufferFactory
  Method CreateIndexBuffer:IndexBuffer( id:IndexData ) Abstract
End Type

Type IndexBuffer Extends Buffer
  Method Bind( ) Abstract
End Type

Type Vertex
  Function Create:Vertex( X#, Y#, Z#,..
                          U#, V#, W#,..
                          R#, G#, B#, A# )
    Local i:Vertex = New Vertex
    i.X=X
    i.Y=Y
    i.Z=Z
    i.U=U
    i.V=V
    i.W=W
    i.R=R
    i.G=G
    i.B=B
    i.A=A
    Return i
  End Function

  Field X#
  Field Y#
  Field Z#

  Field NX#
  Field NY#
  Field NZ#

  Field U#
  Field V#
  Field W#

  Field R#
  Field G#
  Field B#
  Field A#
End Type

Type TextureFlags
  Field Width:Int
  Field Height:Int
  Field MipMaps:Int
  Field CubeTexture:Int
  Field VolumeTexture:Int
  Field SphereMap:Int
  Method Copy:TextureFlags()
    Local i:TextureFlags = New TextureFlags
    MemCopy(i.GetPtr(), GetPtr(), 24)
    Return i
  End Method
  Method GetPtr:Byte Ptr()
    Return Varptr Width
  End Method
End Type

Type Texture
  Field Pixmap:TPixmap

  Function Create:Texture( tf:TextureFlags ) Abstract

  Method Lock( ) Abstract
  Method Unlock( ) Abstract
  Method IsLocked( ) Abstract

  Method WritePixel( X, Y, ARGB )
    Local lockit:Int = Not IsLocked()
    If lockit Then
      Lock()
    EndIf

    Pixmap.WritePixel(X, Y, ARGB)

    If lockit Then
      Unlock()
    EndIf
  End Method

  Method ReadPixel( X, Y )
    Pixmap.ReadPixel(X, Y)
  End Method

  Method Dispose( ) Abstract
  Method Delete( )
    Dispose()
  End Method
End Type

Type Shader
  Method Dispose( ) Abstract
  Method Delete( )
        Dispose()
  End Method
End Type

Type RenderTarget
End Type

Type RenderOp
  Field vb:VertexBuffer
  Field ib:IndexBuffer
  Field drawMode:Int
End Type

Type Matrix
End Type

Type Vector
End Type

Type Quat
End Type

Type Plane
End Type

Type GraphicsState
End Type

Type ClearMode
  Field ZBuffer:Int
  Field Targets:Int
  Field Stencil:Int
  Field Color:Int
  Field Z:Float
  Field S:Int
  Method GetColorPtr:Byte Ptr( )
    Return Byte Ptr(Varptr Color)
  End Method
End Type

Type Buffer Abstract
  Method Dispose( ) Abstract

  Method Delete( )
    Dispose()
  End Method
End Type


This is pretty heavily designed with a likeness to Managed DirectX/Axiom in mind.

Some things I'm somewhat unsure about are the way vertex and index buffers are handled in this so far, that bugs me a bit, and textures -- I'm going to probably scrap the current idea for textures and redesign it.

I haven't begun writing anything that implements the classes because I want to get the foundation at least somewhat well designed before going ahead with writing different implementations for the common APIs (and probably software, 'cause I've seen how slow Direct3D's software rendering is).