summaryrefslogtreecommitdiffstats
path: root/dom/canvas/WebGL2ContextSamplers.cpp
blob: bf5e04779294a827fac28e3c67e316875ebeb5af (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "WebGL2Context.h"
#include "WebGLSampler.h"
#include "GLContext.h"

namespace mozilla {

already_AddRefed<WebGLSampler>
WebGL2Context::CreateSampler()
{
    if (IsContextLost())
        return nullptr;

    GLuint sampler;
    MakeContextCurrent();
    gl->fGenSamplers(1, &sampler);

    RefPtr<WebGLSampler> globj = new WebGLSampler(this, sampler);
    return globj.forget();
}

void
WebGL2Context::DeleteSampler(WebGLSampler* sampler)
{
    if (!ValidateDeleteObject("deleteSampler", sampler))
        return;

    for (int n = 0; n < mGLMaxTextureUnits; n++) {
        if (mBoundSamplers[n] == sampler) {
            mBoundSamplers[n] = nullptr;

            InvalidateResolveCacheForTextureWithTexUnit(n);
        }
    }

    sampler->RequestDelete();
}

bool
WebGL2Context::IsSampler(const WebGLSampler* sampler)
{
    if (!ValidateIsObject("isSampler", sampler))
        return false;

    MakeContextCurrent();
    return gl->fIsSampler(sampler->mGLName);
}

void
WebGL2Context::BindSampler(GLuint unit, WebGLSampler* sampler)
{
    if (IsContextLost())
        return;

    if (sampler && !ValidateObject("bindSampler", *sampler))
        return;

    if (GLint(unit) >= mGLMaxTextureUnits)
        return ErrorInvalidValue("bindSampler: unit must be < %d", mGLMaxTextureUnits);

    ////

    gl->MakeCurrent();
    gl->fBindSampler(unit, sampler ? sampler->mGLName : 0);

    InvalidateResolveCacheForTextureWithTexUnit(unit);
    mBoundSamplers[unit] = sampler;
}

void
WebGL2Context::SamplerParameteri(WebGLSampler& sampler, GLenum pname, GLint param)
{
    const char funcName[] = "samplerParameteri";
    if (IsContextLost())
        return;

    if (!ValidateObject(funcName, sampler))
        return;

    sampler.SamplerParameter(funcName, pname, FloatOrInt(param));
}

void
WebGL2Context::SamplerParameterf(WebGLSampler& sampler, GLenum pname, GLfloat param)
{
    const char funcName[] = "samplerParameterf";
    if (IsContextLost())
        return;

    if (!ValidateObject(funcName, sampler))
        return;

    sampler.SamplerParameter(funcName, pname, FloatOrInt(param));
}

void
WebGL2Context::GetSamplerParameter(JSContext*, const WebGLSampler& sampler, GLenum pname,
                                   JS::MutableHandleValue retval)
{
    const char funcName[] = "getSamplerParameter";
    retval.setNull();

    if (IsContextLost())
        return;

    if (!ValidateObject(funcName, sampler))
        return;

    ////

    gl->MakeCurrent();

    switch (pname) {
    case LOCAL_GL_TEXTURE_MIN_FILTER:
    case LOCAL_GL_TEXTURE_MAG_FILTER:
    case LOCAL_GL_TEXTURE_WRAP_S:
    case LOCAL_GL_TEXTURE_WRAP_T:
    case LOCAL_GL_TEXTURE_WRAP_R:
    case LOCAL_GL_TEXTURE_COMPARE_MODE:
    case LOCAL_GL_TEXTURE_COMPARE_FUNC:
        {
            GLint param = 0;
            gl->fGetSamplerParameteriv(sampler.mGLName, pname, &param);
            retval.set(JS::Int32Value(param));
        }
        return;

    case LOCAL_GL_TEXTURE_MIN_LOD:
    case LOCAL_GL_TEXTURE_MAX_LOD:
        {
            GLfloat param = 0;
            gl->fGetSamplerParameterfv(sampler.mGLName, pname, &param);
            retval.set(JS::Float32Value(param));
        }
        return;

    default:
        ErrorInvalidEnumArg(funcName, "pname", pname);
        return;
    }
}

} // namespace mozilla