diff options
author | Brad Davis <[email protected]> | 2014-07-24 16:47:31 -0700 |
---|---|---|
committer | Brad Davis <[email protected]> | 2014-07-24 16:47:31 -0700 |
commit | 0f49ce8fc6aa54224e4c0d6fda8c4527ad39cce1 (patch) | |
tree | da07ebc6a7f75185bda857dd5f1c34710b416a93 /Samples/CommonSrc/Render | |
parent | ca79271759ff7eecd22ec5c4db438370fe51d687 (diff) |
0.4 Win-Beta0.4.0
Diffstat (limited to 'Samples/CommonSrc/Render')
-rw-r--r-- | Samples/CommonSrc/Render/Render_D3D1X_Device.cpp | 526 | ||||
-rw-r--r-- | Samples/CommonSrc/Render/Render_D3D1X_Device.h | 15 | ||||
-rw-r--r-- | Samples/CommonSrc/Render/Render_Device.cpp | 143 | ||||
-rw-r--r-- | Samples/CommonSrc/Render/Render_Device.h | 87 | ||||
-rw-r--r-- | Samples/CommonSrc/Render/Render_GL_Device.cpp | 106 | ||||
-rw-r--r-- | Samples/CommonSrc/Render/Render_GL_Device.h | 3 | ||||
-rw-r--r-- | Samples/CommonSrc/Render/Render_GL_Win32_Device.cpp | 10 | ||||
-rw-r--r-- | Samples/CommonSrc/Render/Render_GL_Win32_Device.h | 5 | ||||
-rw-r--r-- | Samples/CommonSrc/Render/Render_LoadTextureDDS.cpp | 60 | ||||
-rw-r--r-- | Samples/CommonSrc/Render/Render_XmlSceneLoader.cpp | 38 |
10 files changed, 688 insertions, 305 deletions
diff --git a/Samples/CommonSrc/Render/Render_D3D1X_Device.cpp b/Samples/CommonSrc/Render/Render_D3D1X_Device.cpp index 5476a6f..3f645ab 100644 --- a/Samples/CommonSrc/Render/Render_D3D1X_Device.cpp +++ b/Samples/CommonSrc/Render/Render_D3D1X_Device.cpp @@ -26,6 +26,9 @@ limitations under the License. #include "Kernel/OVR_Log.h" #include "Kernel/OVR_Std.h" +#define WIN32_LEAN_AND_MEAN +#include <comdef.h> + #include "Render_D3D1X_Device.h" #include "Util/Util_ImageWindow.h" @@ -159,7 +162,7 @@ static const char* MultiTexturePixelShaderSrc = " if (color2.a <= 0.4)\n" " discard;\n" // go to back to gamma space space colors (assume gamma 2.0 for speed) - " return float4(sqrt(color2.rgb), color2.a);\n" + " return float4(sqrt(color2.rgb) / color2.a, 1);\n" "}\n"; #define LIGHTING_COMMON \ @@ -225,6 +228,24 @@ static const char* AlphaTexturePixelShaderSrc = " finalColor.rgb *= finalColor.a;\n" " return finalColor;\n" "}\n"; + +static const char* AlphaBlendedTexturePixelShaderSrc = + "Texture2D Texture : register(t0);\n" + "SamplerState Linear : register(s0);\n" + "struct Varyings\n" + "{\n" + " float4 Position : SV_Position;\n" + " float4 Color : COLOR0;\n" + " float2 TexCoord : TEXCOORD0;\n" + "};\n" + "float4 main(in Varyings ov) : SV_Target\n" + "{\n" + " float4 finalColor = ov.Color;\n" + " finalColor *= Texture.Sample(Linear, ov.TexCoord);\n" + // blend state expects premultiplied alpha + " finalColor.rgb *= finalColor.a;\n" + " return finalColor;\n" + "}\n"; #pragma endregion #pragma region Distortion shaders @@ -662,7 +683,8 @@ static const char* FShaderSrcs[FShader_Count] = SolidPixelShaderSrc, GouraudPixelShaderSrc, TexturePixelShaderSrc, - AlphaTexturePixelShaderSrc, + AlphaTexturePixelShaderSrc, + AlphaBlendedTexturePixelShaderSrc, PostProcessPixelShaderWithChromAbSrc, LitSolidPixelShaderSrc, LitTexturePixelShaderSrc, @@ -673,32 +695,96 @@ static const char* FShaderSrcs[FShader_Count] = PostProcessHeightmapTimewarpPixelShaderSrc }; +#ifdef OVR_BUILD_DEBUG + +static void ReportCOMError(HRESULT hr, const char* file, int line) +{ + if (FAILED(hr)) + { + _com_error err(hr); + LPCTSTR errMsg = err.ErrorMessage(); + + if (sizeof(TCHAR) == sizeof(char)) + { + LogError("[D3D] Error in %s on line %d : %s", file, line, errMsg); + } + else + { + size_t len = wcslen(errMsg); + char* data = new char[len + 1]; + size_t count = len; + wcstombs_s(&count, data, len + 1, errMsg, len); + if (count < len) + { + len = count; + } + data[len] = '\0'; + LogError("[D3D] Error in %s on line %d : %s", file, line, data); + delete[] data; + } + + OVR_ASSERT(false); + } +} + +#define OVR_LOG_COM_ERROR(hr) \ + ReportCOMError(hr, __FILE__, __LINE__); + +#else + +#define OVR_LOG_COM_ERROR(hr) ; + +#endif + RenderDevice::RenderDevice(const RendererParams& p, HWND window) { - RECT rc; - GetClientRect(window, &rc); - UINT width = rc.right - rc.left; - UINT height = rc.bottom - rc.top; - ::OVR::Render::RenderDevice::SetWindowSize(width, height); + HRESULT hr; + RECT rc; + if (p.Resolution == Sizei(0)) + { + GetClientRect(window, &rc); + UINT width = rc.right - rc.left; + UINT height = rc.bottom - rc.top; + ::OVR::Render::RenderDevice::SetWindowSize(width, height); + } + else + { + // TBD: This should be renamed to not be tied to window for App mode. + ::OVR::Render::RenderDevice::SetWindowSize(p.Resolution.w, p.Resolution.h); + } + Window = window; Params = p; - HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&DXGIFactory.GetRawRef())); - if (FAILED(hr)) + DXGIFactory = NULL; + hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&DXGIFactory.GetRawRef())); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); return; + } // Find the adapter & output (monitor) to use for fullscreen, based on the reported name of the HMD's monitor. if (Params.Display.MonitorName.GetLength() > 0) { for(UINT AdapterIndex = 0; ; AdapterIndex++) { + Adapter = NULL; HRESULT hr = DXGIFactory->EnumAdapters(AdapterIndex, &Adapter.GetRawRef()); if (hr == DXGI_ERROR_NOT_FOUND) break; + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } DXGI_ADAPTER_DESC Desc; - Adapter->GetDesc(&Desc); + hr = Adapter->GetDesc(&Desc); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } UpdateMonitorOutputs(); @@ -712,25 +798,37 @@ RenderDevice::RenderDevice(const RendererParams& p, HWND window) if (!Adapter) { - DXGIFactory->EnumAdapters(0, &Adapter.GetRawRef()); + hr = DXGIFactory->EnumAdapters(0, &Adapter.GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } UpdateMonitorOutputs(); } int flags = D3D10_CREATE_DEVICE_BGRA_SUPPORT; //0; #if (OVR_D3D_VERSION == 10) + Device = NULL; hr = D3D10CreateDevice1(Adapter, D3D10_DRIVER_TYPE_HARDWARE, NULL, flags, D3D10_FEATURE_LEVEL_10_1, D3D10_1_SDK_VERSION, &Device.GetRawRef()); Context = Device; Context->AddRef(); #else //11 + Device = NULL; + Context = NULL; D3D_FEATURE_LEVEL featureLevel; // TODO: Limit certain features based on D3D feature level hr = D3D11CreateDevice(Adapter, Adapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE, NULL, flags, NULL, 0, D3D1x_(SDK_VERSION), &Device.GetRawRef(), &featureLevel, &Context.GetRawRef()); #endif - if (FAILED(hr)) - return; + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + LogError("[D3D1X] Unable to create device: %x", hr); + OVR_ASSERT(false); + return; + } if (!RecreateSwapChain()) return; @@ -769,28 +867,40 @@ RenderDevice::RenderDevice(const RendererParams& p, HWND window) } } - SPInt bufferSize = vsData->GetBufferSize(); + intptr_t bufferSize = vsData->GetBufferSize(); const void* buffer = vsData->GetBufferPointer(); + ModelVertexIL = NULL; ID3D1xInputLayout** objRef = &ModelVertexIL.GetRawRef(); - HRESULT validate = Device->CreateInputLayout(ModelVertexDesc, sizeof(ModelVertexDesc)/sizeof(ModelVertexDesc[0]), buffer, bufferSize, objRef); - OVR_UNUSED(validate); + hr = Device->CreateInputLayout(ModelVertexDesc, sizeof(ModelVertexDesc)/sizeof(ModelVertexDesc[0]), buffer, bufferSize, objRef); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } { ID3D10Blob* vsData2 = CompileShader("vs_4_1", PostProcessMeshVertexShaderSrc); - SPInt bufferSize2 = vsData2->GetBufferSize(); + intptr_t bufferSize2 = vsData2->GetBufferSize(); const void* buffer2 = vsData2->GetBufferPointer(); + DistortionVertexIL = NULL; ID3D1xInputLayout** objRef2 = &DistortionVertexIL.GetRawRef(); - HRESULT validate2 = Device->CreateInputLayout(DistortionVertexDesc, sizeof(DistortionVertexDesc)/sizeof(DistortionVertexDesc[0]), buffer2, bufferSize2, objRef2); - OVR_UNUSED(validate2); + hr = Device->CreateInputLayout(DistortionVertexDesc, sizeof(DistortionVertexDesc)/sizeof(DistortionVertexDesc[0]), buffer2, bufferSize2, objRef2); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } } { ID3D10Blob* vsData2 = CompileShader("vs_4_1", PostProcessHeightmapTimewarpVertexShaderSrc); - SPInt bufferSize2 = vsData2->GetBufferSize(); + intptr_t bufferSize2 = vsData2->GetBufferSize(); const void* buffer2 = vsData2->GetBufferPointer(); + HeightmapVertexIL = NULL; ID3D1xInputLayout** objRef2 = &HeightmapVertexIL.GetRawRef(); - HRESULT validate2 = Device->CreateInputLayout(HeightmapVertexDesc, sizeof(HeightmapVertexDesc)/sizeof(HeightmapVertexDesc[0]), buffer2, bufferSize2, objRef2); - OVR_UNUSED(validate2); + hr = Device->CreateInputLayout(HeightmapVertexDesc, sizeof(HeightmapVertexDesc)/sizeof(HeightmapVertexDesc[0]), buffer2, bufferSize2, objRef2); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } } Ptr<ShaderSet> gouraudShaders = *new ShaderSet(); @@ -806,7 +916,8 @@ RenderDevice::RenderDevice(const RendererParams& p, HWND window) bm.SrcBlend = bm.SrcBlendAlpha = D3D1x_(BLEND_ONE); //premultiplied alpha bm.DestBlend = bm.DestBlendAlpha = D3D1x_(BLEND_INV_SRC_ALPHA); bm.RenderTargetWriteMask[0] = D3D1x_(COLOR_WRITE_ENABLE_ALL); - Device->CreateBlendState(&bm, &BlendState.GetRawRef()); + BlendState = NULL; + hr = Device->CreateBlendState(&bm, &BlendState.GetRawRef()); #else D3D1x_(BLEND_DESC) bm; memset(&bm, 0, sizeof(bm)); @@ -815,8 +926,13 @@ RenderDevice::RenderDevice(const RendererParams& p, HWND window) bm.RenderTarget[0].SrcBlend = bm.RenderTarget[0].SrcBlendAlpha = D3D1x_(BLEND_ONE); //premultiplied alpha bm.RenderTarget[0].DestBlend = bm.RenderTarget[0].DestBlendAlpha = D3D1x_(BLEND_INV_SRC_ALPHA); bm.RenderTarget[0].RenderTargetWriteMask = D3D1x_(COLOR_WRITE_ENABLE_ALL); - Device->CreateBlendState(&bm, &BlendState.GetRawRef()); + BlendState = NULL; + hr = Device->CreateBlendState(&bm, &BlendState.GetRawRef()); #endif + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } D3D1x_(RASTERIZER_DESC) rs; memset(&rs, 0, sizeof(rs)); @@ -825,13 +941,21 @@ RenderDevice::RenderDevice(const RendererParams& p, HWND window) // rs.CullMode = D3D1x_(CULL_NONE); rs.DepthClipEnable = true; rs.FillMode = D3D1x_(FILL_SOLID); - Device->CreateRasterizerState(&rs, &Rasterizer.GetRawRef()); + Rasterizer = NULL; + hr = Device->CreateRasterizerState(&rs, &Rasterizer.GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } QuadVertexBuffer = *CreateBuffer(); const Render::Vertex QuadVertices[] = { Vertex(Vector3f(0, 1, 0)), Vertex(Vector3f(1, 1, 0)), Vertex(Vector3f(0, 0, 0)), Vertex(Vector3f(1, 0, 0)) }; - QuadVertexBuffer->Data(Buffer_Vertex | Buffer_ReadOnly, QuadVertices, sizeof(QuadVertices)); + if (!QuadVertexBuffer->Data(Buffer_Vertex | Buffer_ReadOnly, QuadVertices, sizeof(QuadVertices))) + { + OVR_ASSERT(false); + } SetDepthMode(0, 0); } @@ -840,7 +964,11 @@ RenderDevice::~RenderDevice() { if (SwapChain && Params.Fullscreen) { - SwapChain->SetFullscreenState(false, NULL); + HRESULT hr = SwapChain->SetFullscreenState(false, NULL); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } } } @@ -848,7 +976,25 @@ RenderDevice::~RenderDevice() // Implement static initializer function to create this class. Render::RenderDevice* RenderDevice::CreateDevice(const RendererParams& rp, void* oswnd) { - return new RenderDevice(rp, (HWND)oswnd); +#if (OVR_D3D_VERSION == 10) + Render::D3D10::RenderDevice* render = new RenderDevice(rp, (HWND)oswnd); +#else + Render::D3D11::RenderDevice* render = new RenderDevice(rp, (HWND)oswnd); +#endif + // Sanity check to make sure our resources were created. + // This should stop a lot of driver related crashes we have experienced + if ((render->DXGIFactory == NULL) || (render->Device == NULL) || (render->SwapChain == NULL)) + { + OVR_ASSERT(false); + // TBD: Probabaly other things like shader creation should be verified as well + render->Shutdown(); + render->Release(); + return NULL; + } + else + { + return render; + } } @@ -861,16 +1007,16 @@ BOOL CALLBACK MonitorEnumFunc(HMONITOR hMonitor, HDC, LPRECT, LPARAM dwData) { RenderDevice* renderer = (RenderDevice*)dwData; - MONITORINFOEX monitor; + MONITORINFOEXA monitor; monitor.cbSize = sizeof(monitor); - if (::GetMonitorInfo(hMonitor, &monitor) && monitor.szDevice[0]) + if (::GetMonitorInfoA(hMonitor, &monitor) && monitor.szDevice[0]) { - DISPLAY_DEVICE dispDev; + DISPLAY_DEVICEA dispDev; memset(&dispDev, 0, sizeof(dispDev)); dispDev.cb = sizeof(dispDev); - if (::EnumDisplayDevices(monitor.szDevice, 0, &dispDev, 0)) + if (::EnumDisplayDevicesA(monitor.szDevice, 0, &dispDev, 0)) { if (strstr(String(dispDev.DeviceName).ToCStr(), renderer->GetParams().Display.MonitorName.ToCStr())) { @@ -895,17 +1041,27 @@ void RenderDevice::UpdateMonitorOutputs(bool needRecreate) // to get latest info about monitors. if (SwapChain) { - SwapChain->SetFullscreenState(FALSE, NULL); - SwapChain->Release(); + hr = SwapChain->SetFullscreenState(FALSE, NULL); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } SwapChain = NULL; } DXGIFactory = NULL; Adapter = NULL; hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&DXGIFactory.GetRawRef())); - if (FAILED(hr)) + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); return; - DXGIFactory->EnumAdapters(0, &Adapter.GetRawRef()); + } + hr = DXGIFactory->EnumAdapters(0, &Adapter.GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } } bool deviceNameFound = false; @@ -918,19 +1074,23 @@ void RenderDevice::UpdateMonitorOutputs(bool needRecreate) { break; } + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } DXGI_OUTPUT_DESC OutDesc; Output->GetDesc(&OutDesc); - MONITORINFOEX monitor; + MONITORINFOEXA monitor; monitor.cbSize = sizeof(monitor); - if (::GetMonitorInfo(OutDesc.Monitor, &monitor) && monitor.szDevice[0]) + if (::GetMonitorInfoA(OutDesc.Monitor, &monitor) && monitor.szDevice[0]) { - DISPLAY_DEVICE dispDev; + DISPLAY_DEVICEA dispDev; memset(&dispDev, 0, sizeof(dispDev)); dispDev.cb = sizeof(dispDev); - if (::EnumDisplayDevices(monitor.szDevice, 0, &dispDev, 0)) + if (::EnumDisplayDevicesA(monitor.szDevice, 0, &dispDev, 0)) { if (strstr(String(dispDev.DeviceName).ToCStr(), Params.Display.MonitorName.ToCStr())) { @@ -946,19 +1106,23 @@ void RenderDevice::UpdateMonitorOutputs(bool needRecreate) if (!deviceNameFound && !Params.Display.MonitorName.IsEmpty()) { - EnumDisplayMonitors(0, 0, MonitorEnumFunc, (LPARAM)this); + if (!EnumDisplayMonitors(0, 0, MonitorEnumFunc, (LPARAM)this)) + { + OVR_ASSERT(false); + } } } bool RenderDevice::RecreateSwapChain() { + HRESULT hr; + DXGI_SWAP_CHAIN_DESC scDesc; memset(&scDesc, 0, sizeof(scDesc)); scDesc.BufferCount = 1; scDesc.BufferDesc.Width = WindowWidth; scDesc.BufferDesc.Height = WindowHeight; scDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - //scDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; // Use default refresh rate; switching rate on CC prototype can cause screen lockup. scDesc.BufferDesc.RefreshRate.Numerator = 0; scDesc.BufferDesc.RefreshRate.Denominator = 1; @@ -971,29 +1135,42 @@ bool RenderDevice::RecreateSwapChain() if (SwapChain) { - SwapChain->SetFullscreenState(FALSE, NULL); - SwapChain->Release(); - SwapChain = NULL; + hr = SwapChain->SetFullscreenState(FALSE, NULL); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } + SwapChain = NULL; } Ptr<IDXGISwapChain> newSC; - if (FAILED(DXGIFactory->CreateSwapChain(Device, &scDesc, &newSC.GetRawRef()))) - return false; + hr = DXGIFactory->CreateSwapChain(Device, &scDesc, &newSC.GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + return false; + } SwapChain = newSC; BackBuffer = NULL; BackBufferRT = NULL; - HRESULT hr = SwapChain->GetBuffer(0, __uuidof(ID3D1xTexture2D), (void**)&BackBuffer.GetRawRef()); + hr = SwapChain->GetBuffer(0, __uuidof(ID3D1xTexture2D), (void**)&BackBuffer.GetRawRef()); if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); return false; + } hr = Device->CreateRenderTargetView(BackBuffer, NULL, &BackBufferRT.GetRawRef()); if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); return false; + } Texture* depthBuffer = GetDepthBuffer(WindowWidth, WindowHeight, Params.Multisample); CurDepthBuffer = depthBuffer; - if (CurRenderTarget == NULL) + if (CurRenderTarget == NULL && depthBuffer != NULL) { Context->OMSetRenderTargets(1, &BackBufferRT.GetRawRef(), depthBuffer->TexDsv); } @@ -1059,20 +1236,12 @@ ovrTexture Texture::Get_ovrTexture() void RenderDevice::SetWindowSize(int w, int h) { - if (w == WindowWidth && h == WindowHeight) - return; - - ::OVR::Render::RenderDevice::SetWindowSize(w, h); - - Context->OMSetRenderTargets(0, NULL, NULL); - BackBuffer = NULL; - BackBufferRT = NULL; - if (SwapChain) - { - SwapChain->ResizeBuffers(2, WindowWidth, WindowHeight, DXGI_FORMAT_R8G8B8A8_UNORM, 0); - SwapChain->GetBuffer(0, __uuidof(ID3D1xTexture2D), (void**)&BackBuffer.GetRawRef()); - } - Device->CreateRenderTargetView(BackBuffer, NULL, &BackBufferRT.GetRawRef()); + // This code is rendered a no-op + // It interferes with proper driver operation in + // application mode and doesn't add any value in + // compatibility mode + OVR_UNUSED(w); + OVR_UNUSED(h); } bool RenderDevice::SetFullscreen(DisplayMode fullscreen) @@ -1121,6 +1290,7 @@ bool RenderDevice::SetFullscreen(DisplayMode fullscreen) HRESULT hr = SwapChain->SetFullscreenState(fullscreen, fullscreen ? FullscreenOutput : NULL); if (FAILED(hr)) { + OVR_LOG_COM_ERROR(hr); return false; } } @@ -1180,7 +1350,11 @@ void RenderDevice::SetDepthMode(bool enable, bool write, CompareFunc func) OVR_ASSERT(0); } dss.DepthWriteMask = write ? D3D1x_(DEPTH_WRITE_MASK_ALL) : D3D1x_(DEPTH_WRITE_MASK_ZERO); - Device->CreateDepthStencilState(&dss, &DepthStates[index].GetRawRef()); + HRESULT hr = Device->CreateDepthStencilState(&dss, &DepthStates[index].GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } Context->OMSetDepthStencilState(DepthStates[index], 0); CurDepthState = DepthStates[index]; } @@ -1258,6 +1432,7 @@ bool Buffer::Data(int use, const void *buffer, size_t size) } else { + OVR_ASSERT (!(use & Buffer_ReadOnly)); Ren->Context->UpdateSubresource(D3DBuffer, 0, NULL, buffer, 0, 0); return true; } @@ -1307,6 +1482,7 @@ bool Buffer::Data(int use, const void *buffer, size_t size) sr.SysMemPitch = 0; sr.SysMemSlicePitch = 0; + D3DBuffer = NULL; HRESULT hr = Ren->Device->CreateBuffer(&desc, buffer ? &sr : NULL, &D3DBuffer.GetRawRef()); if (SUCCEEDED(hr)) { @@ -1314,6 +1490,10 @@ bool Buffer::Data(int use, const void *buffer, size_t size) Size = desc.ByteWidth; return 1; } + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } return 0; } @@ -1360,15 +1540,30 @@ bool Buffer::Unmap(void *m) #if (OVR_D3D_VERSION == 10) template<> bool Shader<Render::Shader_Vertex, ID3D10VertexShader>::Load(void* shader, size_t size) { - return SUCCEEDED(Ren->Device->CreateVertexShader(shader, size, &D3DShader)); + HRESULT hr = Ren->Device->CreateVertexShader(shader, size, &D3DShader); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } + return SUCCEEDED(hr); } template<> bool Shader<Render::Shader_Pixel, ID3D10PixelShader>::Load(void* shader, size_t size) { - return SUCCEEDED(Ren->Device->CreatePixelShader(shader, size, &D3DShader)); + HRESULT hr = Ren->Device->CreatePixelShader(shader, size, &D3DShader); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } + return SUCCEEDED(hr); } template<> bool Shader<Render::Shader_Geometry, ID3D10GeometryShader>::Load(void* shader, size_t size) { - return SUCCEEDED(Ren->Device->CreateGeometryShader(shader, size, &D3DShader)); + HRESULT hr = Ren->Device->CreateGeometryShader(shader, size, &D3DShader); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } + return SUCCEEDED(hr); } template<> void Shader<Render::Shader_Vertex, ID3D10VertexShader>::Set(PrimitiveType) const @@ -1387,15 +1582,30 @@ template<> void Shader<Render::Shader_Geometry, ID3D10GeometryShader>::Set(Primi #else // 11 template<> bool Shader<Render::Shader_Vertex, ID3D11VertexShader>::Load(void* shader, size_t size) { - return SUCCEEDED(Ren->Device->CreateVertexShader(shader, size, NULL, &D3DShader)); + HRESULT hr = Ren->Device->CreateVertexShader(shader, size, NULL, &D3DShader); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } + return SUCCEEDED(hr); } template<> bool Shader<Render::Shader_Pixel, ID3D11PixelShader>::Load(void* shader, size_t size) { - return SUCCEEDED(Ren->Device->CreatePixelShader(shader, size, NULL, &D3DShader)); + HRESULT hr = Ren->Device->CreatePixelShader(shader, size, NULL, &D3DShader); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } + return SUCCEEDED(hr); } template<> bool Shader<Render::Shader_Geometry, ID3D11GeometryShader>::Load(void* shader, size_t size) { - return SUCCEEDED(Ren->Device->CreateGeometryShader(shader, size, NULL, &D3DShader)); + HRESULT hr = Ren->Device->CreateGeometryShader(shader, size, NULL, &D3DShader); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } + return SUCCEEDED(hr); } template<> void Shader<Render::Shader_Vertex, ID3D11VertexShader>::Set(PrimitiveType) const @@ -1436,6 +1646,7 @@ ID3D10Blob* RenderDevice::CompileShader(const char* profile, const char* src, co OVR_DEBUG_LOG(("Compiling D3D shader for %s failed\n%s\n\n%s", profile, src, errors->GetBufferPointer())); OutputDebugStringA((char*)errors->GetBufferPointer()); + OVR_LOG_COM_ERROR(hr); return NULL; } if (errors) @@ -1472,11 +1683,17 @@ bool ShaderBase::SetUniform(const char* name, int n, const float* v) void ShaderBase::InitUniforms(ID3D10Blob* s) { ID3D10ShaderReflection* ref = NULL; - D3D10ReflectShader(s->GetBufferPointer(), s->GetBufferSize(), &ref); + HRESULT hr = D3D10ReflectShader(s->GetBufferPointer(), s->GetBufferSize(), &ref); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } ID3D10ShaderReflectionConstantBuffer* buf = ref->GetConstantBufferByIndex(0); D3D10_SHADER_BUFFER_DESC bufd; - if (FAILED(buf->GetDesc(&bufd))) + hr = buf->GetDesc(&bufd); + if (FAILED(hr)) { + //OVR_LOG_COM_ERROR(hr); - Seems to happen normally UniformsSize = 0; if (UniformData) { @@ -1492,7 +1709,8 @@ void ShaderBase::InitUniforms(ID3D10Blob* s) if (var) { D3D10_SHADER_VARIABLE_DESC vd; - if (SUCCEEDED(var->GetDesc(&vd))) + hr = var->GetDesc(&vd); + if (SUCCEEDED(hr)) { Uniform u; u.Name = vd.Name; @@ -1500,6 +1718,10 @@ void ShaderBase::InitUniforms(ID3D10Blob* s) u.Size = vd.Size; UniformInfo.PushBack(u); } + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } } } @@ -1511,7 +1733,10 @@ void ShaderBase::UpdateBuffer(Buffer* buf) { if (UniformsSize) { - buf->Data(Buffer_Uniform, UniformData, UniformsSize); + if (!buf->Data(Buffer_Uniform, UniformData, UniformsSize)) + { + OVR_ASSERT(false); + } } } @@ -1650,7 +1875,11 @@ ID3D1xSamplerState* RenderDevice::GetSamplerState(int sm) ss.Filter = D3D1x_(FILTER_MIN_MAG_MIP_LINEAR); } ss.MaxLOD = 15; - Device->CreateSamplerState(&ss, &SamplerStates[sm].GetRawRef()); + HRESULT hr = Device->CreateSamplerState(&ss, &SamplerStates[sm].GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } return SamplerStates[sm]; } @@ -1797,14 +2026,28 @@ void RenderDevice::GenerateSubresourceData( Texture* RenderDevice::CreateTexture(int format, int width, int height, const void* data, int mipcount) { - UPInt gpuMemorySize = 0; + OVR_ASSERT(Device != NULL); + + size_t gpuMemorySize = 0; { IDXGIDevice* pDXGIDevice; - Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice); + HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } IDXGIAdapter * pDXGIAdapter; - pDXGIDevice->GetAdapter(&pDXGIAdapter); + hr = pDXGIDevice->GetAdapter(&pDXGIAdapter); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } DXGI_ADAPTER_DESC adapterDesc; - pDXGIAdapter->GetDesc(&adapterDesc); + hr = pDXGIAdapter->GetDesc(&adapterDesc); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } gpuMemorySize = adapterDesc.DedicatedVideoMemory; pDXGIAdapter->Release(); pDXGIDevice->Release(); @@ -1858,14 +2101,10 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo { return NULL; } - int samples = (Texture_RGBA & Texture_SamplesMask); - if (samples < 1) - { - samples = 1; - } Texture* NewTex = new Texture(this, format, largestMipWidth, largestMipHeight); - NewTex->Samples = samples; + // BCn/DXTn - no AA. + NewTex->Samples = 1; D3D1x_(TEXTURE2D_DESC) desc; desc.Width = largestMipWidth; @@ -1873,16 +2112,21 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo desc.MipLevels = effectiveMipCount; desc.ArraySize = 1; desc.Format = static_cast<DXGI_FORMAT>(convertedFormat); - desc.SampleDesc.Count = samples; + desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Usage = D3D1x_(USAGE_DEFAULT); desc.BindFlags = D3D1x_(BIND_SHADER_RESOURCE); desc.CPUAccessFlags = 0; desc.MiscFlags = 0; + NewTex->Tex = NULL; HRESULT hr = Device->CreateTexture2D(&desc, static_cast<D3D1x_(SUBRESOURCE_DATA)*>(subresData), &NewTex->Tex.GetRawRef()); OVR_FREE(subresData); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } if (SUCCEEDED(hr) && NewTex != 0) { @@ -1892,10 +2136,12 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo SRVDesc.ViewDimension = D3D_SRV_DIMENSION_TEXTURE2D; SRVDesc.Texture2D.MipLevels = desc.MipLevels; + NewTex->TexSv = NULL; hr = Device->CreateShaderResourceView(NewTex->Tex, NULL, &NewTex->TexSv.GetRawRef()); if (FAILED(hr)) { + OVR_LOG_COM_ERROR(hr); NewTex->Release(); return NULL; } @@ -1920,11 +2166,11 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo { case Texture_BGRA: bpp = 4; - d3dformat = DXGI_FORMAT_B8G8R8A8_UNORM; + d3dformat = (format & Texture_SRGB) ? DXGI_FORMAT_B8G8R8A8_UNORM_SRGB : DXGI_FORMAT_B8G8R8A8_UNORM; break; case Texture_RGBA: bpp = 4; - d3dformat = DXGI_FORMAT_R8G8B8A8_UNORM; + d3dformat = (format & Texture_SRGB) ? DXGI_FORMAT_R8G8B8A8_UNORM_SRGB : DXGI_FORMAT_R8G8B8A8_UNORM; break; case Texture_R: bpp = 1; @@ -1970,9 +2216,11 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo } } + NewTex->Tex = NULL; HRESULT hr = Device->CreateTexture2D(&dsDesc, NULL, &NewTex->Tex.GetRawRef()); if (FAILED(hr)) { + OVR_LOG_COM_ERROR(hr); OVR_DEBUG_LOG_TEXT(("Failed to create 2D D3D texture.")); NewTex->Release(); return NULL; @@ -1986,11 +2234,21 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo depthSrv.ViewDimension = samples > 1 ? D3D1x_(SRV_DIMENSION_TEXTURE2DMS) : D3D1x_(SRV_DIMENSION_TEXTURE2D); depthSrv.Texture2D.MostDetailedMip = 0; depthSrv.Texture2D.MipLevels = dsDesc.MipLevels; - Device->CreateShaderResourceView(NewTex->Tex, &depthSrv, &NewTex->TexSv.GetRawRef()); + NewTex->TexSv = NULL; + hr = Device->CreateShaderResourceView(NewTex->Tex, &depthSrv, &NewTex->TexSv.GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } } else { - Device->CreateShaderResourceView(NewTex->Tex, NULL, &NewTex->TexSv.GetRawRef()); + NewTex->TexSv = NULL; + hr = Device->CreateShaderResourceView(NewTex->Tex, NULL, &NewTex->TexSv.GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } } } @@ -2001,7 +2259,7 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo { int srcw = width, srch = height; int level = 0; - UByte* mipmaps = NULL; + uint8_t* mipmaps = NULL; do { level++; @@ -2017,9 +2275,9 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo } if (mipmaps == NULL) { - mipmaps = (UByte*)OVR_ALLOC(mipw * miph * 4); + mipmaps = (uint8_t*)OVR_ALLOC(mipw * miph * 4); } - FilterRgba2x2(level == 1 ? (const UByte*)data : mipmaps, srcw, srch, mipmaps); + FilterRgba2x2(level == 1 ? (const uint8_t*)data : mipmaps, srcw, srch, mipmaps); Context->UpdateSubresource(NewTex->Tex, level, NULL, mipmaps, mipw * bpp, miph * bpp); srcw = mipw; srch = miph; @@ -2042,11 +2300,21 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo depthDsv.Format = DXGI_FORMAT_D32_FLOAT; depthDsv.ViewDimension = samples > 1 ? D3D1x_(DSV_DIMENSION_TEXTURE2DMS) : D3D1x_(DSV_DIMENSION_TEXTURE2D); depthDsv.Texture2D.MipSlice = 0; - Device->CreateDepthStencilView(NewTex->Tex, createDepthSrv ? &depthDsv : NULL, &NewTex->TexDsv.GetRawRef()); + NewTex->TexDsv = NULL; + hr = Device->CreateDepthStencilView(NewTex->Tex, createDepthSrv ? &depthDsv : NULL, &NewTex->TexDsv.GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } } else { - Device->CreateRenderTargetView(NewTex->Tex, NULL, &NewTex->TexRtv.GetRawRef()); + NewTex->TexRtv = NULL; + hr = Device->CreateRenderTargetView(NewTex->Tex, NULL, &NewTex->TexRtv.GetRawRef()); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } } } @@ -2109,13 +2377,19 @@ void RenderDevice::Render(const Matrix4f& matrix, Model* model) if (!model->VertexBuffer) { Ptr<Buffer> vb = *CreateBuffer(); - vb->Data(Buffer_Vertex | Buffer_ReadOnly, &model->Vertices[0], model->Vertices.GetSize() * sizeof(Vertex)); + if (!vb->Data(Buffer_Vertex | Buffer_ReadOnly, &model->Vertices[0], model->Vertices.GetSize() * sizeof(Vertex))) + { + OVR_ASSERT(false); + } model->VertexBuffer = vb; } if (!model->IndexBuffer) { Ptr<Buffer> ib = *CreateBuffer(); - ib->Data(Buffer_Index | Buffer_ReadOnly, &model->Indices[0], model->Indices.GetSize() * 2); + if (!ib->Data(Buffer_Index | Buffer_ReadOnly, &model->Indices[0], model->Indices.GetSize() * 2)) + { + OVR_ASSERT(false); + } model->IndexBuffer = ib; } @@ -2176,7 +2450,10 @@ void RenderDevice::Render(const Fill* fill, Render::Buffer* vertices, Render::Bu stdUniforms->Proj = StdUniforms.Proj; } - UniformBuffers[Shader_Vertex]->Data(Buffer_Uniform, vertexData, vshader->UniformsSize); + if (!UniformBuffers[Shader_Vertex]->Data(Buffer_Uniform, vertexData, vshader->UniformsSize)) + { + OVR_ASSERT(false); + } vshader->SetUniformBuffer(UniformBuffers[Shader_Vertex]); } @@ -2223,15 +2500,27 @@ void RenderDevice::Render(const Fill* fill, Render::Buffer* vertices, Render::Bu } } -UPInt RenderDevice::QueryGPUMemorySize() +size_t RenderDevice::QueryGPUMemorySize() { IDXGIDevice* pDXGIDevice; - Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice); + HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void **)&pDXGIDevice); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } IDXGIAdapter * pDXGIAdapter; - pDXGIDevice->GetAdapter(&pDXGIAdapter); - DXGI_ADAPTER_DESC adapterDesc; - pDXGIAdapter->GetDesc(&adapterDesc); - return adapterDesc.DedicatedVideoMemory; + hr = pDXGIDevice->GetAdapter(&pDXGIAdapter); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } + DXGI_ADAPTER_DESC adapterDesc; + hr = pDXGIAdapter->GetDesc(&adapterDesc); + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); + } + return adapterDesc.DedicatedVideoMemory; } @@ -2245,20 +2534,25 @@ void RenderDevice::Present ( bool withVsync ) } } + HRESULT hr; if ( withVsync ) { - SwapChain->Present(1, 0); + hr = SwapChain->Present(1, 0); } else { // Immediate present - SwapChain->Present(0, 0); + hr = SwapChain->Present(0, 0); + } + if (FAILED(hr)) + { + OVR_LOG_COM_ERROR(hr); } } void RenderDevice::WaitUntilGpuIdle() { -#if 0 +#if 1 // If enabling this option and using an NVIDIA GPU, // then make sure your "max pre-rendered frames" is set to 1 under the NVIDIA GPU settings. @@ -2289,35 +2583,35 @@ void RenderDevice::WaitUntilGpuIdle() #endif } -void RenderDevice::FillRect(float left, float top, float right, float bottom, Color c) +void RenderDevice::FillRect(float left, float top, float right, float bottom, Color c, const Matrix4f* view) { Context->OMSetBlendState(BlendState, NULL, 0xffffffff); - OVR::Render::RenderDevice::FillRect(left, top, right, bottom, c); + OVR::Render::RenderDevice::FillRect(left, top, right, bottom, c, view); Context->OMSetBlendState(NULL, NULL, 0xffffffff); } -void RenderDevice::FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm) +void RenderDevice::FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm, const Matrix4f* view) { Context->OMSetBlendState(BlendState, NULL, 0xffffffff); - OVR::Render::RenderDevice::FillGradientRect(left, top, right, bottom, col_top, col_btm); + OVR::Render::RenderDevice::FillGradientRect(left, top, right, bottom, col_top, col_btm, view); Context->OMSetBlendState(NULL, NULL, 0xffffffff); } -void RenderDevice::RenderText(const struct Font* font, const char* str, float x, float y, float size, Color c) +void RenderDevice::RenderText(const struct Font* font, const char* str, float x, float y, float size, Color c, const Matrix4f* view) { Context->OMSetBlendState(BlendState, NULL, 0xffffffff); - OVR::Render::RenderDevice::RenderText(font, str, x, y, size, c); + OVR::Render::RenderDevice::RenderText(font, str, x, y, size, c, view); Context->OMSetBlendState(NULL, NULL, 0xffffffff); } -void RenderDevice::RenderImage(float left, float top, float right, float bottom, ShaderFill* image) +void RenderDevice::RenderImage(float left, float top, float right, float bottom, ShaderFill* image, unsigned char alpha, const Matrix4f* view) { Context->OMSetBlendState(BlendState, NULL, 0xffffffff); - OVR::Render::RenderDevice::RenderImage(left, top, right, bottom, image); + OVR::Render::RenderDevice::RenderImage(left, top, right, bottom, image, alpha, view); Context->OMSetBlendState(NULL, NULL, 0xffffffff); } -void RenderDevice::BeginGpuEvent(const char* markerText, UInt32 markerColor) +void RenderDevice::BeginGpuEvent(const char* markerText, uint32_t markerColor) { #if GPU_PROFILING WCHAR wStr[255]; diff --git a/Samples/CommonSrc/Render/Render_D3D1X_Device.h b/Samples/CommonSrc/Render/Render_D3D1X_Device.h index bd733cc..4207d8a 100644 --- a/Samples/CommonSrc/Render/Render_D3D1X_Device.h +++ b/Samples/CommonSrc/Render/Render_D3D1X_Device.h @@ -38,6 +38,9 @@ limitations under the License. #include "../Render/Render_Device.h" +#include <WinSock2.h> +#include <WS2tcpip.h> +#define WIN32_LEAN_AND_MEAN #include <Windows.h> #if (OVR_D3D_VERSION == 10) @@ -312,7 +315,7 @@ public: virtual void WaitUntilGpuIdle(); virtual bool SetFullscreen(DisplayMode fullscreen); - virtual UPInt QueryGPUMemorySize(); + virtual size_t QueryGPUMemorySize(); virtual void Clear(float r = 0, float g = 0, float b = 0, float a = 1, float depth = 1, @@ -345,10 +348,10 @@ public: } // Overrident to apply proper blend state. - virtual void FillRect(float left, float top, float right, float bottom, Color c); - virtual void FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm); - virtual void RenderText(const struct Font* font, const char* str, float x, float y, float size, Color c); - virtual void RenderImage(float left, float top, float right, float bottom, ShaderFill* image); + virtual void FillRect(float left, float top, float right, float bottom, Color c, const Matrix4f* view=NULL); + virtual void FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm, const Matrix4f* view); + virtual void RenderText(const struct Font* font, const char* str, float x, float y, float size, Color c, const Matrix4f* view=NULL); + virtual void RenderImage(float left, float top, float right, float bottom, ShaderFill* image, unsigned char alpha=255, const Matrix4f* view=NULL); virtual void Render(const Matrix4f& matrix, Model* model); virtual void Render(const Fill* fill, Render::Buffer* vertices, Render::Buffer* indices, @@ -369,7 +372,7 @@ public: void SetTexture(Render::ShaderStage stage, int slot, const Texture* t); // GPU Profiling - virtual void BeginGpuEvent(const char* markerText, UInt32 markerColor); + virtual void BeginGpuEvent(const char* markerText, uint32_t markerColor); virtual void EndGpuEvent(); }; diff --git a/Samples/CommonSrc/Render/Render_Device.cpp b/Samples/CommonSrc/Render/Render_Device.cpp index 88e611a..94ff680 100644 --- a/Samples/CommonSrc/Render/Render_Device.cpp +++ b/Samples/CommonSrc/Render/Render_Device.cpp @@ -77,7 +77,7 @@ namespace OVR { namespace Render { - UInt16 CubeIndices[] = + uint16_t CubeIndices[] = { 0, 1, 3, 3, 1, 2, @@ -127,7 +127,7 @@ namespace OVR { namespace Render { Model* box = new Model(); - UInt16 startIndex = 0; + uint16_t startIndex = 0; // Cube startIndex = box->AddVertex(Vector3f(x1, y2, z1), ycolor); @@ -238,7 +238,7 @@ namespace OVR { namespace Render { }; - UInt16 startIndex = GetNextVertexIndex(); + uint16_t startIndex = GetNextVertexIndex(); enum { @@ -265,7 +265,7 @@ namespace OVR { namespace Render { { Vector3f s = size * 0.5f; Vector3f o = origin; - UInt16 i = GetNextVertexIndex(); + uint16_t i = GetNextVertexIndex(); AddVertex(-s.x + o.x, s.y + o.y, -s.z + o.z, c, 0, 1, 0, 0, -1); AddVertex(s.x + o.x, s.y + o.y, -s.z + o.z, c, 1, 1, 0, 0, -1); @@ -324,15 +324,15 @@ namespace OVR { namespace Render { { Model *cyl = new Model(); float halfht = height * 0.5f; - for(UInt16 i = 0; i < sides; i++) + for(uint16_t i = 0; i < sides; i++) { - float x = cosf(Math<float>::TwoPi * i / float(sides)); - float y = sinf(Math<float>::TwoPi * i / float(sides)); + float x = cosf(MATH_FLOAT_TWOPI * i / float(sides)); + float y = sinf(MATH_FLOAT_TWOPI * i / float(sides)); cyl->AddVertex(radius * x, radius * y, halfht, color, x + 1, y, 0, 0, 1); cyl->AddVertex(radius * x, radius * y, -1.0f*halfht, color, x, y, 0, 0, -1); - UInt16 j = 0; + uint16_t j = 0; if(i < sides - 1) { j = i + 1; @@ -340,8 +340,8 @@ namespace OVR { namespace Render { cyl->AddTriangle(1, i * 4 + 1, i * 4 + 5); } - float nx = cosf(Math<float>::Pi * (0.5f + 2.0f * i / float(sides))); - float ny = sinf(Math<float>::Pi * (0.5f + 2.0f * i / float(sides))); + float nx = cosf(MATH_FLOAT_PI * (0.5f + 2.0f * i / float(sides))); + float ny = sinf(MATH_FLOAT_PI * (0.5f + 2.0f * i / float(sides))); cyl->AddVertex(radius * x, radius * y, halfht, color, x + 1, y, nx, ny, 0); cyl->AddVertex(radius * x, radius * y, -1.0f*halfht, color, x, y, nx, ny, 0); @@ -359,21 +359,21 @@ namespace OVR { namespace Render { float halfht = height * 0.5f; cone->AddVertex(0.0f, 0.0f, -1.0f*halfht, color, 0, 0, 0, 0, -1); - for(UInt16 i = 0; i < sides; i++) + for(uint16_t i = 0; i < sides; i++) { - float x = cosf(Math<float>::TwoPi * i / float(sides)); - float y = sinf(Math<float>::TwoPi * i / float(sides)); + float x = cosf(MATH_FLOAT_TWOPI * i / float(sides)); + float y = sinf(MATH_FLOAT_TWOPI * i / float(sides)); cone->AddVertex(radius * x, radius * y, -1.0f*halfht, color, 0, 0, 0, 0, -1); - UInt16 j = 1; + uint16_t j = 1; if(i < sides - 1) { j = i + 1; } - float next_x = cosf(Math<float>::TwoPi * j / float(sides)); - float next_y = sinf(Math<float>::TwoPi * j / float(sides)); + float next_x = cosf(MATH_FLOAT_TWOPI * j / float(sides)); + float next_y = sinf(MATH_FLOAT_TWOPI * j / float(sides)); Vector3f normal = Vector3f(x, y, -halfht).Cross(Vector3f(next_x, next_y, -halfht)); @@ -391,13 +391,13 @@ namespace OVR { namespace Render { Model* Model::CreateSphere(Color color, Vector3f origin, float radius, int sides) { Model *sphere = new Model(); - UInt16 usides = (UInt16) sides; - UInt16 halfsides = usides/2; + uint16_t usides = (uint16_t) sides; + uint16_t halfsides = usides/2; - for(UInt16 k = 0; k < halfsides; k++) { + for(uint16_t k = 0; k < halfsides; k++) { - float z = cosf(Math<float>::Pi * k / float(halfsides)); - float z_r = sinf(Math<float>::Pi * k / float(halfsides)); // the radius of the cross circle with coordinate z + float z = cosf(MATH_FLOAT_PI * k / float(halfsides)); + float z_r = sinf(MATH_FLOAT_PI * k / float(halfsides)); // the radius of the cross circle with coordinate z if (k == 0) { // add north and south poles @@ -406,12 +406,12 @@ namespace OVR { namespace Render { } else { - for(UInt16 i = 0; i < sides; i++) + for(uint16_t i = 0; i < sides; i++) { - float x = cosf(Math<float>::TwoPi * i / float(sides)) * z_r; - float y = sinf(Math<float>::TwoPi * i / float(sides)) * z_r; + float x = cosf(MATH_FLOAT_TWOPI * i / float(sides)) * z_r; + float y = sinf(MATH_FLOAT_TWOPI * i / float(sides)) * z_r; - UInt16 j = 0; + uint16_t j = 0; if(i < sides - 1) { j = i + 1; @@ -419,8 +419,8 @@ namespace OVR { namespace Render { sphere->AddVertex(radius * x, radius * y, radius * z, color, 0, 1, x, y, z); - UInt16 indi = 2 + (k -1)*usides + i; - UInt16 indj = 2 + (k -1)*usides + j; + uint16_t indi = 2 + (k -1)*usides + i; + uint16_t indj = 2 + (k -1)*usides + j; if (k == 1) // NorthPole sphere->AddTriangle(0, j + 2, i + 2); else if (k == halfsides - 1) //SouthPole @@ -548,14 +548,14 @@ namespace OVR { namespace Render { } float RenderDevice::MeasureText(const Font* font, const char* str, float size, float strsize[2], - const UPInt charRange[2], Vector2f charRangeRect[2]) + const size_t charRange[2], Vector2f charRangeRect[2]) { - UPInt length = strlen(str); + size_t length = strlen(str); float w = 0; float xp = 0; float yp = 0; - for (UPInt i = 0; i < length; i++) + for (size_t i = 0; i < length; i++) { if (str[i] == '\n') { @@ -621,8 +621,13 @@ namespace OVR { namespace Render { void RenderDevice::RenderText(const Font* font, const char* str, - float x, float y, float size, Color c) + float x, float y, float size, Color c, const Matrix4f* view) { + size_t length = strlen(str); + + // Do not attempt to render if we have an empty string. + if (length == 0) { return; } + if(!pTextVertexBuffer) { pTextVertexBuffer = *CreateBuffer(); @@ -638,8 +643,6 @@ namespace OVR { namespace Render { *CreateTexture(Texture_R, font->twidth, font->theight, font->tex)), true); } - UPInt length = strlen(str); - pTextVertexBuffer->Data(Buffer_Vertex, NULL, length * 6 * sizeof(Vertex)); Vertex* vertices = (Vertex*)pTextVertexBuffer->Map(0, length * 6 * sizeof(Vertex), Map_Discard); if(!vertices) @@ -652,10 +655,13 @@ namespace OVR { namespace Render { 0, 0, 0, 0, x, y, 0, 1).Transposed(); + if (view) + m = (*view) * m; + float xp = 0, yp = (float)font->ascent; int ivertex = 0; - for (UPInt i = 0; i < length; i++) + for (size_t i = 0; i < length; i++) { if(str[i] == '\n') { @@ -696,10 +702,10 @@ namespace OVR { namespace Render { pTextVertexBuffer->Unmap(vertices); - Render(font->fill, pTextVertexBuffer, NULL, m, 0, ivertex, Prim_Triangles); + Render(font->fill, pTextVertexBuffer, NULL, m, 0, ivertex, Prim_Triangles); } - void RenderDevice::FillRect(float left, float top, float right, float bottom, Color c) + void RenderDevice::FillRect(float left, float top, float right, float bottom, Color c, const Matrix4f* matrix) { if(!pTextVertexBuffer) { @@ -729,12 +735,15 @@ namespace OVR { namespace Render { pTextVertexBuffer->Unmap(vertices); - Render(fill, pTextVertexBuffer, NULL, Matrix4f(), 0, 6, Prim_Triangles); + if (matrix == NULL) + Render(fill, pTextVertexBuffer, NULL, Matrix4f(), 0, 6, Prim_Triangles); + else + Render(fill, pTextVertexBuffer, NULL, *matrix, 0, 6, Prim_Triangles); } - void RenderDevice::FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm) + void RenderDevice::FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm, const Matrix4f* matrix) { if(!pTextVertexBuffer) { @@ -764,7 +773,10 @@ namespace OVR { namespace Render { pTextVertexBuffer->Unmap(vertices); - Render(fill, pTextVertexBuffer, NULL, Matrix4f(), 0, 6, Prim_Triangles); + if (matrix) + Render(fill, pTextVertexBuffer, NULL, *matrix, 0, 6, Prim_Triangles); + else + Render(fill, pTextVertexBuffer, NULL, Matrix4f(), 0, 6, Prim_Triangles); } @@ -860,7 +872,8 @@ namespace OVR { namespace Render { float right, float bottom, ShaderFill* image, - unsigned char alpha) + unsigned char alpha, + const Matrix4f* view) { Color c = Color(255, 255, 255, alpha); Ptr<Model> m = *new Model(Prim_Triangles); @@ -872,7 +885,10 @@ namespace OVR { namespace Render { m->AddTriangle(0,3,2); m->Fill = image; - Render(Matrix4f(), m); + if (view) + Render(*view, m); + else + Render(Matrix4f(), m); } bool RenderDevice::initPostProcessSupport(PostProcessType pptype) @@ -970,7 +986,6 @@ namespace OVR { namespace Render { { BeginRendering(); initPostProcessSupport(pptype); - SetViewport(VP); SetWorldUniforms(Proj); SetExtraShaders(NULL); } @@ -978,7 +993,7 @@ namespace OVR { namespace Render { void RenderDevice::FinishScene() { SetExtraShaders(0); - SetDefaultRenderTarget(); + //SetDefaultRenderTarget(); } @@ -1002,7 +1017,7 @@ namespace OVR { namespace Render { int numVerts = 0; int numTris = 0; DistortionMeshVertexData *pRawVerts = NULL; - UInt16 *pIndices = NULL; + uint16_t *pIndices = NULL; DistortionMeshCreate ( &pRawVerts, &pIndices, &numVerts, &numTris, stereoParams, hmdRenderInfo ); int numIndices = numTris * 3; @@ -1018,10 +1033,10 @@ namespace OVR { namespace Render { pCurVert->TexG = pCurRawVert->TanEyeAnglesG; pCurVert->TexB = pCurRawVert->TanEyeAnglesB; // Convert [0.0f,1.0f] to [0,255] - pCurVert->Col.R = (OVR::UByte)( floorf ( pCurRawVert->Shade * 255.999f ) ); + pCurVert->Col.R = (uint8_t)( floorf ( pCurRawVert->Shade * 255.999f ) ); pCurVert->Col.G = pCurVert->Col.R; pCurVert->Col.B = pCurVert->Col.R; - pCurVert->Col.A = (OVR::UByte)( floorf ( pCurRawVert->TimewarpLerp * 255.999f ) ); + pCurVert->Col.A = (uint8_t)( floorf ( pCurRawVert->TimewarpLerp * 255.999f ) ); pCurRawVert++; pCurVert++; } @@ -1030,7 +1045,7 @@ namespace OVR { namespace Render { pDistortionMeshVertexBuffer[eyeNum] = *CreateBuffer(); pDistortionMeshVertexBuffer[eyeNum]->Data ( Buffer_Vertex | Buffer_ReadOnly, pVerts, sizeof(DistortionVertex) * numVerts ); pDistortionMeshIndexBuffer[eyeNum] = *CreateBuffer(); - pDistortionMeshIndexBuffer[eyeNum]->Data ( Buffer_Index | Buffer_ReadOnly, pIndices, ( sizeof(UInt16) * numIndices ) ); + pDistortionMeshIndexBuffer[eyeNum]->Data ( Buffer_Index | Buffer_ReadOnly, pIndices, ( sizeof(uint16_t) * numIndices ) ); DistortionMeshDestroy ( pRawVerts, pIndices ); OVR_FREE ( pVerts ); @@ -1047,7 +1062,7 @@ namespace OVR { namespace Render { int numVerts = 0; int numTris = 0; HeightmapMeshVertexData *pRawVerts = NULL; - UInt16 *pIndices = NULL; + uint16_t *pIndices = NULL; HeightmapMeshCreate ( &pRawVerts, &pIndices, &numVerts, &numTris, stereoParams, hmdRenderInfo ); int numIndices = numTris * 3; @@ -1062,7 +1077,7 @@ namespace OVR { namespace Render { Vector2f texCoord = pCurRawVert->TanEyeAngles; pCurVert->Tex.x = texCoord.x; pCurVert->Tex.y = texCoord.y; - pCurVert->Tex.z = (OVR::UByte)( floorf ( pCurRawVert->TimewarpLerp * 255.999f ) ); + pCurVert->Tex.z = (uint8_t)( floorf ( pCurRawVert->TimewarpLerp * 255.999f ) ); pCurRawVert++; pCurVert++; } @@ -1071,7 +1086,7 @@ namespace OVR { namespace Render { pHeightmapMeshVertexBuffer[eyeNum] = *CreateBuffer(); pHeightmapMeshVertexBuffer[eyeNum]->Data ( Buffer_Vertex, pVerts, sizeof(HeightmapVertex) * numVerts ); pHeightmapMeshIndexBuffer[eyeNum] = *CreateBuffer(); - pHeightmapMeshIndexBuffer[eyeNum]->Data ( Buffer_Index, pIndices, ( sizeof(UInt16) * numIndices ) ); + pHeightmapMeshIndexBuffer[eyeNum]->Data ( Buffer_Index, pIndices, ( sizeof(uint16_t) * numIndices ) ); HeightmapMeshDestroy ( pRawVerts, pIndices ); OVR_FREE ( pVerts ); @@ -1091,8 +1106,18 @@ namespace OVR { namespace Render { RenderTarget* pHmdSpaceLayerRenderTargetLeftOrBothEyes, RenderTarget* pHmdSpaceLayerRenderTargetRight, RenderTarget* pOverlayLayerRenderTargetLeftOrBothEyes, - RenderTarget* pOverlayLayerRenderTargetRight) + RenderTarget* pOverlayLayerRenderTargetRight, + RenderTarget* pOutputTarget) { + if(pOutputTarget != NULL) + { + SetRenderTarget(pOutputTarget->pColorTex, pOutputTarget->pDepthTex); + } + else + { + SetDefaultRenderTarget(); + } + SetExtraShaders(0); bool usingOverlay = pOverlayLayerRenderTargetLeftOrBothEyes != NULL; @@ -1310,7 +1335,15 @@ namespace OVR { namespace Render { // Pass 2 - do distortion { + if(pOutputTarget != NULL) + { + SetRenderTarget(pOutputTarget->pColorTex, pOutputTarget->pDepthTex); + } + else + { SetDefaultRenderTarget(); + } + SetDepthMode(false, false); Recti vp( 0, 0, WindowWidth, WindowHeight ); @@ -1540,7 +1573,7 @@ namespace OVR { namespace Render { len = 0; } float tp = Planes[crossing].TestSide(origin + norm * len); - OVR_ASSERT(fabsf(tp) < 0.05f + Mathf::Tolerance); + OVR_ASSERT(fabsf(tp) < 0.05f + MATH_FLOAT_TOLERANCE); OVR_UNUSED(tp); if(ph) @@ -1562,12 +1595,12 @@ namespace OVR { namespace Render { return n; } - void FilterRgba2x2(const UByte* src, int w, int h, UByte* dest) + void FilterRgba2x2(const uint8_t* src, int w, int h, uint8_t* dest) { for(int j = 0; j < (h & ~1); j += 2) { - const UByte* psrc = src + (w * j * 4); - UByte* pdest = dest + ((w >> 1) * (j >> 1) * 4); + const uint8_t* psrc = src + (w * j * 4); + uint8_t* pdest = dest + ((w >> 1) * (j >> 1) * 4); for(int i = 0; i < w >> 1; i++, psrc += 8, pdest += 4) { diff --git a/Samples/CommonSrc/Render/Render_Device.h b/Samples/CommonSrc/Render/Render_Device.h index eea352e..d7e4917 100644 --- a/Samples/CommonSrc/Render/Render_Device.h +++ b/Samples/CommonSrc/Render/Render_Device.h @@ -28,6 +28,7 @@ limitations under the License. #include "Kernel/OVR_RefCount.h" #include "Kernel/OVR_String.h" #include "Kernel/OVR_File.h" +#include "Kernel/OVR_Color.h" #include "OVR_CAPI.h" #include "OVR_Stereo.h" @@ -37,6 +38,7 @@ namespace OVR { namespace Render { class RenderDevice; struct Font; + //----------------------------------------------------------------------------------- enum ShaderStage @@ -90,6 +92,7 @@ enum BuiltinShaders FShader_Gouraud , FShader_Texture , FShader_AlphaTexture , + FShader_AlphaBlendedTexture , FShader_PostProcessWithChromAb , FShader_LitGouraud , FShader_LitTexture , @@ -136,6 +139,7 @@ enum TextureFormat Texture_RenderTarget = 0x10000, Texture_SampleDepth = 0x20000, Texture_GenMipmaps = 0x40000, + Texture_SRGB = 0x80000, }; enum SampleMode @@ -502,7 +506,7 @@ class Model : public Node { public: Array<Vertex> Vertices; - Array<UInt16> Indices; + Array<uint16_t> Indices; PrimitiveType Type; Ptr<class Fill> Fill; bool Visible; @@ -532,42 +536,42 @@ public: } // Returns the index next added vertex will have. - UInt16 GetNextVertexIndex() const + uint16_t GetNextVertexIndex() const { - return (UInt16)Vertices.GetSize(); + return (uint16_t)Vertices.GetSize(); } - UInt16 AddVertex(const Vertex& v) + uint16_t AddVertex(const Vertex& v) { OVR_ASSERT(!VertexBuffer && !IndexBuffer); - UPInt size = Vertices.GetSize(); + size_t size = Vertices.GetSize(); OVR_ASSERT(size <= USHRT_MAX); // We only use a short to store vert indices. - UInt16 index = (UInt16) size; + uint16_t index = (uint16_t) size; Vertices.PushBack(v); return index; } - UInt16 AddVertex(const Vector3f& v, const Color& c, float u_ = 0, float v_ = 0) + uint16_t AddVertex(const Vector3f& v, const Color& c, float u_ = 0, float v_ = 0) { return AddVertex(Vertex(v,c,u_,v_)); } - UInt16 AddVertex(float x, float y, float z, const Color& c, float u, float v) + uint16_t AddVertex(float x, float y, float z, const Color& c, float u, float v) { return AddVertex(Vertex(Vector3f(x,y,z),c, u,v)); } - void AddLine(UInt16 a, UInt16 b) + void AddLine(uint16_t a, uint16_t b) { Indices.PushBack(a); Indices.PushBack(b); } - UInt16 AddVertex(float x, float y, float z, const Color& c, + uint16_t AddVertex(float x, float y, float z, const Color& c, float u, float v, float nx, float ny, float nz) { return AddVertex(Vertex(Vector3f(x,y,z),c, u,v, Vector3f(nx,ny,nz))); } - UInt16 AddVertex(float x, float y, float z, const Color& c, + uint16_t AddVertex(float x, float y, float z, const Color& c, float u1, float v1, float u2, float v2, float nx, float ny, float nz) { return AddVertex(Vertex(Vector3f(x,y,z), c, u1, v1, u2, v2, Vector3f(nx,ny,nz))); @@ -578,7 +582,7 @@ public: AddLine(AddVertex(a), AddVertex(b)); } - void AddTriangle(UInt16 a, UInt16 b, UInt16 c) + void AddTriangle(uint16_t a, uint16_t b, uint16_t c) { Indices.PushBack(a); Indices.PushBack(b); @@ -625,7 +629,7 @@ public: void ClearRenderer() { - for (UPInt i=0; i< Nodes.GetSize(); i++) + for (size_t i=0; i< Nodes.GetSize(); i++) Nodes[i]->ClearRenderer(); } @@ -723,9 +727,9 @@ struct DisplayId // MacOS int CgDisplayId; // CGDirectDisplayID - DisplayId() : CgDisplayId(0) {} + DisplayId() : CgDisplayId(-2) {} DisplayId(int id) : CgDisplayId(id) {} - DisplayId(String m, int id=0) : MonitorName(m), CgDisplayId(id) {} + DisplayId(String m, int id = -2) : MonitorName(m), CgDisplayId(id) {} operator bool () const { @@ -734,19 +738,28 @@ struct DisplayId bool operator== (const DisplayId& b) const { - return CgDisplayId == b.CgDisplayId && - (strstr(MonitorName.ToCStr(), b.MonitorName.ToCStr()) || - strstr(b.MonitorName.ToCStr(), MonitorName.ToCStr())); + if (MonitorName.IsEmpty() || b.MonitorName.IsEmpty()) + { + return CgDisplayId == b.CgDisplayId; + } + else + { + return strstr(MonitorName.ToCStr(), b.MonitorName.ToCStr()) || + strstr(b.MonitorName.ToCStr(), MonitorName.ToCStr()); + } } }; struct RendererParams { - int Multisample; - int Fullscreen; - DisplayId Display; - - RendererParams(int ms = 1) : Multisample(ms), Fullscreen(0) {} + int Multisample; + int Fullscreen; + DisplayId Display; + // Resolution of the rendering buffer used during creation. + // Allows buffer of different size then the widow if not zero. + Sizei Resolution; + + RendererParams(int ms = 1) : Multisample(ms), Fullscreen(0), Resolution(0) {} bool IsDisplaySet() const { @@ -777,7 +790,7 @@ protected: Ptr<ShaderSet> pPostProcessHeightmapShader; Ptr<Buffer> pFullScreenVertexBuffer; Color DistortionClearColor; - UPInt TotalTextureMemoryUsage; + size_t TotalTextureMemoryUsage; float FadeOutBorderFraction; int DistortionMeshNumTris[2]; @@ -893,7 +906,8 @@ public: RenderTarget* pHmdSpaceLayerRenderTargetLeftOrBothEyes, RenderTarget* pHmdSpaceLayerRenderTargetRight, RenderTarget* pStaticLayerRenderTargetLeftOrBothEyes, - RenderTarget* pStaticLayerRenderTargetRight); + RenderTarget* pStaticLayerRenderTargetRight, + RenderTarget* pOutputTarget); // Finish scene. virtual void FinishScene(); @@ -932,14 +946,14 @@ public: // Returns width of text in same units as drawing. If strsize is not null, stores width and height. // Can optionally return char-range selection rectangle. float MeasureText(const Font* font, const char* str, float size, float strsize[2] = NULL, - const UPInt charRange[2] = 0, Vector2f charRangeRect[2] = 0); - virtual void RenderText(const Font* font, const char* str, float x, float y, float size, Color c); + const size_t charRange[2] = 0, Vector2f charRangeRect[2] = 0); + virtual void RenderText(const Font* font, const char* str, float x, float y, float size, Color c, const Matrix4f* view = NULL); - virtual void FillRect(float left, float top, float right, float bottom, Color c); + virtual void FillRect(float left, float top, float right, float bottom, Color c, const Matrix4f* view = NULL); virtual void RenderLines ( int NumLines, Color c, float *x, float *y, float *z = NULL ); virtual void FillTexturedRect(float left, float top, float right, float bottom, float ul, float vt, float ur, float vb, Color c, Ptr<Texture> tex); - virtual void FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm); - virtual void RenderImage(float left, float top, float right, float bottom, ShaderFill* image, unsigned char alpha=255); + virtual void FillGradientRect(float left, float top, float right, float bottom, Color col_top, Color col_btm, const Matrix4f* view); + virtual void RenderImage(float left, float top, float right, float bottom, ShaderFill* image, unsigned char alpha=255, const Matrix4f* view = NULL); virtual Fill *CreateSimpleFill(int flags = Fill::F_Solid) = 0; Fill * CreateTextureFill(Texture* tex, bool useAlpha = false); @@ -963,7 +977,7 @@ public: VP = Recti( 0, 0, WindowWidth, WindowHeight ); } - UPInt GetTotalTextureMemoryUsage() const + size_t GetTotalTextureMemoryUsage() const { return TotalTextureMemoryUsage; } @@ -995,7 +1009,7 @@ public: // GPU Profiling // using (void) to avoid "unused param" warnings - virtual void BeginGpuEvent(const char* markerText, UInt32 markerColor) { (void)markerText; (void)markerColor; } + virtual void BeginGpuEvent(const char* markerText, uint32_t markerColor) { (void)markerText; (void)markerColor; } virtual void EndGpuEvent() { } protected: @@ -1015,7 +1029,7 @@ private: class AutoGpuProf { public: - AutoGpuProf(RenderDevice* device, const char* markerText, UInt32 color) + AutoGpuProf(RenderDevice* device, const char* markerText, uint32_t color) : mDevice(device) { device->BeginGpuEvent(markerText, color); } @@ -1023,7 +1037,7 @@ public: AutoGpuProf(RenderDevice* device, const char* markerText) : mDevice(device) { - UInt32 color = ((rand() & 0xFF) << 24) + + uint32_t color = ((rand() & 0xFF) << 24) + ((rand() & 0xFF) << 16) + ((rand() & 0xFF) << 8) + (rand() & 0xFF); @@ -1043,11 +1057,12 @@ int GetTextureSize(int format, int w, int h); // Filter an rgba image with a 2x2 box filter, for mipmaps. // Image size must be a power of 2. -void FilterRgba2x2(const UByte* src, int w, int h, UByte* dest); +void FilterRgba2x2(const uint8_t* src, int w, int h, uint8_t* dest); Texture* LoadTextureTga(RenderDevice* ren, File* f, unsigned char alpha = 255); Texture* LoadTextureDDS(RenderDevice* ren, File* f); -}} + +}} // namespace OVR::Render #endif diff --git a/Samples/CommonSrc/Render/Render_GL_Device.cpp b/Samples/CommonSrc/Render/Render_GL_Device.cpp index 774af07..f6e13ef 100644 --- a/Samples/CommonSrc/Render/Render_GL_Device.cpp +++ b/Samples/CommonSrc/Render/Render_GL_Device.cpp @@ -306,6 +306,23 @@ static const char* AlphaTextureFragShaderSrc = " gl_FragColor = oColor * vec4(1,1,1,texture2D(Texture0, oTexCoord).r);\n" "}\n"; +static const char* AlphaBlendedTextureFragShaderSrc = + "#version 110\n" + + "uniform sampler2D Texture0;\n" + + "varying vec4 oColor;\n" + "varying vec2 oTexCoord;\n" + + "void main()\n" + "{\n" + " vec4 finalColor = oColor;\n" + " finalColor *= texture2D(Texture0, oTexCoord);\n" + // Blend state expects premultiplied alpha + " finalColor.rgb *= finalColor.a;\n" + " gl_FragColor = finalColor;\n" + "}\n"; + static const char* MultiTextureFragShaderSrc = "#version 110\n" @@ -685,6 +702,7 @@ static const char* FShaderSrcs[FShader_Count] = GouraudFragShaderSrc, TextureFragShaderSrc, AlphaTextureFragShaderSrc, + AlphaBlendedTextureFragShaderSrc, PostProcessFragShaderWithChromAbSrc, LitSolidFragShaderSrc, LitTextureFragShaderSrc, @@ -700,40 +718,44 @@ RenderDevice::RenderDevice(const RendererParams&) { int GlMajorVersion = 0; int GlMinorVersion = 0; + bool isES = false; const char* glVersionString = (const char*)glGetString(GL_VERSION); - char prefix[64]; - bool foundVersion = false; - - for (int i = 10; i < 30; ++i) - { - int major = i / 10; - int minor = i % 10; - OVR_sprintf(prefix, 64, "%d.%d", major, minor); - if (strstr(glVersionString, prefix) == glVersionString) - { - GlMajorVersion = major; - GlMinorVersion = minor; - foundVersion = true; - break; - } - } - - if (!foundVersion) - { - glGetIntegerv(GL_MAJOR_VERSION, &GlMajorVersion); - glGetIntegerv(GL_MAJOR_VERSION, &GlMinorVersion); - } - - if (GlMajorVersion >= 3) + +#ifdef OVR_CC_MSVC + // Hack: This is using sscanf_s on MSVC to kill the security warning. + // Normally the two functions are not interchangeable because the string format + // is different for %s types, however we only use %d so it's fine. +#define _OVR_SSCANF sscanf_s +#else +#define _OVR_SSCANF sscanf +#endif + + isES = strstr( glVersionString, "OpenGL ES-CM" ) != NULL; + if( isES ) { - SupportsVao = true; + _OVR_SSCANF(glVersionString, "OpenGL ES-CM %d.%d", &GlMajorVersion, &GlMinorVersion); } else { - const char* extensions = (const char*)glGetString(GL_EXTENSIONS); - SupportsVao = (strstr("GL_ARB_vertex_array_object", extensions) != NULL); + isES = strstr( glVersionString, "OpenGL ES " ) != NULL; + if ( isES ) + { + _OVR_SSCANF(glVersionString, "OpenGL ES %d.%d", &GlMajorVersion, &GlMinorVersion); + } + else + { + _OVR_SSCANF(glVersionString, "%d.%d", &GlMajorVersion, &GlMinorVersion); + } } + +#undef _OVR_SSCANF + + OVR_ASSERT( isES == false ); + OVR_ASSERT(GlMajorVersion >= 2); + + const char* extensions = (const char*)glGetString(GL_EXTENSIONS); + SupportsVao = (GlMajorVersion >= 3) || (strstr("GL_ARB_vertex_array_object", extensions) != NULL); for (int i = 0; i < VShader_Count; i++) { @@ -806,7 +828,6 @@ Shader *RenderDevice::LoadBuiltinShader(ShaderStage stage, int shader) void RenderDevice::BeginRendering() { - //glEnable(GL_FRAMEBUFFER_SRGB); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glFrontFace(GL_CW); @@ -1339,7 +1360,7 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo { case Texture_RGBA: glformat = GL_RGBA; break; case Texture_R: glformat = GL_RED; break; - case Texture_Depth: glformat = GL_DEPTH_COMPONENT32F; gltype = GL_FLOAT; break; + case Texture_Depth: glformat = GL_DEPTH_COMPONENT; gltype = GL_FLOAT; break; case Texture_DXT1: glformat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break; case Texture_DXT3: glformat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break; case Texture_DXT5: glformat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break; @@ -1348,7 +1369,14 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo } Texture* NewTex = new Texture(this, width, height); glBindTexture(GL_TEXTURE_2D, NewTex->TexId); - OVR_ASSERT(!glGetError()); + GLint err = glGetError(); + + OVR_ASSERT(!err); + + if( err ) + { + printf("%d\n", err); + } if (format & Texture_Compressed) { @@ -1366,10 +1394,14 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo if (h < 1) h = 1; } } - else if (format & Texture_Depth) - glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, GL_DEPTH_COMPONENT, gltype, data); - else - glTexImage2D(GL_TEXTURE_2D, 0, glformat, width, height, 0, glformat, gltype, data); + else + { + bool isSRGB = ((format & Texture_TypeMask) == Texture_RGBA && (format & Texture_SRGB) != 0); + bool isDepth = ((format & Texture_Depth) != 0); + GLenum internalFormat = (isSRGB) ? GL_SRGB_ALPHA : (isDepth) ? GL_DEPTH_COMPONENT32F : glformat; + + glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, glformat, gltype, data); + } glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); @@ -1380,15 +1412,15 @@ Texture* RenderDevice::CreateTexture(int format, int width, int height, const vo { int srcw = width, srch = height; int level = 0; - UByte* mipmaps = NULL; + uint8_t* mipmaps = NULL; do { level++; int mipw = srcw >> 1; if (mipw < 1) mipw = 1; int miph = srch >> 1; if (miph < 1) miph = 1; if (mipmaps == NULL) - mipmaps = (UByte*)OVR_ALLOC(mipw * miph * 4); - FilterRgba2x2(level == 1 ? (const UByte*)data : mipmaps, srcw, srch, mipmaps); + mipmaps = (uint8_t*)OVR_ALLOC(mipw * miph * 4); + FilterRgba2x2(level == 1 ? (const uint8_t*)data : mipmaps, srcw, srch, mipmaps); glTexImage2D(GL_TEXTURE_2D, level, glformat, mipw, miph, 0, glformat, gltype, mipmaps); srcw = mipw; srch = miph; diff --git a/Samples/CommonSrc/Render/Render_GL_Device.h b/Samples/CommonSrc/Render/Render_GL_Device.h index 563c87b..9b8ccef 100644 --- a/Samples/CommonSrc/Render/Render_GL_Device.h +++ b/Samples/CommonSrc/Render/Render_GL_Device.h @@ -27,6 +27,9 @@ limitations under the License. #include "../Render/Render_Device.h" #if defined(OVR_OS_WIN32) + #include <WinSock2.h> + #include <WS2tcpip.h> + #define WIN32_LEAN_AND_MEAN #include <Windows.h> #include <GL/gl.h> #include <GL/glext.h> diff --git a/Samples/CommonSrc/Render/Render_GL_Win32_Device.cpp b/Samples/CommonSrc/Render/Render_GL_Win32_Device.cpp index 9bfcec9..8b4ac73 100644 --- a/Samples/CommonSrc/Render/Render_GL_Win32_Device.cpp +++ b/Samples/CommonSrc/Render/Render_GL_Win32_Device.cpp @@ -28,7 +28,7 @@ limitations under the License. namespace OVR { namespace Render { namespace GL { namespace Win32 { -typedef HRESULT (__stdcall *PFNDWMENABLECOMPOSITIONPROC) (UINT); +typedef HRESULT (WINAPI *PFNDWMENABLECOMPOSITIONPROC) (UINT); #pragma warning(disable : 4995) PFNDWMENABLECOMPOSITIONPROC DwmEnableComposition; @@ -201,16 +201,16 @@ BOOL CALLBACK MonitorEnumFunc(HMONITOR hMonitor, HDC, LPRECT, LPARAM dwData) { RenderDevice* renderer = (RenderDevice*)dwData; - MONITORINFOEX monitor; + MONITORINFOEXA monitor; monitor.cbSize = sizeof(monitor); - if (::GetMonitorInfo(hMonitor, &monitor) && monitor.szDevice[0]) + if (::GetMonitorInfoA(hMonitor, &monitor) && monitor.szDevice[0]) { - DISPLAY_DEVICE dispDev; + DISPLAY_DEVICEA dispDev; memset(&dispDev, 0, sizeof(dispDev)); dispDev.cb = sizeof(dispDev); - if (::EnumDisplayDevices(monitor.szDevice, 0, &dispDev, 0)) + if (::EnumDisplayDevicesA(monitor.szDevice, 0, &dispDev, 0)) { if (strstr(String(dispDev.DeviceName).ToCStr(), renderer->GetParams().Display.MonitorName.ToCStr())) { diff --git a/Samples/CommonSrc/Render/Render_GL_Win32_Device.h b/Samples/CommonSrc/Render/Render_GL_Win32_Device.h index 273e997..14d0b65 100644 --- a/Samples/CommonSrc/Render/Render_GL_Win32_Device.h +++ b/Samples/CommonSrc/Render/Render_GL_Win32_Device.h @@ -26,7 +26,10 @@ limitations under the License. #include "Render_GL_Device.h" -#ifdef WIN32 +#ifdef OVR_OS_WIN32 +#include <WinSock2.h> +#include <WS2tcpip.h> +#define WIN32_LEAN_AND_MEAN #include <Windows.h> #endif diff --git a/Samples/CommonSrc/Render/Render_LoadTextureDDS.cpp b/Samples/CommonSrc/Render/Render_LoadTextureDDS.cpp index cf20cdd..3da1441 100644 --- a/Samples/CommonSrc/Render/Render_LoadTextureDDS.cpp +++ b/Samples/CommonSrc/Render/Render_LoadTextureDDS.cpp @@ -28,45 +28,45 @@ limitations under the License. namespace OVR { namespace Render { -static const UPInt OVR_DDS_PF_FOURCC = 0x4; -static const UInt32 OVR_DXT1_MAGIC_NUMBER = 0x31545844; // "DXT1" -static const UInt32 OVR_DXT2_MAGIC_NUMBER = 0x32545844; // "DXT2" -static const UInt32 OVR_DXT3_MAGIC_NUMBER = 0x33545844; // "DXT3" -static const UInt32 OVR_DXT4_MAGIC_NUMBER = 0x34545844; // "DXT4" -static const UInt32 OVR_DXT5_MAGIC_NUMBER = 0x35545844; // "DXT5" +static const size_t OVR_DDS_PF_FOURCC = 0x4; +static const uint32_t OVR_DXT1_MAGIC_NUMBER = 0x31545844; // "DXT1" +static const uint32_t OVR_DXT2_MAGIC_NUMBER = 0x32545844; // "DXT2" +static const uint32_t OVR_DXT3_MAGIC_NUMBER = 0x33545844; // "DXT3" +static const uint32_t OVR_DXT4_MAGIC_NUMBER = 0x34545844; // "DXT4" +static const uint32_t OVR_DXT5_MAGIC_NUMBER = 0x35545844; // "DXT5" struct OVR_DDS_PIXELFORMAT { - UInt32 Size; - UInt32 Flags; - UInt32 FourCC; - UInt32 RGBBitCount; - UInt32 RBitMask; - UInt32 GBitMask; - UInt32 BBitMask; - UInt32 ABitMask; + uint32_t Size; + uint32_t Flags; + uint32_t FourCC; + uint32_t RGBBitCount; + uint32_t RBitMask; + uint32_t GBitMask; + uint32_t BBitMask; + uint32_t ABitMask; }; struct OVR_DDS_HEADER { - UInt32 Size; - UInt32 Flags; - UInt32 Height; - UInt32 Width; - UInt32 PitchOrLinearSize; - UInt32 Depth; - UInt32 MipMapCount; - UInt32 Reserved1[11]; - OVR_DDS_PIXELFORMAT PixelFormat; - UInt32 Caps; - UInt32 Caps2; - UInt32 Caps3; - UInt32 Caps4; - UInt32 Reserved2; + uint32_t Size; + uint32_t Flags; + uint32_t Height; + uint32_t Width; + uint32_t PitchOrLinearSize; + uint32_t Depth; + uint32_t MipMapCount; + uint32_t Reserved1[11]; + OVR_DDS_PIXELFORMAT PixelFormat; + uint32_t Caps; + uint32_t Caps2; + uint32_t Caps3; + uint32_t Caps4; + uint32_t Reserved2; }; // Returns -1 on failure, or a valid TextureFormat value on success -static inline int InterpretPixelFormatFourCC(UInt32 fourCC) { +static inline int InterpretPixelFormatFourCC(uint32_t fourCC) { switch (fourCC) { case OVR_DXT1_MAGIC_NUMBER: return Texture_DXT1; case OVR_DXT2_MAGIC_NUMBER: return Texture_DXT3; @@ -97,7 +97,7 @@ Texture* LoadTextureDDS(RenderDevice* ren, File* f) int format = Texture_RGBA; - UInt32 mipCount = header.MipMapCount; + uint32_t mipCount = header.MipMapCount; if(mipCount <= 0) { mipCount = 1; diff --git a/Samples/CommonSrc/Render/Render_XmlSceneLoader.cpp b/Samples/CommonSrc/Render/Render_XmlSceneLoader.cpp index 5f31250..3d035e2 100644 --- a/Samples/CommonSrc/Render/Render_XmlSceneLoader.cpp +++ b/Samples/CommonSrc/Render/Render_XmlSceneLoader.cpp @@ -52,9 +52,9 @@ bool XmlHandler::ReadFile(const char* fileName, OVR::Render::RenderDevice* pRend // Extract the relative path to our working directory for loading textures filePath[0] = 0; - SPInt pos = 0; - SPInt len = strlen(fileName); - for(SPInt i = len; i > 0; i--) + intptr_t pos = 0; + intptr_t len = strlen(fileName); + for(intptr_t i = len; i > 0; i--) { if (fileName[i-1]=='\\' || fileName[i-1]=='/') { @@ -76,7 +76,7 @@ bool XmlHandler::ReadFile(const char* fileName, OVR::Render::RenderDevice* pRend for(int i = 0; i < textureCount; ++i) { const char* textureName = pXmlTexture->Attribute("fileName"); - SPInt dotpos = strcspn(textureName, "."); + intptr_t dotpos = strcspn(textureName, "."); char fname[300]; if (pos == len) @@ -216,8 +216,8 @@ bool XmlHandler::ReadFile(const char* fileName, OVR::Render::RenderDevice* pRend Models[i]->Fill = shader; //add all the vertices to the model - const UPInt numVerts = vertices->GetSize(); - for(UPInt v = 0; v < numVerts; ++v) + const size_t numVerts = vertices->GetSize(); + for(size_t v = 0; v < numVerts; ++v) { if(diffuseTextureIndex > -1) { @@ -246,18 +246,18 @@ bool XmlHandler::ReadFile(const char* fileName, OVR::Render::RenderDevice* pRend const char* indexStr = pXmlModel->FirstChildElement("indices")-> FirstChild()->ToText()->Value(); - UPInt stringLength = strlen(indexStr); + size_t stringLength = strlen(indexStr); - for(UPInt j = 0; j < stringLength; ) + for(size_t j = 0; j < stringLength; ) { - UPInt k = j + 1; + size_t k = j + 1; for(; k < stringLength; ++k) { if (indexStr[k] == ' ') break; } char text[20]; - for(UPInt l = 0; l < k - j; ++l) + for(size_t l = 0; l < k - j; ++l) { text[l] = indexStr[j + l]; } @@ -268,10 +268,10 @@ bool XmlHandler::ReadFile(const char* fileName, OVR::Render::RenderDevice* pRend } // Reverse index order to match original expected orientation - Array<UInt16>& indices = Models[i]->Indices; - UPInt indexCount = indices.GetSize(); + Array<uint16_t>& indices = Models[i]->Indices; + size_t indexCount = indices.GetSize(); - for (UPInt revIndex = 0; revIndex < indexCount/2; revIndex++) + for (size_t revIndex = 0; revIndex < indexCount/2; revIndex++) { unsigned short itemp = indices[revIndex]; indices[revIndex] = indices[indexCount - revIndex - 1]; @@ -366,14 +366,14 @@ bool XmlHandler::ReadFile(const char* fileName, OVR::Render::RenderDevice* pRend void XmlHandler::ParseVectorString(const char* str, OVR::Array<OVR::Vector3f> *array, bool is2element) { - UPInt stride = is2element ? 2 : 3; - UPInt stringLength = strlen(str); - UPInt element = 0; + size_t stride = is2element ? 2 : 3; + size_t stringLength = strlen(str); + size_t element = 0; float v[3]; - for(UPInt j = 0; j < stringLength;) + for(size_t j = 0; j < stringLength;) { - UPInt k = j + 1; + size_t k = j + 1; for(; k < stringLength; ++k) { if(str[k] == ' ') @@ -382,7 +382,7 @@ void XmlHandler::ParseVectorString(const char* str, OVR::Array<OVR::Vector3f> *a } } char text[20]; - for(UPInt l = 0; l < k - j; ++l) + for(size_t l = 0; l < k - j; ++l) { text[l] = str[j + l]; } |