rsSampler.cpp revision fb6b614bcea88a587a7ea4530be45ff0ffa0210e
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{
36    mAllocFile = __FILE__;
37    mAllocLine = __LINE__;
38    // Should not get called.
39    rsAssert(0);
40}
41
42Sampler::Sampler(Context *rsc,
43                 RsSamplerValue magFilter,
44                 RsSamplerValue minFilter,
45                 RsSamplerValue wrapS,
46                 RsSamplerValue wrapT,
47                 RsSamplerValue wrapR) : ObjectBase(rsc)
48{
49    mAllocFile = __FILE__;
50    mAllocLine = __LINE__;
51    mMagFilter = magFilter;
52    mMinFilter = minFilter;
53    mWrapS = wrapS;
54    mWrapT = wrapT;
55    mWrapR = wrapR;
56}
57
58Sampler::~Sampler()
59{
60}
61
62void Sampler::setupGL(const Context *rsc, bool npot)
63{
64    GLenum trans[] = {
65        GL_NEAREST, //RS_SAMPLER_NEAREST,
66        GL_LINEAR, //RS_SAMPLER_LINEAR,
67        GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
68        GL_REPEAT, //RS_SAMPLER_WRAP,
69        GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
70
71    };
72
73    bool forceNonMip = false;
74    if (!rsc->ext_OES_texture_npot() && npot) {
75        forceNonMip = true;
76    }
77
78    if ((mMinFilter == RS_SAMPLER_LINEAR_MIP_LINEAR) && forceNonMip) {
79        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
80    } else {
81        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
82    }
83    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
84    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
85    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
86
87
88    rsc->checkError("ProgramFragment::setupGL2 tex env");
89}
90
91void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
92{
93    ss->mSamplers[slot].set(this);
94    mBoundSlot = slot;
95}
96
97void Sampler::unbindFromContext(SamplerState *ss)
98{
99    int32_t slot = mBoundSlot;
100    mBoundSlot = -1;
101    ss->mSamplers[slot].clear();
102}
103
104void Sampler::serialize(OStream *stream) const
105{
106
107}
108
109Sampler *Sampler::createFromStream(Context *rsc, IStream *stream)
110{
111    return NULL;
112}
113
114/*
115void SamplerState::setupGL()
116{
117    for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
118        Sampler *s = mSamplers[ct].get();
119        if (s) {
120            s->setupGL(rsc);
121        } else {
122            glBindTexture(GL_TEXTURE_2D, 0);
123        }
124    }
125}*/
126
127////////////////////////////////
128
129namespace android {
130namespace renderscript {
131
132
133void rsi_SamplerBegin(Context *rsc)
134{
135    SamplerState * ss = &rsc->mStateSampler;
136
137    ss->mMagFilter = RS_SAMPLER_LINEAR;
138    ss->mMinFilter = RS_SAMPLER_LINEAR;
139    ss->mWrapS = RS_SAMPLER_WRAP;
140    ss->mWrapT = RS_SAMPLER_WRAP;
141    ss->mWrapR = RS_SAMPLER_WRAP;
142}
143
144void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
145{
146    SamplerState * ss = &rsc->mStateSampler;
147
148    switch(param) {
149    case RS_SAMPLER_MAG_FILTER:
150        ss->mMagFilter = value;
151        break;
152    case RS_SAMPLER_MIN_FILTER:
153        ss->mMinFilter = value;
154        break;
155    case RS_SAMPLER_WRAP_S:
156        ss->mWrapS = value;
157        break;
158    case RS_SAMPLER_WRAP_T:
159        ss->mWrapT = value;
160        break;
161    case RS_SAMPLER_WRAP_R:
162        ss->mWrapR = value;
163        break;
164    }
165
166}
167
168RsSampler rsi_SamplerCreate(Context *rsc)
169{
170    SamplerState * ss = &rsc->mStateSampler;
171
172
173    Sampler * s = new Sampler(rsc,
174                              ss->mMagFilter,
175                              ss->mMinFilter,
176                              ss->mWrapS,
177                              ss->mWrapT,
178                              ss->mWrapR);
179    s->incUserRef();
180    return s;
181}
182
183
184}}
185