Jason Sams | d19f10d | 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 | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_RS_BUILD_FOR_HOST |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 18 | #include <GLES/gl.h> |
| 19 | #include <GLES/glext.h> |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | #include "rsContext.h" |
Alex Sakhartchouk | aa7d288 | 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 | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 27 | #include "rsSampler.h" |
| 28 | |
Jason Sams | 4b962e5 | 2009-06-22 17:15:15 -0700 | [diff] [blame] | 29 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 30 | using namespace android; |
| 31 | using namespace android::renderscript; |
| 32 | |
| 33 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 34 | Sampler::Sampler(Context *rsc) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 35 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 36 | mAllocFile = __FILE__; |
| 37 | mAllocLine = __LINE__; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 38 | // Should not get called. |
| 39 | rsAssert(0); |
| 40 | } |
| 41 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 42 | Sampler::Sampler(Context *rsc, |
| 43 | RsSamplerValue magFilter, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 44 | RsSamplerValue minFilter, |
| 45 | RsSamplerValue wrapS, |
| 46 | RsSamplerValue wrapT, |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 47 | RsSamplerValue wrapR) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 48 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 49 | mAllocFile = __FILE__; |
| 50 | mAllocLine = __LINE__; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | mMagFilter = magFilter; |
| 52 | mMinFilter = minFilter; |
| 53 | mWrapS = wrapS; |
| 54 | mWrapT = wrapT; |
| 55 | mWrapR = wrapR; |
| 56 | } |
| 57 | |
| 58 | Sampler::~Sampler() |
| 59 | { |
| 60 | } |
| 61 | |
Jason Sams | d081fff | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 62 | void Sampler::setupGL(const Context *rsc, const Allocation *tex) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | { |
Jason Sams | 243e3fb | 2009-05-28 16:16:24 -0700 | [diff] [blame] | 64 | GLenum trans[] = { |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 65 | GL_NEAREST, //RS_SAMPLER_NEAREST, |
| 66 | GL_LINEAR, //RS_SAMPLER_LINEAR, |
Jason Sams | 243e3fb | 2009-05-28 16:16:24 -0700 | [diff] [blame] | 67 | GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR, |
| 68 | GL_REPEAT, //RS_SAMPLER_WRAP, |
| 69 | GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP |
Jason Sams | 243e3fb | 2009-05-28 16:16:24 -0700 | [diff] [blame] | 70 | }; |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 71 | |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 72 | GLenum transNP[] = { |
| 73 | GL_NEAREST, //RS_SAMPLER_NEAREST, |
| 74 | GL_LINEAR, //RS_SAMPLER_LINEAR, |
| 75 | GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR, |
| 76 | GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP, |
| 77 | GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP |
| 78 | }; |
Jason Sams | 2978bfc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 79 | |
Jason Sams | d081fff | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 80 | if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) { |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 81 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]); |
| 82 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]); |
| 83 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, transNP[mWrapS]); |
| 84 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, transNP[mWrapT]); |
Jason Sams | 2978bfc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 85 | } else { |
Jason Sams | d081fff | 2010-09-16 18:18:29 -0700 | [diff] [blame] | 86 | if (tex->getHasGraphicsMipmaps()) { |
| 87 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]); |
| 88 | } else { |
| 89 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]); |
| 90 | } |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 91 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]); |
| 92 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]); |
| 93 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]); |
Jason Sams | 2978bfc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 94 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 95 | |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 96 | rsc->checkError("Sampler::setupGL2 tex env"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void Sampler::bindToContext(SamplerState *ss, uint32_t slot) |
| 100 | { |
| 101 | ss->mSamplers[slot].set(this); |
| 102 | mBoundSlot = slot; |
| 103 | } |
| 104 | |
| 105 | void Sampler::unbindFromContext(SamplerState *ss) |
| 106 | { |
| 107 | int32_t slot = mBoundSlot; |
| 108 | mBoundSlot = -1; |
| 109 | ss->mSamplers[slot].clear(); |
| 110 | } |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 111 | |
| 112 | void Sampler::serialize(OStream *stream) const |
| 113 | { |
Jason Sams | cfc0436 | 2010-09-14 14:59:03 -0700 | [diff] [blame] | 114 | |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) |
| 118 | { |
| 119 | return NULL; |
| 120 | } |
| 121 | |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 122 | /* |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 123 | void SamplerState::setupGL() |
| 124 | { |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 125 | for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 126 | Sampler *s = mSamplers[ct].get(); |
| 127 | if (s) { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 128 | s->setupGL(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 129 | } else { |
| 130 | glBindTexture(GL_TEXTURE_2D, 0); |
| 131 | } |
| 132 | } |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 133 | }*/ |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 134 | |
| 135 | //////////////////////////////// |
| 136 | |
| 137 | namespace android { |
| 138 | namespace renderscript { |
| 139 | |
| 140 | |
| 141 | void rsi_SamplerBegin(Context *rsc) |
| 142 | { |
| 143 | SamplerState * ss = &rsc->mStateSampler; |
| 144 | |
| 145 | ss->mMagFilter = RS_SAMPLER_LINEAR; |
| 146 | ss->mMinFilter = RS_SAMPLER_LINEAR; |
| 147 | ss->mWrapS = RS_SAMPLER_WRAP; |
| 148 | ss->mWrapT = RS_SAMPLER_WRAP; |
| 149 | ss->mWrapR = RS_SAMPLER_WRAP; |
| 150 | } |
| 151 | |
| 152 | void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value) |
| 153 | { |
| 154 | SamplerState * ss = &rsc->mStateSampler; |
| 155 | |
| 156 | switch(param) { |
| 157 | case RS_SAMPLER_MAG_FILTER: |
| 158 | ss->mMagFilter = value; |
| 159 | break; |
| 160 | case RS_SAMPLER_MIN_FILTER: |
| 161 | ss->mMinFilter = value; |
| 162 | break; |
| 163 | case RS_SAMPLER_WRAP_S: |
| 164 | ss->mWrapS = value; |
| 165 | break; |
| 166 | case RS_SAMPLER_WRAP_T: |
| 167 | ss->mWrapT = value; |
| 168 | break; |
| 169 | case RS_SAMPLER_WRAP_R: |
| 170 | ss->mWrapR = value; |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | } |
| 175 | |
| 176 | RsSampler rsi_SamplerCreate(Context *rsc) |
| 177 | { |
| 178 | SamplerState * ss = &rsc->mStateSampler; |
| 179 | |
| 180 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 181 | Sampler * s = new Sampler(rsc, |
| 182 | ss->mMagFilter, |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 183 | ss->mMinFilter, |
| 184 | ss->mWrapS, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 185 | ss->mWrapT, |
| 186 | ss->mWrapR); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 187 | s->incUserRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 188 | return s; |
| 189 | } |
| 190 | |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 191 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 192 | }} |