blob: 47b8a613aa0ad8b22af59e360d36ba84d62dc104 [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
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
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Samsd19f10d2009-05-22 14:03:28 -070018#include <GLES/gl.h>
19#include <GLES/glext.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070020#include "rsContext.h"
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#include <OpenGL/glext.h>
25#endif //ANDROID_RS_BUILD_FOR_HOST
26
Jason Samsd19f10d2009-05-22 14:03:28 -070027#include "rsSampler.h"
28
Jason Sams4b962e52009-06-22 17:15:15 -070029
Jason Samsd19f10d2009-05-22 14:03:28 -070030using namespace android;
31using namespace android::renderscript;
32
33
Jason Samsa9e7a052009-09-25 14:51:22 -070034Sampler::Sampler(Context *rsc) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070035{
Jason Sams61f08d62009-09-25 16:37:33 -070036 mAllocFile = __FILE__;
37 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070038 // Should not get called.
39 rsAssert(0);
40}
41
Jason Samsa9e7a052009-09-25 14:51:22 -070042Sampler::Sampler(Context *rsc,
43 RsSamplerValue magFilter,
Jason Samsd19f10d2009-05-22 14:03:28 -070044 RsSamplerValue minFilter,
45 RsSamplerValue wrapS,
46 RsSamplerValue wrapT,
Jason Samsa9e7a052009-09-25 14:51:22 -070047 RsSamplerValue wrapR) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070048{
Jason Sams61f08d62009-09-25 16:37:33 -070049 mAllocFile = __FILE__;
50 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070051 mMagFilter = magFilter;
52 mMinFilter = minFilter;
53 mWrapS = wrapS;
54 mWrapT = wrapT;
55 mWrapR = wrapR;
56}
57
58Sampler::~Sampler()
59{
60}
61
Jason Sams2978bfc2010-02-22 15:37:51 -080062void Sampler::setupGL(const Context *rsc, bool npot)
Jason Samsd19f10d2009-05-22 14:03:28 -070063{
Jason Sams243e3fb2009-05-28 16:16:24 -070064 GLenum trans[] = {
Jason Sams02fb2cb2009-05-28 15:37:57 -070065 GL_NEAREST, //RS_SAMPLER_NEAREST,
66 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams243e3fb2009-05-28 16:16:24 -070067 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
68 GL_REPEAT, //RS_SAMPLER_WRAP,
69 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Jason Sams243e3fb2009-05-28 16:16:24 -070070 };
Jason Sams02fb2cb2009-05-28 15:37:57 -070071
Jason Samscfc04362010-09-14 14:59:03 -070072 GLenum transNP[] = {
73 GL_NEAREST, //RS_SAMPLER_NEAREST,
74 GL_LINEAR, //RS_SAMPLER_LINEAR,
75 GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
76 GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
77 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
78 };
Jason Sams2978bfc2010-02-22 15:37:51 -080079
Jason Samscfc04362010-09-14 14:59:03 -070080 if (!rsc->ext_OES_texture_npot() && npot) {
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
Jason Sams2978bfc2010-02-22 15:37:51 -080085 } else {
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
Jason Samscfc04362010-09-14 14:59:03 -070087 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Sams2978bfc2010-02-22 15:37:51 -080090 }
Jason Samsd19f10d2009-05-22 14:03:28 -070091
Jason Samscfc04362010-09-14 14:59:03 -070092 rsc->checkError("Sampler::setupGL2 tex env");
Jason Samsd19f10d2009-05-22 14:03:28 -070093}
94
95void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
96{
97 ss->mSamplers[slot].set(this);
98 mBoundSlot = slot;
99}
100
101void Sampler::unbindFromContext(SamplerState *ss)
102{
103 int32_t slot = mBoundSlot;
104 mBoundSlot = -1;
105 ss->mSamplers[slot].clear();
106}
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700107
108void Sampler::serialize(OStream *stream) const
109{
Jason Samscfc04362010-09-14 14:59:03 -0700110
Alex Sakhartchoukaa7d2882010-05-21 12:53:13 -0700111}
112
113Sampler *Sampler::createFromStream(Context *rsc, IStream *stream)
114{
115 return NULL;
116}
117
Jason Sams5dbfe932010-01-27 14:41:43 -0800118/*
Jason Samsd19f10d2009-05-22 14:03:28 -0700119void SamplerState::setupGL()
120{
Jason Sams02fb2cb2009-05-28 15:37:57 -0700121 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700122 Sampler *s = mSamplers[ct].get();
123 if (s) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800124 s->setupGL(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700125 } else {
126 glBindTexture(GL_TEXTURE_2D, 0);
127 }
128 }
Jason Sams5dbfe932010-01-27 14:41:43 -0800129}*/
Jason Samsd19f10d2009-05-22 14:03:28 -0700130
131////////////////////////////////
132
133namespace android {
134namespace renderscript {
135
136
137void rsi_SamplerBegin(Context *rsc)
138{
139 SamplerState * ss = &rsc->mStateSampler;
140
141 ss->mMagFilter = RS_SAMPLER_LINEAR;
142 ss->mMinFilter = RS_SAMPLER_LINEAR;
143 ss->mWrapS = RS_SAMPLER_WRAP;
144 ss->mWrapT = RS_SAMPLER_WRAP;
145 ss->mWrapR = RS_SAMPLER_WRAP;
146}
147
148void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
149{
150 SamplerState * ss = &rsc->mStateSampler;
151
152 switch(param) {
153 case RS_SAMPLER_MAG_FILTER:
154 ss->mMagFilter = value;
155 break;
156 case RS_SAMPLER_MIN_FILTER:
157 ss->mMinFilter = value;
158 break;
159 case RS_SAMPLER_WRAP_S:
160 ss->mWrapS = value;
161 break;
162 case RS_SAMPLER_WRAP_T:
163 ss->mWrapT = value;
164 break;
165 case RS_SAMPLER_WRAP_R:
166 ss->mWrapR = value;
167 break;
168 }
169
170}
171
172RsSampler rsi_SamplerCreate(Context *rsc)
173{
174 SamplerState * ss = &rsc->mStateSampler;
175
176
Jason Samsa9e7a052009-09-25 14:51:22 -0700177 Sampler * s = new Sampler(rsc,
178 ss->mMagFilter,
Jason Sams7ce033d2009-08-18 14:14:24 -0700179 ss->mMinFilter,
180 ss->mWrapS,
Jason Samsd19f10d2009-05-22 14:03:28 -0700181 ss->mWrapT,
182 ss->mWrapR);
Jason Sams07ae4062009-08-27 20:23:34 -0700183 s->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700184 return s;
185}
186
Jason Sams02fb2cb2009-05-28 15:37:57 -0700187
Jason Samsd19f10d2009-05-22 14:03:28 -0700188}}