blob: 5693c8abeda8b355b783615cdcbf39fa5e5d6d06 [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
Jason Samsd19f10d2009-05-22 14:03:28 -070017#include <GLES/gl.h>
18#include <GLES/glext.h>
Jason Samsd19f10d2009-05-22 14:03:28 -070019
20#include "rsContext.h"
21#include "rsSampler.h"
22
Jason Sams4b962e52009-06-22 17:15:15 -070023
Jason Samsd19f10d2009-05-22 14:03:28 -070024using namespace android;
25using namespace android::renderscript;
26
27
Jason Samsa9e7a052009-09-25 14:51:22 -070028Sampler::Sampler(Context *rsc) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070029{
Jason Sams61f08d62009-09-25 16:37:33 -070030 mAllocFile = __FILE__;
31 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070032 // Should not get called.
33 rsAssert(0);
34}
35
Jason Samsa9e7a052009-09-25 14:51:22 -070036Sampler::Sampler(Context *rsc,
37 RsSamplerValue magFilter,
Jason Samsd19f10d2009-05-22 14:03:28 -070038 RsSamplerValue minFilter,
39 RsSamplerValue wrapS,
40 RsSamplerValue wrapT,
Jason Samsa9e7a052009-09-25 14:51:22 -070041 RsSamplerValue wrapR) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070042{
Jason Sams61f08d62009-09-25 16:37:33 -070043 mAllocFile = __FILE__;
44 mAllocLine = __LINE__;
Jason Samsd19f10d2009-05-22 14:03:28 -070045 mMagFilter = magFilter;
46 mMinFilter = minFilter;
47 mWrapS = wrapS;
48 mWrapT = wrapT;
49 mWrapR = wrapR;
50}
51
52Sampler::~Sampler()
53{
54}
55
Jason Sams2978bfc2010-02-22 15:37:51 -080056void Sampler::setupGL(const Context *rsc, bool npot)
Jason Samsd19f10d2009-05-22 14:03:28 -070057{
Jason Sams243e3fb2009-05-28 16:16:24 -070058 GLenum trans[] = {
Jason Sams02fb2cb2009-05-28 15:37:57 -070059 GL_NEAREST, //RS_SAMPLER_NEAREST,
60 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams243e3fb2009-05-28 16:16:24 -070061 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
62 GL_REPEAT, //RS_SAMPLER_WRAP,
63 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Jason Sams02fb2cb2009-05-28 15:37:57 -070064
Jason Sams243e3fb2009-05-28 16:16:24 -070065 };
Jason Sams02fb2cb2009-05-28 15:37:57 -070066
Jason Sams2978bfc2010-02-22 15:37:51 -080067 bool forceNonMip = false;
68 if (!rsc->ext_OES_texture_npot() && npot) {
69 forceNonMip = true;
70 }
71
72 if ((mMinFilter == RS_SAMPLER_LINEAR_MIP_LINEAR) && forceNonMip) {
Mathias Agopianf9954c72010-12-15 16:59:55 -080073 if (rsc->ext_GL_IMG_texture_npot()) {
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
75 } else {
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
77 }
Jason Sams2978bfc2010-02-22 15:37:51 -080078 } else {
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
80 }
Jason Sams243e3fb2009-05-28 16:16:24 -070081 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Samsd19f10d2009-05-22 14:03:28 -070084
Jason Sams2978bfc2010-02-22 15:37:51 -080085
Jason Sams5dbfe932010-01-27 14:41:43 -080086 rsc->checkError("ProgramFragment::setupGL2 tex env");
Jason Samsd19f10d2009-05-22 14:03:28 -070087}
88
89void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
90{
91 ss->mSamplers[slot].set(this);
92 mBoundSlot = slot;
93}
94
95void Sampler::unbindFromContext(SamplerState *ss)
96{
97 int32_t slot = mBoundSlot;
98 mBoundSlot = -1;
99 ss->mSamplers[slot].clear();
100}
Jason Sams5dbfe932010-01-27 14:41:43 -0800101/*
Jason Samsd19f10d2009-05-22 14:03:28 -0700102void SamplerState::setupGL()
103{
Jason Sams02fb2cb2009-05-28 15:37:57 -0700104 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
Jason Samsd19f10d2009-05-22 14:03:28 -0700105 Sampler *s = mSamplers[ct].get();
106 if (s) {
Jason Sams5dbfe932010-01-27 14:41:43 -0800107 s->setupGL(rsc);
Jason Samsd19f10d2009-05-22 14:03:28 -0700108 } else {
109 glBindTexture(GL_TEXTURE_2D, 0);
110 }
111 }
Jason Sams5dbfe932010-01-27 14:41:43 -0800112}*/
Jason Samsd19f10d2009-05-22 14:03:28 -0700113
114////////////////////////////////
115
116namespace android {
117namespace renderscript {
118
119
120void rsi_SamplerBegin(Context *rsc)
121{
122 SamplerState * ss = &rsc->mStateSampler;
123
124 ss->mMagFilter = RS_SAMPLER_LINEAR;
125 ss->mMinFilter = RS_SAMPLER_LINEAR;
126 ss->mWrapS = RS_SAMPLER_WRAP;
127 ss->mWrapT = RS_SAMPLER_WRAP;
128 ss->mWrapR = RS_SAMPLER_WRAP;
129}
130
131void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
132{
133 SamplerState * ss = &rsc->mStateSampler;
134
135 switch(param) {
136 case RS_SAMPLER_MAG_FILTER:
137 ss->mMagFilter = value;
138 break;
139 case RS_SAMPLER_MIN_FILTER:
140 ss->mMinFilter = value;
141 break;
142 case RS_SAMPLER_WRAP_S:
143 ss->mWrapS = value;
144 break;
145 case RS_SAMPLER_WRAP_T:
146 ss->mWrapT = value;
147 break;
148 case RS_SAMPLER_WRAP_R:
149 ss->mWrapR = value;
150 break;
151 }
152
153}
154
155RsSampler rsi_SamplerCreate(Context *rsc)
156{
157 SamplerState * ss = &rsc->mStateSampler;
158
159
Jason Samsa9e7a052009-09-25 14:51:22 -0700160 Sampler * s = new Sampler(rsc,
161 ss->mMagFilter,
Jason Sams7ce033d2009-08-18 14:14:24 -0700162 ss->mMinFilter,
163 ss->mWrapS,
Jason Samsd19f10d2009-05-22 14:03:28 -0700164 ss->mWrapT,
165 ss->mWrapR);
Jason Sams07ae4062009-08-27 20:23:34 -0700166 s->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700167 return s;
168}
169
Jason Sams02fb2cb2009-05-28 15:37:57 -0700170
Jason Samsd19f10d2009-05-22 14:03:28 -0700171}}