blob: c80aecca79b85c02e176f20a4c0056766569441d [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
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080064 GL_LINEAR_MIPMAP_NEAREST, //RS_SAMPLER_LINEAR_MIP_NEAREST
Jason Sams2f2898c2009-05-28 16:16:24 -070065 };
Jason Sams39c8bc72009-05-28 15:37:57 -070066
Jason Sams4c5f99e2010-09-14 14:59:03 -070067 GLenum transNP[] = {
68 GL_NEAREST, //RS_SAMPLER_NEAREST,
69 GL_LINEAR, //RS_SAMPLER_LINEAR,
70 GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
71 GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
72 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Alex Sakhartchouka2aab8b2010-12-15 09:59:58 -080073 GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_NEAREST,
Jason Sams4c5f99e2010-09-14 14:59:03 -070074 };
Jason Samsef21edc2010-02-22 15:37:51 -080075
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080076 // This tells us the correct texture type
77 GLenum target = (GLenum)tex->getGLTarget();
78
Jason Sams900f1612010-09-16 18:18:29 -070079 if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
Jason Sams0f7785c2011-01-13 17:02:35 -080080 if (tex->getHasGraphicsMipmaps() &&
81 (rsc->ext_GL_NV_texture_npot_2D_mipmap() || rsc->ext_GL_IMG_texture_npot())) {
82 if (rsc->ext_GL_NV_texture_npot_2D_mipmap()) {
83 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
84 } else {
85 switch (trans[mMinFilter]) {
86 case GL_LINEAR_MIPMAP_LINEAR:
87 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
88 break;
89 default:
90 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
91 break;
92 }
93 }
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070094 } else {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080095 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070096 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080097 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
98 glTexParameteri(target, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
99 glTexParameteri(target, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -0800100 } else {
Jason Sams900f1612010-09-16 18:18:29 -0700101 if (tex->getHasGraphicsMipmaps()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800102 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
Jason Sams900f1612010-09-16 18:18:29 -0700103 } else {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800104 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
Jason Sams900f1612010-09-16 18:18:29 -0700105 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800106 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
107 glTexParameteri(target, GL_TEXTURE_WRAP_S, trans[mWrapS]);
108 glTexParameteri(target, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -0800109 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700110
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700111 float anisoValue = rsMin(rsc->ext_texture_max_aniso(), mAniso);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800112 if (rsc->ext_texture_max_aniso() > 1.0f) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800113 glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700114 }
115
Jason Sams4c5f99e2010-09-14 14:59:03 -0700116 rsc->checkError("Sampler::setupGL2 tex env");
Jason Sams326e0dd2009-05-22 14:03:28 -0700117}
118
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800119void Sampler::bindToContext(SamplerState *ss, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700120 ss->mSamplers[slot].set(this);
121 mBoundSlot = slot;
122}
123
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800124void Sampler::unbindFromContext(SamplerState *ss) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700125 int32_t slot = mBoundSlot;
126 mBoundSlot = -1;
127 ss->mSamplers[slot].clear();
128}
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700129
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800130void Sampler::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700131}
132
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800133Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700134 return NULL;
135}
136
Jason Sams326e0dd2009-05-22 14:03:28 -0700137////////////////////////////////
138
139namespace android {
140namespace renderscript {
141
142
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800143void rsi_SamplerBegin(Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700144 SamplerState * ss = &rsc->mStateSampler;
145
146 ss->mMagFilter = RS_SAMPLER_LINEAR;
147 ss->mMinFilter = RS_SAMPLER_LINEAR;
148 ss->mWrapS = RS_SAMPLER_WRAP;
149 ss->mWrapT = RS_SAMPLER_WRAP;
150 ss->mWrapR = RS_SAMPLER_WRAP;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700151 ss->mAniso = 1.0f;
Jason Sams326e0dd2009-05-22 14:03:28 -0700152}
153
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800154void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700155 SamplerState * ss = &rsc->mStateSampler;
156
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800157 switch (param) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700158 case RS_SAMPLER_MAG_FILTER:
159 ss->mMagFilter = value;
160 break;
161 case RS_SAMPLER_MIN_FILTER:
162 ss->mMinFilter = value;
163 break;
164 case RS_SAMPLER_WRAP_S:
165 ss->mWrapS = value;
166 break;
167 case RS_SAMPLER_WRAP_T:
168 ss->mWrapT = value;
169 break;
170 case RS_SAMPLER_WRAP_R:
171 ss->mWrapR = value;
172 break;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700173 default:
174 LOGE("Attempting to set invalid value on sampler");
175 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700176 }
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700177}
Jason Sams326e0dd2009-05-22 14:03:28 -0700178
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800179void rsi_SamplerSet2(Context *rsc, RsSamplerParam param, float value) {
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700180 SamplerState * ss = &rsc->mStateSampler;
181
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800182 switch (param) {
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700183 case RS_SAMPLER_ANISO:
184 ss->mAniso = value;
185 break;
186 default:
187 LOGE("Attempting to set invalid value on sampler");
188 break;
189 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700190}
191
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800192RsSampler rsi_SamplerCreate(Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700193 SamplerState * ss = &rsc->mStateSampler;
194
Jason Samse514b452009-09-25 14:51:22 -0700195 Sampler * s = new Sampler(rsc,
196 ss->mMagFilter,
Jason Sams707aaf32009-08-18 14:14:24 -0700197 ss->mMinFilter,
198 ss->mWrapS,
Jason Sams326e0dd2009-05-22 14:03:28 -0700199 ss->mWrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700200 ss->mWrapR,
201 ss->mAniso);
Jason Sams9397e302009-08-27 20:23:34 -0700202 s->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700203 return s;
204}
205
Jason Sams326e0dd2009-05-22 14:03:28 -0700206}}