blob: cbdc4072fbfc97ca27690f8df0d884e36299c88b [file] [log] [blame]
Jason Sams326e0dd2009-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 Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070018#include <GLES/gl.h>
19#include <GLES/glext.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070020#include "rsContext.h"
Alex Sakhartchoukfb6b6142010-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 Sams326e0dd2009-05-22 14:03:28 -070027#include "rsSampler.h"
28
Jason Sams1aa5a4e2009-06-22 17:15:15 -070029
Jason Sams326e0dd2009-05-22 14:03:28 -070030using namespace android;
31using namespace android::renderscript;
32
33
Jason Samse514b452009-09-25 14:51:22 -070034Sampler::Sampler(Context *rsc) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070035{
36 // Should not get called.
37 rsAssert(0);
38}
39
Jason Samse514b452009-09-25 14:51:22 -070040Sampler::Sampler(Context *rsc,
41 RsSamplerValue magFilter,
Jason Sams326e0dd2009-05-22 14:03:28 -070042 RsSamplerValue minFilter,
43 RsSamplerValue wrapS,
44 RsSamplerValue wrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070045 RsSamplerValue wrapR,
46 float aniso) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070047{
48 mMagFilter = magFilter;
49 mMinFilter = minFilter;
50 mWrapS = wrapS;
51 mWrapT = wrapT;
52 mWrapR = wrapR;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070053 mAniso = aniso;
Jason Sams326e0dd2009-05-22 14:03:28 -070054}
55
56Sampler::~Sampler()
57{
58}
59
Jason Sams900f1612010-09-16 18:18:29 -070060void Sampler::setupGL(const Context *rsc, const Allocation *tex)
Jason Sams326e0dd2009-05-22 14:03:28 -070061{
Jason Sams2f2898c2009-05-28 16:16:24 -070062 GLenum trans[] = {
Jason Sams39c8bc72009-05-28 15:37:57 -070063 GL_NEAREST, //RS_SAMPLER_NEAREST,
64 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams2f2898c2009-05-28 16:16:24 -070065 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
66 GL_REPEAT, //RS_SAMPLER_WRAP,
67 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Jason Sams2f2898c2009-05-28 16:16:24 -070068 };
Jason Sams39c8bc72009-05-28 15:37:57 -070069
Jason Sams4c5f99e2010-09-14 14:59:03 -070070 GLenum transNP[] = {
71 GL_NEAREST, //RS_SAMPLER_NEAREST,
72 GL_LINEAR, //RS_SAMPLER_LINEAR,
73 GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
74 GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
75 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
76 };
Jason Samsef21edc2010-02-22 15:37:51 -080077
Jason Sams900f1612010-09-16 18:18:29 -070078 if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070079 if (tex->getHasGraphicsMipmaps() && rsc->ext_GL_NV_texture_npot_2D_mipmap()) {
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
81 } else {
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
83 }
Jason Sams4c5f99e2010-09-14 14:59:03 -070084 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080087 } else {
Jason Sams900f1612010-09-16 18:18:29 -070088 if (tex->getHasGraphicsMipmaps()) {
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
90 } else {
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
92 }
Jason Sams4c5f99e2010-09-14 14:59:03 -070093 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
94 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080096 }
Jason Sams326e0dd2009-05-22 14:03:28 -070097
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070098 float anisoValue = rsMin(rsc->ext_texture_max_aniso(), mAniso);
99 if(rsc->ext_texture_max_aniso() > 1.0f) {
100 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
101 }
102
Jason Sams4c5f99e2010-09-14 14:59:03 -0700103 rsc->checkError("Sampler::setupGL2 tex env");
Jason Sams326e0dd2009-05-22 14:03:28 -0700104}
105
106void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
107{
108 ss->mSamplers[slot].set(this);
109 mBoundSlot = slot;
110}
111
112void Sampler::unbindFromContext(SamplerState *ss)
113{
114 int32_t slot = mBoundSlot;
115 mBoundSlot = -1;
116 ss->mSamplers[slot].clear();
117}
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700118
119void Sampler::serialize(OStream *stream) const
120{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700121
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700122}
123
124Sampler *Sampler::createFromStream(Context *rsc, IStream *stream)
125{
126 return NULL;
127}
128
Jason Sams3eb28f02010-01-27 14:41:43 -0800129/*
Jason Sams326e0dd2009-05-22 14:03:28 -0700130void SamplerState::setupGL()
131{
Jason Sams39c8bc72009-05-28 15:37:57 -0700132 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700133 Sampler *s = mSamplers[ct].get();
134 if (s) {
Jason Sams3eb28f02010-01-27 14:41:43 -0800135 s->setupGL(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700136 } else {
137 glBindTexture(GL_TEXTURE_2D, 0);
138 }
139 }
Jason Sams3eb28f02010-01-27 14:41:43 -0800140}*/
Jason Sams326e0dd2009-05-22 14:03:28 -0700141
142////////////////////////////////
143
144namespace android {
145namespace renderscript {
146
147
148void rsi_SamplerBegin(Context *rsc)
149{
150 SamplerState * ss = &rsc->mStateSampler;
151
152 ss->mMagFilter = RS_SAMPLER_LINEAR;
153 ss->mMinFilter = RS_SAMPLER_LINEAR;
154 ss->mWrapS = RS_SAMPLER_WRAP;
155 ss->mWrapT = RS_SAMPLER_WRAP;
156 ss->mWrapR = RS_SAMPLER_WRAP;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700157 ss->mAniso = 1.0f;
Jason Sams326e0dd2009-05-22 14:03:28 -0700158}
159
160void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
161{
162 SamplerState * ss = &rsc->mStateSampler;
163
164 switch(param) {
165 case RS_SAMPLER_MAG_FILTER:
166 ss->mMagFilter = value;
167 break;
168 case RS_SAMPLER_MIN_FILTER:
169 ss->mMinFilter = value;
170 break;
171 case RS_SAMPLER_WRAP_S:
172 ss->mWrapS = value;
173 break;
174 case RS_SAMPLER_WRAP_T:
175 ss->mWrapT = value;
176 break;
177 case RS_SAMPLER_WRAP_R:
178 ss->mWrapR = value;
179 break;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700180 default:
181 LOGE("Attempting to set invalid value on sampler");
182 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700183 }
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700184}
Jason Sams326e0dd2009-05-22 14:03:28 -0700185
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700186void rsi_SamplerSet2(Context *rsc, RsSamplerParam param, float value)
187{
188 SamplerState * ss = &rsc->mStateSampler;
189
190 switch(param) {
191 case RS_SAMPLER_ANISO:
192 ss->mAniso = value;
193 break;
194 default:
195 LOGE("Attempting to set invalid value on sampler");
196 break;
197 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700198}
199
200RsSampler rsi_SamplerCreate(Context *rsc)
201{
202 SamplerState * ss = &rsc->mStateSampler;
203
Jason Samse514b452009-09-25 14:51:22 -0700204 Sampler * s = new Sampler(rsc,
205 ss->mMagFilter,
Jason Sams707aaf32009-08-18 14:14:24 -0700206 ss->mMinFilter,
207 ss->mWrapS,
Jason Sams326e0dd2009-05-22 14:03:28 -0700208 ss->mWrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700209 ss->mWrapR,
210 ss->mAniso);
Jason Sams9397e302009-08-27 20:23:34 -0700211 s->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700212 return s;
213}
214
Jason Sams39c8bc72009-05-28 15:37:57 -0700215
Jason Sams326e0dd2009-05-22 14:03:28 -0700216}}