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