rsSampler.cpp revision 84e4027f83b20af59f5b1fc52be6e45f159d3970
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_RS_BUILD_FOR_HOST
18#include <GLES/gl.h>
19#include <GLES/glext.h>
20#include "rsContext.h"
21#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#include <OpenGL/glext.h>
25#endif //ANDROID_RS_BUILD_FOR_HOST
26
27#include "rsSampler.h"
28
29
30using namespace android;
31using namespace android::renderscript;
32
33
34Sampler::Sampler(Context *rsc) : ObjectBase(rsc) {
35    // Should not get called.
36    rsAssert(0);
37}
38
39Sampler::Sampler(Context *rsc,
40                 RsSamplerValue magFilter,
41                 RsSamplerValue minFilter,
42                 RsSamplerValue wrapS,
43                 RsSamplerValue wrapT,
44                 RsSamplerValue wrapR,
45                 float aniso) : ObjectBase(rsc) {
46    mMagFilter = magFilter;
47    mMinFilter = minFilter;
48    mWrapS = wrapS;
49    mWrapT = wrapT;
50    mWrapR = wrapR;
51    mAniso = aniso;
52}
53
54Sampler::~Sampler() {
55}
56
57void Sampler::setupGL(const Context *rsc, const Allocation *tex) {
58    GLenum trans[] = {
59        GL_NEAREST, //RS_SAMPLER_NEAREST,
60        GL_LINEAR, //RS_SAMPLER_LINEAR,
61        GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
62        GL_REPEAT, //RS_SAMPLER_WRAP,
63        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
64    };
65
66    GLenum transNP[] = {
67        GL_NEAREST, //RS_SAMPLER_NEAREST,
68        GL_LINEAR, //RS_SAMPLER_LINEAR,
69        GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
70        GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
71        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
72    };
73
74    // This tells us the correct texture type
75    GLenum target = (GLenum)tex->getGLTarget();
76
77    if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
78        if (tex->getHasGraphicsMipmaps() && rsc->ext_GL_NV_texture_npot_2D_mipmap()) {
79            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
80        } else {
81            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
82        }
83        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
84        glTexParameteri(target, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
85        glTexParameteri(target, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
86    } else {
87        if (tex->getHasGraphicsMipmaps()) {
88            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
89        } else {
90            glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
91        }
92        glTexParameteri(target, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
93        glTexParameteri(target, GL_TEXTURE_WRAP_S, trans[mWrapS]);
94        glTexParameteri(target, GL_TEXTURE_WRAP_T, trans[mWrapT]);
95    }
96
97    float anisoValue = rsMin(rsc->ext_texture_max_aniso(), mAniso);
98    if (rsc->ext_texture_max_aniso() > 1.0f) {
99        glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
100    }
101
102    rsc->checkError("Sampler::setupGL2 tex env");
103}
104
105void Sampler::bindToContext(SamplerState *ss, uint32_t slot) {
106    ss->mSamplers[slot].set(this);
107    mBoundSlot = slot;
108}
109
110void Sampler::unbindFromContext(SamplerState *ss) {
111    int32_t slot = mBoundSlot;
112    mBoundSlot = -1;
113    ss->mSamplers[slot].clear();
114}
115
116void Sampler::serialize(OStream *stream) const {
117}
118
119Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) {
120    return NULL;
121}
122
123////////////////////////////////
124
125namespace android {
126namespace renderscript {
127
128
129void rsi_SamplerBegin(Context *rsc) {
130    SamplerState * ss = &rsc->mStateSampler;
131
132    ss->mMagFilter = RS_SAMPLER_LINEAR;
133    ss->mMinFilter = RS_SAMPLER_LINEAR;
134    ss->mWrapS = RS_SAMPLER_WRAP;
135    ss->mWrapT = RS_SAMPLER_WRAP;
136    ss->mWrapR = RS_SAMPLER_WRAP;
137    ss->mAniso = 1.0f;
138}
139
140void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value) {
141    SamplerState * ss = &rsc->mStateSampler;
142
143    switch (param) {
144    case RS_SAMPLER_MAG_FILTER:
145        ss->mMagFilter = value;
146        break;
147    case RS_SAMPLER_MIN_FILTER:
148        ss->mMinFilter = value;
149        break;
150    case RS_SAMPLER_WRAP_S:
151        ss->mWrapS = value;
152        break;
153    case RS_SAMPLER_WRAP_T:
154        ss->mWrapT = value;
155        break;
156    case RS_SAMPLER_WRAP_R:
157        ss->mWrapR = value;
158        break;
159    default:
160        LOGE("Attempting to set invalid value on sampler");
161        break;
162    }
163}
164
165void rsi_SamplerSet2(Context *rsc, RsSamplerParam param, float value) {
166    SamplerState * ss = &rsc->mStateSampler;
167
168    switch (param) {
169    case RS_SAMPLER_ANISO:
170        ss->mAniso = value;
171        break;
172    default:
173        LOGE("Attempting to set invalid value on sampler");
174        break;
175    }
176}
177
178RsSampler rsi_SamplerCreate(Context *rsc) {
179    SamplerState * ss = &rsc->mStateSampler;
180
181    Sampler * s = new Sampler(rsc,
182                              ss->mMagFilter,
183                              ss->mMinFilter,
184                              ss->mWrapS,
185                              ss->mWrapT,
186                              ss->mWrapR,
187                              ss->mAniso);
188    s->incUserRef();
189    return s;
190}
191
192}}
193