Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 18 | #include <GLES/gl.h> |
| 19 | #include <GLES/glext.h> |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | #include "rsContext.h" |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 21 | #else |
| 22 | #include "rsContextHostStub.h" |
| 23 | #include <OpenGL/gl.h> |
| 24 | #include <OpenGL/glext.h> |
| 25 | #endif //ANDROID_RS_BUILD_FOR_HOST |
| 26 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | #include "rsSampler.h" |
| 28 | |
Jason Sams | 1aa5a4e | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 29 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | using namespace android; |
| 31 | using namespace android::renderscript; |
| 32 | |
| 33 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 34 | Sampler::Sampler(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 35 | // Should not get called. |
| 36 | rsAssert(0); |
| 37 | } |
| 38 | |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 39 | Sampler::Sampler(Context *rsc, |
| 40 | RsSamplerValue magFilter, |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 41 | RsSamplerValue minFilter, |
| 42 | RsSamplerValue wrapS, |
| 43 | RsSamplerValue wrapT, |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 44 | RsSamplerValue wrapR, |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 45 | float aniso) : ObjectBase(rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 46 | mMagFilter = magFilter; |
| 47 | mMinFilter = minFilter; |
| 48 | mWrapS = wrapS; |
| 49 | mWrapT = wrapT; |
| 50 | mWrapR = wrapR; |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 51 | mAniso = aniso; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 54 | Sampler::~Sampler() { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 57 | void Sampler::setupGL(const Context *rsc, const Allocation *tex) { |
Jason Sams | 2f2898c | 2009-05-28 16:16:24 -0700 | [diff] [blame] | 58 | GLenum trans[] = { |
Jason Sams | 39c8bc7 | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 59 | GL_NEAREST, //RS_SAMPLER_NEAREST, |
| 60 | GL_LINEAR, //RS_SAMPLER_LINEAR, |
Jason Sams | 2f2898c | 2009-05-28 16:16:24 -0700 | [diff] [blame] | 61 | GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR, |
| 62 | GL_REPEAT, //RS_SAMPLER_WRAP, |
| 63 | GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP |
Jason Sams | 2f2898c | 2009-05-28 16:16:24 -0700 | [diff] [blame] | 64 | }; |
Jason Sams | 39c8bc7 | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 65 | |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 66 | 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 Sams | ef21edc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 73 | |
Jason Sams | 900f161 | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 74 | if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) { |
Alex Sakhartchouk | dc763f3 | 2010-10-27 14:10:07 -0700 | [diff] [blame] | 75 | if (tex->getHasGraphicsMipmaps() && rsc->ext_GL_NV_texture_npot_2D_mipmap()) { |
| 76 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]); |
| 77 | } else { |
| 78 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]); |
| 79 | } |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 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 Sams | ef21edc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 83 | } else { |
Jason Sams | 900f161 | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 84 | 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 Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 89 | 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 Sams | ef21edc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 92 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 93 | |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 94 | float anisoValue = rsMin(rsc->ext_texture_max_aniso(), mAniso); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 95 | if (rsc->ext_texture_max_aniso() > 1.0f) { |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 96 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue); |
| 97 | } |
| 98 | |
Jason Sams | 4c5f99e | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 99 | rsc->checkError("Sampler::setupGL2 tex env"); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 102 | void Sampler::bindToContext(SamplerState *ss, uint32_t slot) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 103 | ss->mSamplers[slot].set(this); |
| 104 | mBoundSlot = slot; |
| 105 | } |
| 106 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 107 | void Sampler::unbindFromContext(SamplerState *ss) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 108 | int32_t slot = mBoundSlot; |
| 109 | mBoundSlot = -1; |
| 110 | ss->mSamplers[slot].clear(); |
| 111 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 112 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 113 | void Sampler::serialize(OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 116 | Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 117 | return NULL; |
| 118 | } |
| 119 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 120 | //////////////////////////////// |
| 121 | |
| 122 | namespace android { |
| 123 | namespace renderscript { |
| 124 | |
| 125 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 126 | void rsi_SamplerBegin(Context *rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 127 | SamplerState * ss = &rsc->mStateSampler; |
| 128 | |
| 129 | ss->mMagFilter = RS_SAMPLER_LINEAR; |
| 130 | ss->mMinFilter = RS_SAMPLER_LINEAR; |
| 131 | ss->mWrapS = RS_SAMPLER_WRAP; |
| 132 | ss->mWrapT = RS_SAMPLER_WRAP; |
| 133 | ss->mWrapR = RS_SAMPLER_WRAP; |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 134 | ss->mAniso = 1.0f; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 137 | void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 138 | SamplerState * ss = &rsc->mStateSampler; |
| 139 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 140 | switch (param) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 141 | case RS_SAMPLER_MAG_FILTER: |
| 142 | ss->mMagFilter = value; |
| 143 | break; |
| 144 | case RS_SAMPLER_MIN_FILTER: |
| 145 | ss->mMinFilter = value; |
| 146 | break; |
| 147 | case RS_SAMPLER_WRAP_S: |
| 148 | ss->mWrapS = value; |
| 149 | break; |
| 150 | case RS_SAMPLER_WRAP_T: |
| 151 | ss->mWrapT = value; |
| 152 | break; |
| 153 | case RS_SAMPLER_WRAP_R: |
| 154 | ss->mWrapR = value; |
| 155 | break; |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 156 | default: |
| 157 | LOGE("Attempting to set invalid value on sampler"); |
| 158 | break; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 159 | } |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 160 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 161 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 162 | void rsi_SamplerSet2(Context *rsc, RsSamplerParam param, float value) { |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 163 | SamplerState * ss = &rsc->mStateSampler; |
| 164 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 165 | switch (param) { |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 166 | case RS_SAMPLER_ANISO: |
| 167 | ss->mAniso = value; |
| 168 | break; |
| 169 | default: |
| 170 | LOGE("Attempting to set invalid value on sampler"); |
| 171 | break; |
| 172 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame^] | 175 | RsSampler rsi_SamplerCreate(Context *rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 176 | SamplerState * ss = &rsc->mStateSampler; |
| 177 | |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 178 | Sampler * s = new Sampler(rsc, |
| 179 | ss->mMagFilter, |
Jason Sams | 707aaf3 | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 180 | ss->mMinFilter, |
| 181 | ss->mWrapS, |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 182 | ss->mWrapT, |
Alex Sakhartchouk | 1103d8e | 2010-09-30 11:36:37 -0700 | [diff] [blame] | 183 | ss->mWrapR, |
| 184 | ss->mAniso); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 185 | s->incUserRef(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 186 | return s; |
| 187 | } |
| 188 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 189 | }} |