rsSampler.cpp revision f2649a961db2995e9e24a6c98f8a419f1496c1b7
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#include <GLES/gl.h>
18#include <GLES/glext.h>
19
20#include "rsContext.h"
21#include "rsSampler.h"
22
23
24using namespace android;
25using namespace android::renderscript;
26
27
28Sampler::Sampler(Context *rsc) : ObjectBase(rsc)
29{
30    mAllocFile = __FILE__;
31    mAllocLine = __LINE__;
32    // Should not get called.
33    rsAssert(0);
34}
35
36Sampler::Sampler(Context *rsc,
37                 RsSamplerValue magFilter,
38                 RsSamplerValue minFilter,
39                 RsSamplerValue wrapS,
40                 RsSamplerValue wrapT,
41                 RsSamplerValue wrapR) : ObjectBase(rsc)
42{
43    mAllocFile = __FILE__;
44    mAllocLine = __LINE__;
45    mMagFilter = magFilter;
46    mMinFilter = minFilter;
47    mWrapS = wrapS;
48    mWrapT = wrapT;
49    mWrapR = wrapR;
50}
51
52Sampler::~Sampler()
53{
54}
55
56void Sampler::setupGL()
57{
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
67
68    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
69    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
70    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
71    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
72
73}
74
75void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
76{
77    ss->mSamplers[slot].set(this);
78    mBoundSlot = slot;
79}
80
81void Sampler::unbindFromContext(SamplerState *ss)
82{
83    int32_t slot = mBoundSlot;
84    mBoundSlot = -1;
85    ss->mSamplers[slot].clear();
86}
87
88void SamplerState::setupGL()
89{
90    for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
91        Sampler *s = mSamplers[ct].get();
92        if (s) {
93            s->setupGL();
94        } else {
95            glBindTexture(GL_TEXTURE_2D, 0);
96        }
97    }
98}
99
100////////////////////////////////
101
102namespace android {
103namespace renderscript {
104
105
106void rsi_SamplerBegin(Context *rsc)
107{
108    SamplerState * ss = &rsc->mStateSampler;
109
110    ss->mMagFilter = RS_SAMPLER_LINEAR;
111    ss->mMinFilter = RS_SAMPLER_LINEAR;
112    ss->mWrapS = RS_SAMPLER_WRAP;
113    ss->mWrapT = RS_SAMPLER_WRAP;
114    ss->mWrapR = RS_SAMPLER_WRAP;
115}
116
117void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
118{
119    SamplerState * ss = &rsc->mStateSampler;
120
121    switch(param) {
122    case RS_SAMPLER_MAG_FILTER:
123        ss->mMagFilter = value;
124        break;
125    case RS_SAMPLER_MIN_FILTER:
126        ss->mMinFilter = value;
127        break;
128    case RS_SAMPLER_WRAP_S:
129        ss->mWrapS = value;
130        break;
131    case RS_SAMPLER_WRAP_T:
132        ss->mWrapT = value;
133        break;
134    case RS_SAMPLER_WRAP_R:
135        ss->mWrapR = value;
136        break;
137    }
138
139}
140
141RsSampler rsi_SamplerCreate(Context *rsc)
142{
143    SamplerState * ss = &rsc->mStateSampler;
144
145
146    Sampler * s = new Sampler(rsc,
147                              ss->mMagFilter,
148                              ss->mMinFilter,
149                              ss->mWrapS,
150                              ss->mWrapT,
151                              ss->mWrapR);
152    s->incUserRef();
153    return s;
154}
155
156
157}}
158