00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef OVR_RenderTiny_Device_h
00025 #define OVR_RenderTiny_Device_h
00026
00027 #include "Kernel/OVR_Math.h"
00028 #include "Kernel/OVR_Array.h"
00029 #include "Kernel/OVR_RefCount.h"
00030 #include "Kernel/OVR_String.h"
00031 #include "Kernel/OVR_File.h"
00032 #include "Kernel/OVR_Color.h"
00033
00034 #include "Util/Util_Render_Stereo.h"
00035
00036 namespace OVR { namespace RenderTiny {
00037
00038 using namespace OVR::Util::Render;
00039
00040 class RenderDevice;
00041
00042
00043
00044
00045
00046 enum PrimitiveType
00047 {
00048 Prim_Triangles,
00049 Prim_Lines,
00050 Prim_TriangleStrip,
00051 Prim_Unknown,
00052 Prim_Count
00053 };
00054
00055
00056 enum ShaderStage
00057 {
00058 Shader_Vertex = 0,
00059 Shader_Fragment = 2,
00060 Shader_Pixel = 2,
00061 Shader_Count = 3,
00062 };
00063
00064
00065 enum BuiltinShaders
00066 {
00067 VShader_MV = 0,
00068 VShader_MVP = 1,
00069 VShader_PostProcess = 2,
00070 VShader_Count = 3,
00071
00072 FShader_Solid = 0,
00073 FShader_Gouraud = 1,
00074 FShader_Texture = 2,
00075 FShader_PostProcess = 3,
00076 FShader_PostProcessWithChromAb = 4,
00077 FShader_LitGouraud = 5,
00078 FShader_LitTexture = 6,
00079 FShader_Count
00080 };
00081
00082
00083 enum MapFlags
00084 {
00085 Map_Discard = 1,
00086 Map_Read = 2,
00087 Map_Unsynchronized = 4,
00088 };
00089
00090
00091 enum BufferUsage
00092 {
00093 Buffer_Unknown = 0,
00094 Buffer_Vertex = 1,
00095 Buffer_Index = 2,
00096 Buffer_Uniform = 4,
00097 Buffer_TypeMask = 0xff,
00098 Buffer_ReadOnly = 0x100,
00099 };
00100
00101 enum TextureFormat
00102 {
00103 Texture_RGBA = 0x0100,
00104 Texture_Depth = 0x8000,
00105 Texture_TypeMask = 0xff00,
00106 Texture_SamplesMask = 0x00ff,
00107 Texture_RenderTarget = 0x10000,
00108 Texture_GenMipmaps = 0x20000,
00109 };
00110
00111
00112 enum SampleMode
00113 {
00114 Sample_Linear = 0,
00115 Sample_Nearest = 1,
00116 Sample_Anisotropic = 2,
00117 Sample_FilterMask = 3,
00118
00119 Sample_Repeat = 0,
00120 Sample_Clamp = 4,
00121 Sample_ClampBorder = 8,
00122 Sample_AddressMask =12,
00123
00124 Sample_Count =13,
00125 };
00126
00127
00128
00129 struct Vector4f : public Vector3f
00130 {
00131 float w;
00132
00133 Vector4f() : w(1) {}
00134 Vector4f(const Vector3f& v) : Vector3f(v), w(1) {}
00135 Vector4f(float r, float g, float b, float a) : Vector3f(r,g,b), w(a) {}
00136 };
00137
00138
00139
00140 class Shader : public RefCountBase<Shader>
00141 {
00142 friend class ShaderSet;
00143
00144 protected:
00145 ShaderStage Stage;
00146
00147 public:
00148 Shader(ShaderStage s) : Stage(s) {}
00149 virtual ~Shader() {}
00150
00151 ShaderStage GetStage() const { return Stage; }
00152
00153 virtual void Set(PrimitiveType) const { }
00154 virtual void SetUniformBuffer(class Buffer* buffers, int i = 0) { OVR_UNUSED2(buffers, i); }
00155
00156 protected:
00157 virtual bool SetUniform(const char* name, int n, const float* v) { OVR_UNUSED3(name, n, v); return false; }
00158 };
00159
00160
00161
00162
00163 class ShaderSet : public RefCountBase<ShaderSet>
00164 {
00165 protected:
00166 Ptr<Shader> Shaders[Shader_Count];
00167
00168 public:
00169 ShaderSet() { }
00170 ~ShaderSet() { }
00171
00172 virtual void SetShader(Shader *s)
00173 {
00174 Shaders[s->GetStage()] = s;
00175 }
00176 virtual void UnsetShader(int stage)
00177 {
00178 Shaders[stage] = NULL;
00179 }
00180 Shader* GetShader(int stage) { return Shaders[stage]; }
00181
00182 virtual void Set(PrimitiveType prim) const
00183 {
00184 for (int i = 0; i < Shader_Count; i++)
00185 if (Shaders[i])
00186 Shaders[i]->Set(prim);
00187 }
00188
00189
00190
00191
00192 virtual bool SetUniform(const char* name, int n, const float* v)
00193 {
00194 bool result = 0;
00195 for (int i = 0; i < Shader_Count; i++)
00196 if (Shaders[i])
00197 result |= Shaders[i]->SetUniform(name, n, v);
00198
00199 return result;
00200 }
00201 bool SetUniform1f(const char* name, float x)
00202 {
00203 const float v[] = {x};
00204 return SetUniform(name, 1, v);
00205 }
00206 bool SetUniform2f(const char* name, float x, float y)
00207 {
00208 const float v[] = {x,y};
00209 return SetUniform(name, 2, v);
00210 }
00211 bool SetUniform4f(const char* name, float x, float y, float z, float w = 1)
00212 {
00213 const float v[] = {x,y,z,w};
00214 return SetUniform(name, 4, v);
00215 }
00216 bool SetUniformv(const char* name, const Vector3f& v)
00217 {
00218 const float a[] = {v.x,v.y,v.z,1};
00219 return SetUniform(name, 4, a);
00220 }
00221 bool SetUniform4fv(const char* name, int n, const Vector4f* v)
00222 {
00223 return SetUniform(name, 4*n, &v[0].x);
00224 }
00225 virtual bool SetUniform4x4f(const char* name, const Matrix4f& m)
00226 {
00227 Matrix4f mt = m.Transposed();
00228 return SetUniform(name, 16, &mt.M[0][0]);
00229 }
00230 };
00231
00232
00233
00234
00235 class ShaderFill : public RefCountBase<ShaderFill>
00236 {
00237 Ptr<ShaderSet> Shaders;
00238 Ptr<class Texture> Textures[8];
00239
00240 public:
00241 ShaderFill(ShaderSet* sh) : Shaders(sh) { }
00242 ShaderFill(ShaderSet& sh) : Shaders(sh) { }
00243
00244 ShaderSet* GetShaders() { return Shaders; }
00245
00246 virtual void Set(PrimitiveType prim = Prim_Unknown) const;
00247 virtual void SetTexture(int i, class Texture* tex) { if (i < 8) Textures[i] = tex; }
00248 };
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 class Buffer : public RefCountBase<Buffer>
00264 {
00265 public:
00266 virtual ~Buffer() {}
00267
00268 virtual size_t GetSize() = 0;
00269 virtual void* Map(size_t start, size_t size, int flags = 0) = 0;
00270 virtual bool Unmap(void *m) = 0;
00271
00272
00273 virtual bool Data(int use, const void* buffer, size_t size) = 0;
00274 };
00275
00276 class Texture : public RefCountBase<Texture>
00277 {
00278 public:
00279 virtual ~Texture() {}
00280
00281 virtual int GetWidth() const = 0;
00282 virtual int GetHeight() const = 0;
00283 virtual int GetSamples() const { return 1; }
00284
00285 virtual void SetSampleMode(int sm) = 0;
00286 virtual void Set(int slot, ShaderStage stage = Shader_Fragment) const = 0;
00287 };
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297 class Node : public RefCountBase<Node>
00298 {
00299 Vector3f Pos;
00300 Quatf Rot;
00301
00302 mutable Matrix4f Mat;
00303 mutable bool MatCurrent;
00304
00305 public:
00306 Node() : Pos(Vector3f(0)), MatCurrent(1) { }
00307 virtual ~Node() { }
00308
00309 enum NodeType
00310 {
00311 Node_NonDisplay,
00312 Node_Container,
00313 Node_Model
00314 };
00315 virtual NodeType GetType() const { return Node_NonDisplay; }
00316
00317 const Vector3f& GetPosition() const { return Pos; }
00318 const Quatf& GetOrientation() const { return Rot; }
00319 void SetPosition(Vector3f p) { Pos = p; MatCurrent = 0; }
00320 void SetOrientation(Quatf q) { Rot = q; MatCurrent = 0; }
00321
00322 void Move(Vector3f p) { Pos += p; MatCurrent = 0; }
00323 void Rotate(Quatf q) { Rot = q * Rot; MatCurrent = 0; }
00324
00325
00326
00327 void SetMatrix(const Matrix4f& m)
00328 {
00329 MatCurrent = true;
00330 Mat = m;
00331 }
00332
00333 const Matrix4f& GetMatrix() const
00334 {
00335 if (!MatCurrent)
00336 {
00337 Mat = Rot;
00338 Mat = Matrix4f::Translation(Pos) * Mat;
00339 MatCurrent = 1;
00340 }
00341 return Mat;
00342 }
00343
00344 virtual void Render(const Matrix4f& ltw, RenderDevice* ren) { OVR_UNUSED2(ltw, ren); }
00345 };
00346
00347
00348
00349
00350 struct Vertex
00351 {
00352 Vector3f Pos;
00353 Color C;
00354 float U, V;
00355 Vector3f Norm;
00356
00357 Vertex (const Vector3f& p, const Color& c = Color(64,0,0,255),
00358 float u = 0, float v = 0, Vector3f n = Vector3f(1,0,0))
00359 : Pos(p), C(c), U(u), V(v), Norm(n)
00360 {}
00361 Vertex(float x, float y, float z, const Color& c = Color(64,0,0,255),
00362 float u = 0, float v = 0) : Pos(x,y,z), C(c), U(u), V(v)
00363 { }
00364
00365 bool operator==(const Vertex& b) const
00366 {
00367 return Pos == b.Pos && C == b.C && U == b.U && V == b.V;
00368 }
00369 };
00370
00371
00372
00373 struct LightingParams
00374 {
00375 Vector4f Ambient;
00376 Vector4f LightPos[8];
00377 Vector4f LightColor[8];
00378 float LightCount;
00379 int Version;
00380
00381 LightingParams() : LightCount(0), Version(0) {}
00382
00383
00384 void Update(const Matrix4f& view, const Vector4f* SceneLightPos)
00385 {
00386 Version++;
00387 for (int i = 0; i < LightCount; i++)
00388 {
00389 LightPos[i] = view.Transform(SceneLightPos[i]);
00390 }
00391 }
00392
00393 void Set(ShaderSet* s) const
00394 {
00395 s->SetUniform4fv("Ambient", 1, &Ambient);
00396 s->SetUniform1f("LightCount", LightCount);
00397 s->SetUniform4fv("LightPos", (int)LightCount, LightPos);
00398 s->SetUniform4fv("LightColor", (int)LightCount, LightColor);
00399 }
00400
00401 };
00402
00403
00404
00405
00406
00407 class Model : public Node
00408 {
00409 public:
00410 Array<Vertex> Vertices;
00411 Array<UInt16> Indices;
00412 PrimitiveType Type;
00413 Ptr<ShaderFill> Fill;
00414 bool Visible;
00415
00416
00417
00418 Ptr<Buffer> VertexBuffer;
00419 Ptr<Buffer> IndexBuffer;
00420
00421 Model(PrimitiveType t = Prim_Triangles) : Type(t), Fill(NULL), Visible(true) { }
00422 ~Model() { }
00423
00424 PrimitiveType GetPrimType() const { return Type; }
00425
00426 void SetVisible(bool visible) { Visible = visible; }
00427 bool IsVisible() const { return Visible; }
00428
00429
00430
00431 virtual NodeType GetType() const { return Node_Model; }
00432 virtual void Render(const Matrix4f& ltw, RenderDevice* ren);
00433
00434
00435
00436 UInt16 GetNextVertexIndex() const
00437 {
00438 return (UInt16)Vertices.GetSize();
00439 }
00440
00441 UInt16 AddVertex(const Vertex& v)
00442 {
00443 assert(!VertexBuffer && !IndexBuffer);
00444 UInt16 index = (UInt16)Vertices.GetSize();
00445 Vertices.PushBack(v);
00446 return index;
00447 }
00448
00449 void AddTriangle(UInt16 a, UInt16 b, UInt16 c)
00450 {
00451 Indices.PushBack(a);
00452 Indices.PushBack(b);
00453 Indices.PushBack(c);
00454 }
00455
00456
00457 void AddSolidColorBox(float x1, float y1, float z1,
00458 float x2, float y2, float z2,
00459 Color c);
00460 };
00461
00462
00463
00464 class Container : public Node
00465 {
00466 public:
00467 Array<Ptr<Node> > Nodes;
00468
00469 Container() { }
00470 ~Container() { }
00471
00472 virtual NodeType GetType() const { return Node_Container; }
00473
00474 virtual void Render(const Matrix4f& ltw, RenderDevice* ren);
00475
00476 void Add(Node *n) { Nodes.PushBack(n); }
00477 void Clear() { Nodes.Clear(); }
00478 };
00479
00480
00481
00482 class Scene
00483 {
00484 public:
00485 Container World;
00486 Vector4f LightPos[8];
00487 LightingParams Lighting;
00488
00489 public:
00490 void Render(RenderDevice* ren, const Matrix4f& view);
00491
00492 void SetAmbient(Vector4f color)
00493 {
00494 Lighting.Ambient = color;
00495 }
00496
00497 void AddLight(Vector3f pos, Vector4f color)
00498 {
00499 int n = (int)Lighting.LightCount;
00500 OVR_ASSERT(n < 8);
00501 LightPos[n] = pos;
00502 Lighting.LightColor[n] = color;
00503 Lighting.LightCount++;
00504 }
00505
00506 void Clear()
00507 {
00508 World.Clear();
00509 Lighting.Ambient = Vector4f(0.0f, 0.0f, 0.0f, 0.0f);
00510 Lighting.LightCount = 0;
00511 }
00512 };
00513
00514
00515
00516
00517
00518
00519 enum PostProcessType
00520 {
00521 PostProcess_None,
00522 PostProcess_Distortion
00523 };
00524
00525 enum DisplayMode
00526 {
00527 Display_Window = 0,
00528 Display_Fullscreen = 1
00529 };
00530
00531
00532
00533 struct RendererParams
00534 {
00535 int Multisample;
00536 int Fullscreen;
00537
00538
00539 String MonitorName;
00540
00541 long DisplayId;
00542
00543 RendererParams(int ms = 1) : Multisample(ms), Fullscreen(0) {}
00544
00545 bool IsDisplaySet() const
00546 {
00547 return MonitorName.GetLength() || DisplayId;
00548 }
00549 };
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560 class RenderDevice : public RefCountBase<RenderDevice>
00561 {
00562 protected:
00563 int WindowWidth, WindowHeight;
00564 RendererParams Params;
00565 Viewport VP;
00566
00567 Matrix4f Proj;
00568 Ptr<Buffer> pTextVertexBuffer;
00569
00570
00571 PostProcessType CurPostProcess;
00572 Ptr<Texture> pSceneColorTex;
00573 int SceneColorTexW;
00574 int SceneColorTexH;
00575 Ptr<ShaderSet> pPostProcessShader;
00576 Ptr<Buffer> pFullScreenVertexBuffer;
00577 float SceneRenderScale;
00578 DistortionConfig Distortion;
00579
00580
00581 Ptr<Buffer> LightingBuffer;
00582
00583 void FinishScene1();
00584
00585 public:
00586 enum CompareFunc
00587 {
00588 Compare_Always = 0,
00589 Compare_Less = 1,
00590 Compare_Greater = 2,
00591 Compare_Count
00592 };
00593 RenderDevice();
00594 virtual ~RenderDevice() { Shutdown(); }
00595
00596
00597
00598
00599
00600
00601 virtual void Init() {}
00602 virtual void Shutdown() {}
00603 virtual bool SetParams(const RendererParams&) { return 0; }
00604
00605 const RendererParams& GetParams() const { return Params; }
00606
00607
00608
00609
00610 void ApplyStereoParams(const StereoEyeParams& params)
00611 {
00612 SetViewport(params.VP);
00613 SetProjection(params.Projection);
00614 if (params.pDistortion)
00615 SetDistortionConfig(*params.pDistortion, params.Eye);
00616 }
00617
00618 virtual void SetViewport(const Viewport& vp);
00619 void SetViewport(int x, int y, int w, int h) { SetViewport(Viewport(x,y,w,h)); }
00620
00621
00622 void SetSceneRenderScale(float ss);
00623
00624 void SetDistortionConfig(const DistortionConfig& config, StereoEye eye = StereoEye_Left)
00625 {
00626 Distortion = config;
00627 if (eye == StereoEye_Right)
00628 Distortion.XCenterOffset = -Distortion.XCenterOffset;
00629 }
00630
00631
00632 virtual void SetRealViewport(const Viewport& vp) = 0;
00633
00634 virtual void Clear(float r = 0, float g = 0, float b = 0, float a = 1, float depth = 1) = 0;
00635
00636 virtual bool IsFullscreen() const { return Params.Fullscreen != Display_Window; }
00637 virtual void Present() = 0;
00638
00639 virtual void ForceFlushGPU() { }
00640
00641
00642 virtual Buffer* CreateBuffer() { return NULL; }
00643 virtual Texture* CreateTexture(int format, int width, int height, const void* data, int mipcount=1)
00644 { OVR_UNUSED5(format,width,height,data, mipcount); return NULL; }
00645
00646
00647 virtual ShaderSet* CreateShaderSet() { return new ShaderSet; }
00648 virtual Shader* LoadBuiltinShader(ShaderStage stage, int shader) = 0;
00649
00650
00651
00652
00653 virtual void BeginRendering() {}
00654
00655
00656 virtual void BeginScene(PostProcessType pp = PostProcess_None);
00657
00658 virtual void FinishScene();
00659
00660
00661
00662 virtual void SetRenderTarget(Texture* color, Texture* depth = NULL, Texture* stencil = NULL)
00663 { OVR_UNUSED3(color, depth, stencil); }
00664 virtual void SetDepthMode(bool enable, bool write, CompareFunc func = Compare_Less) = 0;
00665 virtual void SetProjection(const Matrix4f& proj);
00666 virtual void SetWorldUniforms(const Matrix4f& proj) = 0;
00667
00668
00669 virtual void SetLighting(const LightingParams* light);
00670
00671
00672 virtual void SetCommonUniformBuffer(int i, Buffer* buffer) { OVR_UNUSED2(i, buffer); }
00673
00674 virtual Matrix4f GetProjection() const { return Proj; }
00675
00676
00677 virtual void Render(const Matrix4f& matrix, Model* model) = 0;
00678
00679 virtual void Render(const ShaderFill* fill, Buffer* vertices, Buffer* indices,
00680 const Matrix4f& matrix, int offset, int count, PrimitiveType prim = Prim_Triangles) = 0;
00681
00682 virtual ShaderFill *CreateSimpleFill() = 0;
00683 ShaderFill * CreateTextureFill(Texture* tex);
00684
00685
00686
00687 virtual bool SetFullscreen(DisplayMode fullscreen) { OVR_UNUSED(fullscreen); return false; }
00688
00689
00690 enum PostProcessShader
00691 {
00692 PostProcessShader_Distortion = 0,
00693 PostProcessShader_DistortionAndChromAb = 1,
00694 PostProcessShader_Count
00695 };
00696
00697 PostProcessShader GetPostProcessShader()
00698 {
00699 return PostProcessShaderActive;
00700 }
00701
00702 void SetPostProcessShader(PostProcessShader newShader)
00703 {
00704 PostProcessShaderRequested = newShader;
00705 }
00706
00707 protected:
00708
00709 virtual bool initPostProcessSupport(PostProcessType pptype);
00710
00711 private:
00712 PostProcessShader PostProcessShaderRequested;
00713 PostProcessShader PostProcessShaderActive;
00714 };
00715
00716 int GetNumMipLevels(int w, int h);
00717
00718
00719
00720 void FilterRgba2x2(const UByte* src, int w, int h, UByte* dest);
00721
00722 }}
00723
00724 #endif