File: depth.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (177 lines) | stat: -rw-r--r-- 6,137 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "depth.hpp"

#include <algorithm>
#include <array>

#include <components/debug/debuglog.hpp>
#include <components/sceneutil/glextensions.hpp>
#include <components/settings/values.hpp>

namespace SceneUtil
{
    void setCameraClearDepth(osg::Camera* camera)
    {
        camera->setClearDepth(AutoDepth::isReversed() ? 0.0 : 1.0);
    }

    osg::Matrix getReversedZProjectionMatrixAsPerspectiveInf(double fov, double aspect, double near)
    {
        double A = 1.0 / std::tan(osg::DegreesToRadians(fov) / 2.0);
        return osg::Matrix(A / aspect, 0, 0, 0, 0, A, 0, 0, 0, 0, 0, -1, 0, 0, near, 0);
    }

    osg::Matrix getReversedZProjectionMatrixAsPerspective(double fov, double aspect, double near, double far)
    {
        double A = 1.0 / std::tan(osg::DegreesToRadians(fov) / 2.0);
        return osg::Matrix(
            A / aspect, 0, 0, 0, 0, A, 0, 0, 0, 0, near / (far - near), -1, 0, 0, (far * near) / (far - near), 0);
    }

    osg::Matrix getReversedZProjectionMatrixAsOrtho(
        double left, double right, double bottom, double top, double near, double far)
    {
        return osg::Matrix(2 / (right - left), 0, 0, 0, 0, 2 / (top - bottom), 0, 0, 0, 0, 1 / (far - near), 0,
            (right + left) / (left - right), (top + bottom) / (bottom - top), far / (far - near), 1);
    }

    bool isDepthFormat(GLenum format)
    {
        constexpr std::array<GLenum, 8> formats = {
            GL_DEPTH_COMPONENT32F,
            GL_DEPTH_COMPONENT32F_NV,
            GL_DEPTH_COMPONENT16,
            GL_DEPTH_COMPONENT24,
            GL_DEPTH_COMPONENT32,
            GL_DEPTH32F_STENCIL8,
            GL_DEPTH32F_STENCIL8_NV,
            GL_DEPTH24_STENCIL8,
        };

        return std::find(formats.cbegin(), formats.cend(), format) != formats.cend();
    }

    bool isDepthStencilFormat(GLenum format)
    {
        constexpr std::array<GLenum, 8> formats = {
            GL_DEPTH32F_STENCIL8,
            GL_DEPTH32F_STENCIL8_NV,
            GL_DEPTH24_STENCIL8,
        };

        return std::find(formats.cbegin(), formats.cend(), format) != formats.cend();
    }

    void getDepthFormatSourceFormatAndType(GLenum internalFormat, GLenum& sourceFormat, GLenum& sourceType)
    {
        switch (internalFormat)
        {
            case GL_DEPTH_COMPONENT16:
            case GL_DEPTH_COMPONENT24:
            case GL_DEPTH_COMPONENT32:
                sourceType = GL_UNSIGNED_INT;
                sourceFormat = GL_DEPTH_COMPONENT;
                break;
            case GL_DEPTH_COMPONENT32F:
            case GL_DEPTH_COMPONENT32F_NV:
                sourceType = GL_FLOAT;
                sourceFormat = GL_DEPTH_COMPONENT;
                break;
            case GL_DEPTH24_STENCIL8:
                sourceType = GL_UNSIGNED_INT_24_8_EXT;
                sourceFormat = GL_DEPTH_STENCIL_EXT;
                break;
            case GL_DEPTH32F_STENCIL8:
            case GL_DEPTH32F_STENCIL8_NV:
                sourceType = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
                sourceFormat = GL_DEPTH_STENCIL_EXT;
                break;
            default:
                sourceType = GL_UNSIGNED_INT;
                sourceFormat = GL_DEPTH_COMPONENT;
                break;
        }
    }

    GLenum getDepthFormatOfDepthStencilFormat(GLenum internalFormat)
    {
        switch (internalFormat)
        {
            case GL_DEPTH24_STENCIL8:
                return GL_DEPTH_COMPONENT24;
                break;
            case GL_DEPTH32F_STENCIL8:
                return GL_DEPTH_COMPONENT32F;
                break;
            case GL_DEPTH32F_STENCIL8_NV:
                return GL_DEPTH_COMPONENT32F_NV;
                break;
            default:
                return internalFormat;
                break;
        }
    }

    void SelectDepthFormatOperation::operator()(osg::GraphicsContext* graphicsContext)
    {
        bool enableReverseZ = false;

        if (Settings::camera().mReverseZ)
        {
            if (SceneUtil::getGLExtensions().isClipControlSupported)
            {
                enableReverseZ = true;
                Log(Debug::Info) << "Using reverse-z depth buffer";
            }
            else
                Log(Debug::Warning) << "GL_ARB_clip_control not supported: disabling reverse-z depth buffer";
        }
        else
            Log(Debug::Info) << "Using standard depth buffer";

        SceneUtil::AutoDepth::setReversed(enableReverseZ);

        constexpr char errPreamble[] = "Postprocessing and floating point depth buffers disabled: ";
        std::vector<GLenum> requestedFormats;
        unsigned int contextID = graphicsContext->getState()->getContextID();
        if (SceneUtil::AutoDepth::isReversed())
        {
            if (osg::isGLExtensionSupported(contextID, "GL_ARB_depth_buffer_float"))
            {
                requestedFormats.push_back(GL_DEPTH32F_STENCIL8);
            }
            else if (osg::isGLExtensionSupported(contextID, "GL_NV_depth_buffer_float"))
            {
                requestedFormats.push_back(GL_DEPTH32F_STENCIL8_NV);
            }
            else
            {
                Log(Debug::Warning) << errPreamble
                                    << "'GL_ARB_depth_buffer_float' and 'GL_NV_depth_buffer_float' unsupported.";
            }
        }

        requestedFormats.push_back(GL_DEPTH24_STENCIL8);
        if (mSupportedFormats.empty())
        {
            SceneUtil::AutoDepth::setDepthFormat(requestedFormats.front());
        }
        else
        {
            for (auto requestedFormat : requestedFormats)
            {
                if (std::find(mSupportedFormats.cbegin(), mSupportedFormats.cend(), requestedFormat)
                    != mSupportedFormats.cend())
                {
                    SceneUtil::AutoDepth::setDepthFormat(requestedFormat);
                    break;
                }
            }
        }
    }

    void AutoDepth::setDepthFormat(GLenum format)
    {
        sDepthInternalFormat = format;
        getDepthFormatSourceFormatAndType(sDepthInternalFormat, sDepthSourceFormat, sDepthSourceType);
    }
}