Render_Device.h
Go to the documentation of this file.
00001 /************************************************************************************
00002 
00003 Filename    :   Render_Device.h
00004 Content     :   Platform renderer for simple scene graph
00005 Created     :   September 6, 2012
00006 Authors     :   Andrew Reisse
00007 
00008 Copyright   :   Copyright 2012 Oculus VR, Inc. All Rights reserved.
00009 
00010 Licensed under the Apache License, Version 2.0 (the "License");
00011 you may not use this file except in compliance with the License.
00012 You may obtain a copy of the License at
00013 
00014 http://www.apache.org/licenses/LICENSE-2.0
00015 
00016 Unless required by applicable law or agreed to in writing, software
00017 distributed under the License is distributed on an "AS IS" BASIS,
00018 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00019 See the License for the specific language governing permissions and
00020 limitations under the License.
00021 
00022 ************************************************************************************/
00023 #ifndef OVR_Render_Device_h
00024 #define OVR_Render_Device_h
00025 
00026 #include "Kernel/OVR_Math.h"
00027 #include "Kernel/OVR_Array.h"
00028 #include "Kernel/OVR_RefCount.h"
00029 #include "Kernel/OVR_String.h"
00030 #include "Kernel/OVR_File.h"
00031 
00032 #include "Util/Util_Render_Stereo.h"
00033 
00034 namespace OVR { namespace Render {
00035 
00036 using namespace OVR::Util::Render;
00037 
00038 class RenderDevice;
00039 struct Font;
00040 
00041 //-----------------------------------------------------------------------------------
00042 
00043 enum PrimitiveType
00044 {
00045     Prim_Triangles,
00046     Prim_Lines,
00047     Prim_TriangleStrip,
00048     Prim_Points,
00049     Prim_Unknown,
00050     Prim_Count
00051 };
00052 
00053 class Fill : public RefCountBase<Fill>
00054 {
00055 public:
00056     enum Flags
00057     {
00058         F_Solid = 1,
00059         F_Wireframe = 2,
00060     };
00061 
00062     virtual ~Fill() {}
00063 
00064     virtual void Set(PrimitiveType prim = Prim_Unknown) const = 0;
00065     virtual void Unset() const {}
00066 
00067     virtual void SetTexture(int i, class Texture* tex) { OVR_UNUSED2(i,tex); }
00068     virtual Texture* GetTexture(int i) { OVR_UNUSED(i); return 0; }
00069 };
00070 
00071 enum ShaderStage
00072 {
00073     Shader_Vertex   = 0,
00074     Shader_Geometry = 1,
00075     Shader_Fragment = 2,
00076     Shader_Pixel    = 2,
00077     Shader_Count    = 3,
00078 };
00079 
00080 enum BuiltinShaders
00081 {
00082     VShader_MV                      = 0,
00083     VShader_MVP                     = 1,
00084     VShader_PostProcess             = 2,
00085     VShader_Count                   = 3,
00086 
00087     FShader_Solid                   = 0,
00088     FShader_Gouraud                 = 1,
00089     FShader_Texture                 = 2,
00090     FShader_AlphaTexture            = 3,
00091     FShader_PostProcess             = 4,
00092     FShader_PostProcessWithChromAb  = 5,
00093     FShader_LitGouraud              = 6,
00094     FShader_LitTexture              = 7,
00095         FShader_MultiTexture            = 8,
00096     FShader_Count                   = 9,
00097 };
00098 
00099 
00100 enum MapFlags
00101 {
00102     Map_Discard        = 1,
00103     Map_Read           = 2, // do not use
00104     Map_Unsynchronized = 4, // like D3D11_MAP_NO_OVERWRITE
00105 };
00106 
00107 enum BufferUsage
00108 {
00109     Buffer_Unknown  = 0,
00110     Buffer_Vertex   = 1,
00111     Buffer_Index    = 2,
00112     Buffer_Uniform  = 4,
00113     Buffer_Feedback = 8,
00114     Buffer_TypeMask = 0xff,
00115     Buffer_ReadOnly = 0x100, // Buffer must be created with Data().
00116 };
00117 
00118 enum TextureFormat
00119 {
00120     Texture_RGBA            = 0x100,
00121     Texture_R               = 0x200,
00122     Texture_DXT1            = 0x1100,
00123     Texture_DXT3            = 0x1200,
00124     Texture_DXT5            = 0x1300,
00125     Texture_Depth           = 0x8000,
00126     Texture_TypeMask        = 0xff00,
00127     Texture_Compressed      = 0x1000,
00128     Texture_SamplesMask     = 0x00ff,
00129     Texture_RenderTarget    = 0x10000,
00130     Texture_GenMipmaps      = 0x20000,
00131 };
00132 
00133 enum SampleMode
00134 {
00135     Sample_Linear       = 0,
00136     Sample_Nearest      = 1,
00137     Sample_Anisotropic  = 2,
00138     Sample_FilterMask   = 3,
00139 
00140     Sample_Repeat       = 0,
00141     Sample_Clamp        = 4,
00142     Sample_ClampBorder  = 8, // If unsupported Clamp is used instead.
00143     Sample_AddressMask  =12,
00144 
00145     Sample_Count        =13,
00146 };
00147 
00148 // A vector with a dummy w component for alignment in uniform buffers (and for float colors).
00149 // The w component is not used in any calculations.
00150 
00151 struct Vector4f : public Vector3f
00152 {
00153     float w;
00154 
00155     Vector4f() : w(1) {}
00156     Vector4f(const Vector3f& v) : Vector3f(v), w(1) {}
00157     Vector4f(float r, float g, float b, float a) : Vector3f(r,g,b), w(a) {}
00158 };
00159 
00160 
00161 class Shader : public RefCountBase<Shader>
00162 {
00163     friend class ShaderSet;
00164 
00165 protected:
00166     ShaderStage Stage;
00167 
00168 public:
00169     Shader(ShaderStage s) : Stage(s) {}
00170     virtual ~Shader() {}
00171 
00172     ShaderStage GetStage() const { return Stage; }
00173 
00174     virtual void Set(PrimitiveType) const { }
00175     virtual void SetUniformBuffer(class Buffer* buffers, int i = 0) { OVR_UNUSED2(buffers, i); }
00176     virtual bool UseTransposeMatrix() const { return 0; }
00177 
00178 protected:
00179     virtual bool SetUniform(const char* name, int n, const float* v) { OVR_UNUSED3(name, n, v); return false; }
00180 };
00181 
00182 
00183 // A group of shaders, one per stage.
00184 // Some renderers subclass this, so CreateShaderSet must be used.
00185 
00186 class ShaderSet : public RefCountBase<ShaderSet>
00187 {
00188  protected:
00189     Ptr<Shader> Shaders[Shader_Count];
00190 
00191 public:
00192     ShaderSet() { }
00193     ~ShaderSet() { }
00194 
00195     virtual void SetShader(Shader *s)
00196     {
00197         Shaders[s->GetStage()] = s;
00198     }
00199     virtual void UnsetShader(int stage)
00200     {
00201         Shaders[stage] = NULL;
00202     }
00203     Shader* GetShader(int stage) { return Shaders[stage]; }
00204 
00205     virtual void Set(PrimitiveType prim) const
00206     {
00207         for (int i = 0; i < Shader_Count; i++)
00208             if (Shaders[i])
00209                 Shaders[i]->Set(prim);
00210     }
00211 
00212     // Set a uniform (other than the standard matrices). It is undefined whether the
00213     // uniforms from one shader occupy the same space as those in other shaders
00214     // (unless a buffer is used, then each buffer is independent).     
00215     virtual bool SetUniform(const char* name, int n, const float* v)
00216     {
00217         bool result = 0;
00218         for (int i = 0; i < Shader_Count; i++)
00219             if (Shaders[i])
00220                 result |= Shaders[i]->SetUniform(name, n, v);
00221 
00222         return result;
00223     }
00224     bool SetUniform1f(const char* name, float x)
00225     {
00226         const float v[] = {x};
00227         return SetUniform(name, 1, v);
00228     }
00229     bool SetUniform2f(const char* name, float x, float y)
00230     {
00231         const float v[] = {x,y};
00232         return SetUniform(name, 2, v);
00233     }
00234     bool SetUniform4f(const char* name, float x, float y, float z, float w = 1)
00235     {
00236         const float v[] = {x,y,z,w};
00237         return SetUniform(name, 4, v);
00238     }
00239     bool SetUniformv(const char* name, const Vector3f& v)
00240     {
00241         const float a[] = {v.x,v.y,v.z,1};
00242         return SetUniform(name, 4, a);
00243     }
00244     bool SetUniform4fv(const char* name, int n, const Vector4f* v)
00245     {
00246         return SetUniform(name, 4*n, &v[0].x);
00247     }
00248     virtual bool SetUniform4x4f(const char* name, const Matrix4f& m)
00249     {
00250         return SetUniform(name, 16, &m.M[0][0]);
00251     }
00252 };
00253 
00254 class ShaderSetMatrixTranspose : public ShaderSet
00255 {
00256 public:
00257     virtual bool SetUniform4x4f(const char* name, const Matrix4f& m)
00258     {
00259         Matrix4f mt = m.Transposed();
00260         return SetUniform(name, 16, &mt.M[0][0]);
00261     }
00262 };
00263 
00264 class ShaderFill : public Fill
00265 {
00266     Ptr<ShaderSet> Shaders;
00267     Ptr<Texture>   Textures[8];
00268 
00269 public:
00270     ShaderFill(ShaderSet* sh) : Shaders(sh) {  }
00271     ShaderFill(ShaderSet& sh) : Shaders(sh) {  }
00272     void Set(PrimitiveType prim) const;
00273     ShaderSet* GetShaders() { return Shaders; }
00274 
00275     virtual void SetTexture(int i, class Texture* tex) { if (i < 8) Textures[i] = tex; }
00276     virtual Texture* GetTexture(int i) { if (i < 8) return Textures[i]; else return 0; }
00277 };
00278 
00279 /* Buffer for vertex or index data. Some renderers require separate buffers, so that
00280    is recommended. Some renderers cannot have high-performance buffers which are readable,
00281    so reading in Map should not be relied on.
00282 
00283    Constraints on buffers, such as ReadOnly, are not enforced by the api but may result in 
00284    rendering-system dependent undesirable behavior, such as terrible performance or unreported failure.
00285 
00286    Use of a buffer inconsistent with usage is also not checked by the api, but it may result in bad
00287    performance or even failure.
00288 
00289    Use the Data() function to set buffer data the first time, if possible (it may be faster).
00290 */
00291 
00292 class Buffer : public RefCountBase<Buffer>
00293 {
00294 public:
00295     virtual ~Buffer() {}
00296 
00297     virtual size_t GetSize() = 0;
00298     virtual void*  Map(size_t start, size_t size, int flags = 0) = 0;
00299     virtual bool   Unmap(void *m) = 0;
00300 
00301     // Allocates a buffer, optionally filling it with data.
00302     virtual bool   Data(int use, const void* buffer, size_t size) = 0;
00303 };
00304 
00305 class Texture : public RefCountBase<Texture>
00306 {
00307 public:
00308     virtual ~Texture() {}
00309 
00310     virtual int GetWidth() const = 0;
00311     virtual int GetHeight() const = 0;
00312     virtual int GetSamples() const { return 1; }
00313 
00314     virtual void SetSampleMode(int sm) = 0;
00315     virtual void Set(int slot, ShaderStage stage = Shader_Fragment) const = 0;
00316 };
00317 
00318 
00319 
00320 //-----------------------------------------------------------------------------------
00321 
00322 class CollisionModel : public RefCountBase<CollisionModel>
00323 {
00324 public:
00325         Array<Planef > Planes;
00326 
00327         void Add(const Planef& p)
00328         {
00329                 Planes.PushBack(p);
00330         }
00331 
00332         // Return whether p is inside this
00333         bool TestPoint(const Vector3f& p) const;
00334 
00335         // Assumes that the origin of the ray is outside this.
00336         bool TestRay(const Vector3f& origin, const Vector3f& norm, float& len, Planef* ph = NULL) const;
00337 };
00338 
00339 class Node : public RefCountBase<Node>
00340 {
00341     Vector3f     Pos;
00342     Quatf        Rot;
00343 
00344     mutable Matrix4f  Mat;
00345         mutable bool      MatCurrent;
00346 
00347 public:
00348     Node() : Pos(Vector3f(0)), MatCurrent(1) { }
00349     virtual ~Node() { }
00350 
00351     enum NodeType
00352     {
00353         Node_NonDisplay,
00354         Node_Container,
00355         Node_Model
00356     };
00357     virtual NodeType GetType() const { return Node_NonDisplay; }
00358 
00359     virtual void ClearRenderer() { }
00360 
00361     const Vector3f&  GetPosition() const      { return Pos; }
00362     const Quatf&     GetOrientation() const   { return Rot; }
00363     void             SetPosition(Vector3f p)  { Pos = p; MatCurrent = 0; }
00364     void             SetOrientation(Quatf q)  { Rot = q; MatCurrent = 0; }
00365 
00366     void             Move(Vector3f p)         { Pos += p; MatCurrent = 0; }
00367     void             Rotate(Quatf q)          { Rot = q * Rot; MatCurrent = 0; }
00368 
00369 
00370     // For testing only; causes Position an Orientation
00371     void  SetMatrix(const Matrix4f& m)
00372     {
00373         MatCurrent = true;
00374         Mat = m;        
00375     }
00376 
00377 
00378     const Matrix4f&  GetMatrix() const 
00379     {
00380         if (!MatCurrent)
00381         {
00382             Mat = Rot;
00383             Mat = Matrix4f::Translation(Pos) * Mat;
00384             MatCurrent = 1;
00385         }
00386         return Mat;
00387     }
00388 
00389         virtual void     Render(const Matrix4f& ltw, RenderDevice* ren) { OVR_UNUSED2(ltw, ren); }
00390 };
00391 
00392 struct Vertex
00393 {
00394     Vector3f  Pos;
00395     Color     C;
00396     float     U, V;
00397         float     U2, V2;
00398     Vector3f  Norm;
00399 
00400     Vertex (const Vector3f& p, const Color& c = Color(64,0,0,255), 
00401             float u = 0, float v = 0, Vector3f n = Vector3f(1,0,0))
00402       : Pos(p), C(c), U(u), V(v), Norm(n), U2(u), V2(v) {}
00403     Vertex(float x, float y, float z, const Color& c = Color(64,0,0,255),
00404            float u = 0, float v = 0) : Pos(x,y,z), C(c), U(u), V(v), U2(u), V2(v) { }
00405 
00406         // for multiple UV coords
00407         Vertex(const Vector3f& p, const Color& c,
00408            float u, float v, float u2, float v2, Vector3f n) : Pos(p), C(c), U(u), V(v), U2(u2), V2(v2), Norm(n) { }
00409 
00410     bool operator==(const Vertex& b) const
00411     {
00412         return Pos == b.Pos && C == b.C && U == b.U && V == b.V;
00413     }
00414 };
00415 
00416 // this is stored in a uniform buffer, don't change it without fixing all renderers
00417 struct LightingParams
00418 {
00419     Vector4f Ambient;
00420     Vector4f LightPos[8];
00421     Vector4f LightColor[8];
00422     float    LightCount;
00423     int      Version;
00424 
00425     LightingParams() : LightCount(0), Version(0) {}
00426 
00427     void Update(const Matrix4f& view, const Vector4f* SceneLightPos);
00428 
00429     void Set(ShaderSet* s) const;
00430 };
00431 
00432 //-----------------------------------------------------------------------------------
00433 
00434 class Model : public Node
00435 {
00436 public:
00437     Array<Vertex>     Vertices;
00438     Array<UInt16>     Indices;
00439     PrimitiveType     Type;
00440     Ptr<class Fill>   Fill;
00441     bool              Visible;
00442         bool                      IsCollisionModel;
00443 
00444     // Some renderers will create these if they didn't exist before rendering.
00445     // Currently they are not updated, so vertex data should not be changed after rendering.
00446     Ptr<Buffer>       VertexBuffer;
00447     Ptr<Buffer>       IndexBuffer;
00448 
00449     Model(PrimitiveType t = Prim_Triangles) : Type(t), Fill(NULL), Visible(true) { }
00450     ~Model() { }
00451 
00452     virtual NodeType GetType() const { return Node_Model; }
00453 
00454     virtual void Render(const Matrix4f& ltw, RenderDevice* ren);
00455 
00456     PrimitiveType GetPrimType() const { return Type; }
00457 
00458     void SetVisible(bool visible) { Visible = visible; }
00459     bool IsVisible() const        { return Visible; }
00460 
00461     void ClearRenderer()
00462     {
00463         VertexBuffer.Clear();
00464         IndexBuffer.Clear();
00465     }
00466 
00467     // Returns the index next added vertex will have.
00468     UInt16 GetNextVertexIndex() const
00469     {
00470         return (UInt16)Vertices.GetSize();
00471     }
00472 
00473     UInt16 AddVertex(const Vertex& v)
00474     {
00475         assert(!VertexBuffer && !IndexBuffer);
00476         UInt16 index = (UInt16)Vertices.GetSize();
00477         Vertices.PushBack(v);
00478         return index;
00479     }
00480     UInt16 AddVertex(const Vector3f& v, const Color& c, float u_ = 0, float v_ = 0)
00481     {
00482         return AddVertex(Vertex(v,c,u_,v_));
00483     }
00484     UInt16 AddVertex(float x, float y, float z, const Color& c, float u, float v)
00485     {
00486         return AddVertex(Vertex(Vector3f(x,y,z),c, u,v));
00487     }
00488 
00489     void AddLine(UInt16 a, UInt16 b)
00490     {
00491         Indices.PushBack(a);
00492         Indices.PushBack(b);
00493     }
00494 
00495     UInt16 AddVertex(float x, float y, float z, const Color& c,
00496                      float u, float v, float nx, float ny, float nz)
00497     {
00498         return AddVertex(Vertex(Vector3f(x,y,z),c, u,v, Vector3f(nx,ny,nz)));
00499     }
00500 
00501         UInt16 AddVertex(float x, float y, float z, const Color& c,
00502                      float u1, float v1, float u2, float v2, float nx, float ny, float nz)
00503     {
00504         return AddVertex(Vertex(Vector3f(x,y,z), c, u1, v1, u2, v2, Vector3f(nx,ny,nz)));
00505     }
00506 
00507     void AddLine(const Vertex& a, const Vertex& b)
00508     {
00509         AddLine(AddVertex(a), AddVertex(b));
00510     }
00511 
00512     void AddTriangle(UInt16 a, UInt16 b, UInt16 c)
00513     {
00514         Indices.PushBack(a);
00515         Indices.PushBack(b);
00516         Indices.PushBack(c);
00517     }
00518 
00519 
00520     // Uses texture coordinates for uniform world scaling (must use a repeat sampler).
00521     void  AddSolidColorBox(float x1, float y1, float z1,
00522                            float x2, float y2, float z2,
00523                            Color c);
00524 
00525 
00526     static Model* CreateAxisFaceColorBox(float x1, float x2, Color xcolor,
00527                                          float y1, float y2, Color ycolor,
00528                                          float z1, float z2, Color zcolor);
00529    
00530 
00531 
00532     // Uses texture coordinates for exactly covering each surface once.
00533     static Model* CreateBox(Color c, Vector3f origin, Vector3f size);
00534     static Model* CreateCylinder(Color c, Vector3f origin, float height, float radius, int sides = 20);
00535     static Model* CreateCone(Color c, Vector3f origin, float height, float radius, int sides = 20);
00536     static Model* CreateSphere(Color c, Vector3f origin, float radius, int sides = 20);
00537 
00538     // Grid having halfx,halfy lines in each direction from the origin
00539     static Model* CreateGrid(Vector3f origin, Vector3f stepx, Vector3f stepy,
00540                              int halfx, int halfy, int nmajor = 5,
00541                                                          Color minor = Color(64,64,64,192), Color major = Color(128,128,128,192));
00542 };
00543 
00544 class Container : public Node
00545 {
00546 public:
00547     Array<Ptr<Node> > Nodes;
00548 
00549     ~Container()
00550     {
00551 
00552     }
00553 
00554     void ClearRenderer()
00555     {
00556         for (UPInt i=0; i< Nodes.GetSize(); i++)
00557             Nodes[i]->ClearRenderer();
00558     }
00559 
00560     virtual NodeType GetType() const { return Node_Container; }
00561 
00562     virtual void Render(const Matrix4f& ltw, RenderDevice* ren);
00563 
00564     void Add(Node *n) { Nodes.PushBack(n); }
00565         void Add(Model *n, class Fill *f) { n->Fill = f; Nodes.PushBack(n); }
00566     void RemoveLast() { Nodes.PopBack(); }
00567         void Clear() { Nodes.Clear(); }
00568 
00569         bool               CollideChildren;
00570 
00571         Container() : CollideChildren(1) {}
00572 };
00573 
00574 class Scene
00575 {
00576 public:
00577     Container                   World;
00578     Vector4f                    LightPos[8];
00579     LightingParams              Lighting;
00580         Array<Ptr<Model> >      Models;
00581 
00582 public:
00583     void Render(RenderDevice* ren, const Matrix4f& view);
00584 
00585     void SetAmbient(Vector4f color)
00586     {
00587         Lighting.Ambient = color;
00588     }
00589     void AddLight(Vector3f pos, Vector4f color)
00590     {
00591         int n = (int)Lighting.LightCount;
00592         OVR_ASSERT(n < 8);
00593         LightPos[n] = pos;
00594         Lighting.LightColor[n] = color;
00595         Lighting.LightCount++;
00596     }
00597 
00598         void Clear()
00599         {
00600                 World.Clear();
00601                 Models.Clear();
00602                 Lighting.Ambient = Vector4f(0.0f, 0.0f, 0.0f, 0.0f);
00603                 Lighting.LightCount = 0;
00604         }
00605 
00606     void ClearRenderer()
00607     {
00608         World.ClearRenderer();
00609     }
00610 };
00611 
00612 class SceneView : public Node
00613 {
00614 public:
00615     Matrix4f GetViewMatrix() const;
00616 };
00617 
00618 
00619 //-----------------------------------------------------------------------------------
00620 
00621 enum RenderCaps
00622 {
00623     Cap_VertexBuffer = 1,
00624 };
00625 
00626 // Post-processing type to apply to scene after rendering. PostProcess_Distortion
00627 // applied distortion as described by DistortionConfig.
00628 enum PostProcessType
00629 {
00630     PostProcess_None,
00631     PostProcess_Distortion
00632 };
00633 
00634 enum DisplayMode
00635 {
00636     Display_Window = 0,
00637     Display_Fullscreen = 1,
00638     Display_FakeFullscreen
00639 };
00640     
00641 struct DisplayId
00642 {
00643     // Windows
00644     String MonitorName; // Monitor name for fullscreen mode
00645     
00646     // MacOS
00647     long   CgDisplayId; // CGDirectDisplayID
00648     
00649     DisplayId() : CgDisplayId(0) {}
00650     DisplayId(long id) : CgDisplayId(id) {}
00651     DisplayId(String m, long id=0) : MonitorName(m), CgDisplayId(id) {}
00652     
00653     operator bool () const
00654     {
00655         return MonitorName.GetLength() || CgDisplayId;
00656     }
00657     
00658     bool operator== (const DisplayId& b) const
00659     {
00660         return CgDisplayId == b.CgDisplayId &&
00661             (strstr(MonitorName.ToCStr(), b.MonitorName.ToCStr()) ||
00662              strstr(b.MonitorName.ToCStr(), MonitorName.ToCStr()));
00663     }
00664 };
00665 
00666 struct RendererParams
00667 {
00668     int  Multisample;
00669     int  Fullscreen;
00670     DisplayId Display;
00671 
00672     RendererParams(int ms = 1) : Multisample(ms), Fullscreen(0) {}
00673     
00674     bool IsDisplaySet() const
00675     {
00676         return Display;
00677     }
00678 };
00679 
00680 
00681 
00682 //-----------------------------------------------------------------------------------
00683 // ***** RenderDevice
00684 
00685 class RenderDevice : public RefCountBase<RenderDevice>
00686 {
00687     friend class StereoGeomShaders;
00688 protected:
00689     int                 WindowWidth, WindowHeight;
00690     RendererParams      Params;
00691     Viewport            VP;
00692 
00693     Matrix4f            Proj;
00694     Ptr<Buffer>         pTextVertexBuffer;
00695 
00696 
00697     // For rendering with lens warping
00698     PostProcessType     CurPostProcess;
00699     Ptr<Texture>        pSceneColorTex;
00700     int                 SceneColorTexW;
00701     int                 SceneColorTexH;
00702     Ptr<ShaderSet>      pPostProcessShader;
00703     Ptr<Buffer>         pFullScreenVertexBuffer;
00704     float               SceneRenderScale;
00705     DistortionConfig    Distortion;
00706     Color               DistortionClearColor;
00707     UPInt                           TotalTextureMemoryUsage;
00708 
00709     // For lighting on platforms with uniform buffers
00710     Ptr<Buffer>         LightingBuffer;
00711 
00712     void FinishScene1();
00713 
00714 public:
00715     enum CompareFunc
00716     {
00717         Compare_Always  = 0,
00718         Compare_Less    = 1,
00719         Compare_Greater = 2,
00720         Compare_Count
00721     };
00722     RenderDevice();
00723     virtual ~RenderDevice() { Shutdown(); }
00724 
00725     // This static function is implemented in each derived class
00726     // to support a specific renderer type.
00727     //static RenderDevice* CreateDevice(const RendererParams& rp, void* oswnd);
00728 
00729 
00730     virtual void Init() {}
00731     virtual void Shutdown() {}
00732     virtual bool SetParams(const RendererParams&) { return 0; }
00733 
00734     const RendererParams& GetParams() const { return Params; }
00735 
00736     
00737     // StereoParams apply Viewport, Projection and Distortion simultaneously,
00738     // doing full configuration for one eye.
00739     void        ApplyStereoParams(const StereoEyeParams& params)
00740     {
00741         SetViewport(params.VP);
00742         SetProjection(params.Projection);
00743         if (params.pDistortion)
00744             SetDistortionConfig(*params.pDistortion, params.Eye);
00745     }
00746 
00747     // Apply "orthographic" stereo parameters used for rendering 2D HUD overlays.
00748     void        ApplyStereoParams2D(const StereoEyeParams& params)
00749     {
00750         SetViewport(params.VP);
00751         SetProjection(params.OrthoProjection);
00752         if (params.pDistortion)
00753             SetDistortionConfig(*params.pDistortion, params.Eye);
00754     }
00755 
00756 
00757     virtual void SetViewport(const Viewport& vp);
00758     void         SetViewport(int x, int y, int w, int h) { SetViewport(Viewport(x,y,w,h)); }
00759     //virtual void SetScissor(int x, int y, int w, int h) = 0;
00760 
00761     // Set viewport ignoring any adjustments used for the stereo mode.
00762     virtual void SetRealViewport(const Viewport& vp) { SetMultipleViewports(1, &vp); }
00763     virtual void SetMultipleViewports(int n, const Viewport* vps) { OVR_UNUSED2(n, vps); }
00764 
00765     virtual void Clear(float r = 0, float g = 0, float b = 0, float a = 1, float depth = 1) = 0;
00766     virtual void Rect(float left, float top, float right, float bottom) = 0;
00767 
00768     inline void Clear(const Color &c, float depth = 1)
00769     {
00770         float r, g, b, a;
00771         c.GetRGBA(&r, &g, &b, &a);
00772         Clear(r, g, b, a, depth);
00773     }
00774 
00775     virtual bool IsFullscreen() const { return Params.Fullscreen != Display_Window; }
00776     virtual void Present() = 0;
00777     // Waits for rendering to complete; important for reducing latency.
00778     virtual void ForceFlushGPU() { }
00779 
00780     // Resources
00781     virtual Buffer*  CreateBuffer() { return NULL; }
00782     virtual Texture* CreateTexture(int format, int width, int height, const void* data, int mipcount=1)
00783     { OVR_UNUSED5(format,width,height,data, mipcount); return NULL; }
00784    
00785     virtual bool     GetSamplePositions(Render::Texture*, Vector3f* pos) { pos[0] = Vector3f(0); return 1; }
00786 
00787     virtual ShaderSet* CreateShaderSet() { return new ShaderSetMatrixTranspose; }
00788     virtual Shader* LoadBuiltinShader(ShaderStage stage, int shader) = 0;
00789 
00790     // Rendering
00791 
00792     // Begin drawing directly to the currently selected render target, no post-processing.
00793     virtual void BeginRendering() {}
00794     // Begin drawing the primary scene. This will have post-processing applied (if enabled)
00795     // during FinishScene.
00796     virtual void BeginScene(PostProcessType pp = PostProcess_None); //StereoDisplay disp = Stereo_Center);
00797     // Postprocess the scene and return to the screen render target.
00798     virtual void FinishScene();
00799 
00800     // Texture must have been created with Texture_RenderTarget. Use NULL for the default render target.
00801     // NULL depth buffer means use an internal, temporary one.
00802     virtual void SetRenderTarget(Texture* color, Texture* depth = NULL, Texture* stencil = NULL)
00803     { OVR_UNUSED3(color, depth, stencil); }
00804     virtual void SetDepthMode(bool enable, bool write, CompareFunc func = Compare_Less) = 0;
00805     virtual void SetProjection(const Matrix4f& proj);
00806     virtual void SetWorldUniforms(const Matrix4f& proj) = 0;
00807 
00808     // The data is not copied, it must remain valid until the end of the frame
00809     virtual void SetLighting(const LightingParams* light);
00810 
00811     // The index 0 is reserved for non-buffer uniforms, and so cannot be used with this function.
00812     virtual void SetCommonUniformBuffer(int i, Buffer* buffer) { OVR_UNUSED2(i, buffer); }
00813 
00814     virtual void SetExtraShaders(ShaderSet* s) { OVR_UNUSED(s); }
00815     virtual Matrix4f GetProjection() const { return Proj; }
00816 
00817     // This is a View matrix only, it will be combined with the projection matrix from SetProjection
00818     virtual void Render(const Matrix4f& matrix, Model* model) = 0;
00819     // offset is in bytes; indices can be null.
00820     virtual void Render(const Fill* fill, Buffer* vertices, Buffer* indices,
00821                         const Matrix4f& matrix, int offset, int count, PrimitiveType prim = Prim_Triangles) = 0;
00822     virtual void RenderWithAlpha(const Fill* fill, Render::Buffer* vertices, Render::Buffer* indices,
00823                         const Matrix4f& matrix, int offset, int count, PrimitiveType prim = Prim_Triangles) = 0;
00824 
00825     // Returns width of text in same units as drawing. If strsize is not null, stores width and height.
00826     float        MeasureText(const Font* font, const char* str, float size, float* strsize = NULL);
00827     virtual void RenderText(const Font* font, const char* str, float x, float y, float size, Color c);
00828 
00829     virtual void FillRect(float left, float top, float right, float bottom, Color c);
00830     virtual void FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm);
00831     virtual void RenderImage(float left, float top, float right, float bottom, ShaderFill* image, unsigned char alpha=255);
00832 
00833     virtual Fill *CreateSimpleFill(int flags = Fill::F_Solid) = 0;
00834     Fill *        CreateTextureFill(Texture* tex, bool useAlpha = false);
00835 
00836     // PostProcess distortion
00837     void          SetSceneRenderScale(float ss);
00838 
00839     void          SetDistortionConfig(const DistortionConfig& config, StereoEye eye = StereoEye_Left)
00840     {
00841         Distortion = config;
00842         if (eye == StereoEye_Right)
00843             Distortion.XCenterOffset = -Distortion.XCenterOffset;
00844     }
00845 
00846     // Sets the color that is applied around distortion.
00847     void          SetDistortionClearColor(Color clearColor)
00848     {
00849         DistortionClearColor = clearColor;
00850     }
00851 
00852     // Don't call these directly, use App/Platform instead
00853     virtual bool SetFullscreen(DisplayMode fullscreen) { OVR_UNUSED(fullscreen); return false; }
00854     virtual void SetWindowSize(int w, int h) { WindowWidth = w; WindowHeight = h; }
00855 
00856     UPInt GetTotalTextureMemoryUsage() const
00857     {
00858         return TotalTextureMemoryUsage;
00859     }
00860 
00861     enum PostProcessShader
00862     {
00863         PostProcessShader_Distortion                = 0,
00864         PostProcessShader_DistortionAndChromAb      = 1,
00865         PostProcessShader_Count
00866     };
00867 
00868     PostProcessShader GetPostProcessShader()
00869     {
00870         return PostProcessShaderActive;
00871     }
00872 
00873     void SetPostProcessShader(PostProcessShader newShader)
00874     {
00875         PostProcessShaderRequested = newShader;
00876     }
00877 
00878 protected:
00879     // Stereo & post-processing
00880     virtual bool  initPostProcessSupport(PostProcessType pptype);
00881     
00882     virtual Shader* CreateStereoShader(PrimitiveType prim, Shader* vs)
00883     { OVR_UNUSED2(prim, vs); return NULL; }
00884 
00885 private:
00886     PostProcessShader   PostProcessShaderRequested;
00887     PostProcessShader   PostProcessShaderActive;
00888 };
00889 
00890 int GetNumMipLevels(int w, int h);
00891 int GetTextureSize(int format, int w, int h);
00892 
00893 // Filter an rgba image with a 2x2 box filter, for mipmaps.
00894 // Image size must be a power of 2.
00895 void FilterRgba2x2(const UByte* src, int w, int h, UByte* dest);
00896 
00897 Texture* LoadTextureTga(RenderDevice* ren, File* f, unsigned char alpha = 255);
00898 Texture* LoadTextureDDS(RenderDevice* ren, File* f);
00899 
00900 }}
00901 
00902 #endif


oculus_sdk
Author(s):
autogenerated on Mon Oct 6 2014 03:01:19