blob: 54282a8dd8cbe0dbbbf272677190e08c6d90f44c [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
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080034Sampler::Sampler(Context *rsc) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070035 // Should not get called.
36 rsAssert(0);
37}
38
Jason Samse514b452009-09-25 14:51:22 -070039Sampler::Sampler(Context *rsc,
40 RsSamplerValue magFilter,
Jason Sams326e0dd2009-05-22 14:03:28 -070041 RsSamplerValue minFilter,
42 RsSamplerValue wrapS,
43 RsSamplerValue wrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070044 RsSamplerValue wrapR,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080045 float aniso) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070046 mMagFilter = magFilter;
47 mMinFilter = minFilter;
48 mWrapS = wrapS;
49 mWrapT = wrapT;
50 mWrapR = wrapR;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070051 mAniso = aniso;
Jason Sams326e0dd2009-05-22 14:03:28 -070052}
53
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080054Sampler::~Sampler() {
Jason Sams326e0dd2009-05-22 14:03:28 -070055}
56
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080057void Sampler::setupGL(const Context *rsc, const Allocation *tex) {
Jason Sams2f2898c2009-05-28 16:16:24 -070058 GLenum trans[] = {
Jason Sams39c8bc72009-05-28 15:37:57 -070059 GL_NEAREST, //RS_SAMPLER_NEAREST,
60 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams2f2898c2009-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 Sams2f2898c2009-05-28 16:16:24 -070064 };
Jason Sams39c8bc72009-05-28 15:37:57 -070065
Jason Sams4c5f99e2010-09-14 14:59:03 -070066 GLenum transNP[] = {
67 GL_NEAREST, //RS_SAMPLER_NEAREST,
68 GL_LINEAR, //RS_SAMPLER_LINEAR,
69 GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
70 GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
71 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
72 };
Jason Samsef21edc2010-02-22 15:37:51 -080073
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080074 // This tells us the correct texture type
75 GLenum target = (GLenum)tex->getGLTarget();
76
Jason Sams900f1612010-09-16 18:18:29 -070077 if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070078 if (tex->getHasGraphicsMipmaps() && rsc->ext_GL_NV_texture_npot_2D_mipmap()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080079 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070080 } else {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080081 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070082 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080083 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
84 glTexParameteri(target, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
85 glTexParameteri(target, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080086 } else {
Jason Sams900f1612010-09-16 18:18:29 -070087 if (tex->getHasGraphicsMipmaps()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080088 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
Jason Sams900f1612010-09-16 18:18:29 -070089 } else {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080090 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
Jason Sams900f1612010-09-16 18:18:29 -070091 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080092 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
93 glTexParameteri(target, GL_TEXTURE_WRAP_S, trans[mWrapS]);
94 glTexParameteri(target, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080095 }
Jason Sams326e0dd2009-05-22 14:03:28 -070096
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070097 float anisoValue = rsMin(rsc->ext_texture_max_aniso(), mAniso);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080098 if (rsc->ext_texture_max_aniso() > 1.0f) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080099 glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700100 }
101
Jason Sams4c5f99e2010-09-14 14:59:03 -0700102 rsc->checkError("Sampler::setupGL2 tex env");
Jason Sams326e0dd2009-05-22 14:03:28 -0700103}
104
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800105void Sampler::bindToContext(SamplerState *ss, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700106 ss->mSamplers[slot].set(this);
107 mBoundSlot = slot;
108}
109
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800110void Sampler::unbindFromContext(SamplerState *ss) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700111 int32_t slot = mBoundSlot;
112 mBoundSlot = -1;
113 ss->mSamplers[slot].clear();
114}
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700115
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800116void Sampler::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700117}
118
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800119Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700120 return NULL;
121}
122
Jason Sams326e0dd2009-05-22 14:03:28 -0700123////////////////////////////////
124
125namespace android {
126namespace renderscript {
127
128
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800129void rsi_SamplerBegin(Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700130 SamplerState * ss = &rsc->mStateSampler;
131
132 ss->mMagFilter = RS_SAMPLER_LINEAR;
133 ss->mMinFilter = RS_SAMPLER_LINEAR;
134 ss->mWrapS = RS_SAMPLER_WRAP;
135 ss->mWrapT = RS_SAMPLER_WRAP;
136 ss->mWrapR = RS_SAMPLER_WRAP;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700137 ss->mAniso = 1.0f;
Jason Sams326e0dd2009-05-22 14:03:28 -0700138}
139
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800140void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700141 SamplerState * ss = &rsc->mStateSampler;
142
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800143 switch (param) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700144 case RS_SAMPLER_MAG_FILTER:
145 ss->mMagFilter = value;
146 break;
147 case RS_SAMPLER_MIN_FILTER:
148 ss->mMinFilter = value;
149 break;
150 case RS_SAMPLER_WRAP_S:
151 ss->mWrapS = value;
152 break;
153 case RS_SAMPLER_WRAP_T:
154 ss->mWrapT = value;
155 break;
156 case RS_SAMPLER_WRAP_R:
157 ss->mWrapR = value;
158 break;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700159 default:
160 LOGE("Attempting to set invalid value on sampler");
161 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700162 }
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700163}
Jason Sams326e0dd2009-05-22 14:03:28 -0700164
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800165void rsi_SamplerSet2(Context *rsc, RsSamplerParam param, float value) {
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700166 SamplerState * ss = &rsc->mStateSampler;
167
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800168 switch (param) {
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700169 case RS_SAMPLER_ANISO:
170 ss->mAniso = value;
171 break;
172 default:
173 LOGE("Attempting to set invalid value on sampler");
174 break;
175 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700176}
177
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800178RsSampler rsi_SamplerCreate(Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700179 SamplerState * ss = &rsc->mStateSampler;
180
Jason Samse514b452009-09-25 14:51:22 -0700181 Sampler * s = new Sampler(rsc,
182 ss->mMagFilter,
Jason Sams707aaf32009-08-18 14:14:24 -0700183 ss->mMinFilter,
184 ss->mWrapS,
Jason Sams326e0dd2009-05-22 14:03:28 -0700185 ss->mWrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700186 ss->mWrapR,
187 ss->mAniso);
Jason Sams9397e302009-08-27 20:23:34 -0700188 s->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700189 return s;
190}
191
Jason Sams326e0dd2009-05-22 14:03:28 -0700192}}