blob: e2757df10831111dac6624360cd9b6ee27768f30 [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()) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070080 if (tex->getHasGraphicsMipmaps() && rsc->ext_GL_NV_texture_npot_2D_mipmap()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080081 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070082 } else {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080083 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070084 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080085 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
86 glTexParameteri(target, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
87 glTexParameteri(target, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080088 } else {
Jason Sams900f1612010-09-16 18:18:29 -070089 if (tex->getHasGraphicsMipmaps()) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080090 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
Jason Sams900f1612010-09-16 18:18:29 -070091 } else {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080092 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
Jason Sams900f1612010-09-16 18:18:29 -070093 }
Alex Sakhartchouk84e40272010-11-18 15:22:43 -080094 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
95 glTexParameteri(target, GL_TEXTURE_WRAP_S, trans[mWrapS]);
96 glTexParameteri(target, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080097 }
Jason Sams326e0dd2009-05-22 14:03:28 -070098
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070099 float anisoValue = rsMin(rsc->ext_texture_max_aniso(), mAniso);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800100 if (rsc->ext_texture_max_aniso() > 1.0f) {
Alex Sakhartchouk84e40272010-11-18 15:22:43 -0800101 glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700102 }
103
Jason Sams4c5f99e2010-09-14 14:59:03 -0700104 rsc->checkError("Sampler::setupGL2 tex env");
Jason Sams326e0dd2009-05-22 14:03:28 -0700105}
106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800107void Sampler::bindToContext(SamplerState *ss, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700108 ss->mSamplers[slot].set(this);
109 mBoundSlot = slot;
110}
111
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800112void Sampler::unbindFromContext(SamplerState *ss) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700113 int32_t slot = mBoundSlot;
114 mBoundSlot = -1;
115 ss->mSamplers[slot].clear();
116}
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700117
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800118void Sampler::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700119}
120
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800121Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700122 return NULL;
123}
124
Jason Sams326e0dd2009-05-22 14:03:28 -0700125////////////////////////////////
126
127namespace android {
128namespace renderscript {
129
130
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800131void rsi_SamplerBegin(Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700132 SamplerState * ss = &rsc->mStateSampler;
133
134 ss->mMagFilter = RS_SAMPLER_LINEAR;
135 ss->mMinFilter = RS_SAMPLER_LINEAR;
136 ss->mWrapS = RS_SAMPLER_WRAP;
137 ss->mWrapT = RS_SAMPLER_WRAP;
138 ss->mWrapR = RS_SAMPLER_WRAP;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700139 ss->mAniso = 1.0f;
Jason Sams326e0dd2009-05-22 14:03:28 -0700140}
141
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800142void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700143 SamplerState * ss = &rsc->mStateSampler;
144
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800145 switch (param) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700146 case RS_SAMPLER_MAG_FILTER:
147 ss->mMagFilter = value;
148 break;
149 case RS_SAMPLER_MIN_FILTER:
150 ss->mMinFilter = value;
151 break;
152 case RS_SAMPLER_WRAP_S:
153 ss->mWrapS = value;
154 break;
155 case RS_SAMPLER_WRAP_T:
156 ss->mWrapT = value;
157 break;
158 case RS_SAMPLER_WRAP_R:
159 ss->mWrapR = value;
160 break;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700161 default:
162 LOGE("Attempting to set invalid value on sampler");
163 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700164 }
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700165}
Jason Sams326e0dd2009-05-22 14:03:28 -0700166
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800167void rsi_SamplerSet2(Context *rsc, RsSamplerParam param, float value) {
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700168 SamplerState * ss = &rsc->mStateSampler;
169
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800170 switch (param) {
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700171 case RS_SAMPLER_ANISO:
172 ss->mAniso = value;
173 break;
174 default:
175 LOGE("Attempting to set invalid value on sampler");
176 break;
177 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700178}
179
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800180RsSampler rsi_SamplerCreate(Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700181 SamplerState * ss = &rsc->mStateSampler;
182
Jason Samse514b452009-09-25 14:51:22 -0700183 Sampler * s = new Sampler(rsc,
184 ss->mMagFilter,
Jason Sams707aaf32009-08-18 14:14:24 -0700185 ss->mMinFilter,
186 ss->mWrapS,
Jason Sams326e0dd2009-05-22 14:03:28 -0700187 ss->mWrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700188 ss->mWrapR,
189 ss->mAniso);
Jason Sams9397e302009-08-27 20:23:34 -0700190 s->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700191 return s;
192}
193
Jason Sams326e0dd2009-05-22 14:03:28 -0700194}}