blob: cfae7b2abfc0258d05496b9fb639a9c26a460192 [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()) {
Jason Sams4c5f99e2010-09-14 14:59:03 -070079 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080083 } else {
Jason Sams900f1612010-09-16 18:18:29 -070084 if (tex->getHasGraphicsMipmaps()) {
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
86 } else {
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
88 }
Jason Sams4c5f99e2010-09-14 14:59:03 -070089 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080092 }
Jason Sams326e0dd2009-05-22 14:03:28 -070093
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070094 float anisoValue = rsMin(rsc->ext_texture_max_aniso(), mAniso);
95 if(rsc->ext_texture_max_aniso() > 1.0f) {
96 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
97 }
98
Jason Sams4c5f99e2010-09-14 14:59:03 -070099 rsc->checkError("Sampler::setupGL2 tex env");
Jason Sams326e0dd2009-05-22 14:03:28 -0700100}
101
102void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
103{
104 ss->mSamplers[slot].set(this);
105 mBoundSlot = slot;
106}
107
108void Sampler::unbindFromContext(SamplerState *ss)
109{
110 int32_t slot = mBoundSlot;
111 mBoundSlot = -1;
112 ss->mSamplers[slot].clear();
113}
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700114
115void Sampler::serialize(OStream *stream) const
116{
Jason Sams4c5f99e2010-09-14 14:59:03 -0700117
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700118}
119
120Sampler *Sampler::createFromStream(Context *rsc, IStream *stream)
121{
122 return NULL;
123}
124
Jason Sams3eb28f02010-01-27 14:41:43 -0800125/*
Jason Sams326e0dd2009-05-22 14:03:28 -0700126void SamplerState::setupGL()
127{
Jason Sams39c8bc72009-05-28 15:37:57 -0700128 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700129 Sampler *s = mSamplers[ct].get();
130 if (s) {
Jason Sams3eb28f02010-01-27 14:41:43 -0800131 s->setupGL(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700132 } else {
133 glBindTexture(GL_TEXTURE_2D, 0);
134 }
135 }
Jason Sams3eb28f02010-01-27 14:41:43 -0800136}*/
Jason Sams326e0dd2009-05-22 14:03:28 -0700137
138////////////////////////////////
139
140namespace android {
141namespace renderscript {
142
143
144void rsi_SamplerBegin(Context *rsc)
145{
146 SamplerState * ss = &rsc->mStateSampler;
147
148 ss->mMagFilter = RS_SAMPLER_LINEAR;
149 ss->mMinFilter = RS_SAMPLER_LINEAR;
150 ss->mWrapS = RS_SAMPLER_WRAP;
151 ss->mWrapT = RS_SAMPLER_WRAP;
152 ss->mWrapR = RS_SAMPLER_WRAP;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700153 ss->mAniso = 1.0f;
Jason Sams326e0dd2009-05-22 14:03:28 -0700154}
155
156void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
157{
158 SamplerState * ss = &rsc->mStateSampler;
159
160 switch(param) {
161 case RS_SAMPLER_MAG_FILTER:
162 ss->mMagFilter = value;
163 break;
164 case RS_SAMPLER_MIN_FILTER:
165 ss->mMinFilter = value;
166 break;
167 case RS_SAMPLER_WRAP_S:
168 ss->mWrapS = value;
169 break;
170 case RS_SAMPLER_WRAP_T:
171 ss->mWrapT = value;
172 break;
173 case RS_SAMPLER_WRAP_R:
174 ss->mWrapR = value;
175 break;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700176 default:
177 LOGE("Attempting to set invalid value on sampler");
178 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700179 }
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700180}
Jason Sams326e0dd2009-05-22 14:03:28 -0700181
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700182void rsi_SamplerSet2(Context *rsc, RsSamplerParam param, float value)
183{
184 SamplerState * ss = &rsc->mStateSampler;
185
186 switch(param) {
187 case RS_SAMPLER_ANISO:
188 ss->mAniso = value;
189 break;
190 default:
191 LOGE("Attempting to set invalid value on sampler");
192 break;
193 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700194}
195
196RsSampler rsi_SamplerCreate(Context *rsc)
197{
198 SamplerState * ss = &rsc->mStateSampler;
199
Jason Samse514b452009-09-25 14:51:22 -0700200 Sampler * s = new Sampler(rsc,
201 ss->mMagFilter,
Jason Sams707aaf32009-08-18 14:14:24 -0700202 ss->mMinFilter,
203 ss->mWrapS,
Jason Sams326e0dd2009-05-22 14:03:28 -0700204 ss->mWrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700205 ss->mWrapR,
206 ss->mAniso);
Jason Sams9397e302009-08-27 20:23:34 -0700207 s->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700208 return s;
209}
210
Jason Sams39c8bc72009-05-28 15:37:57 -0700211
Jason Sams326e0dd2009-05-22 14:03:28 -0700212}}