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 | 2978bfc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 62 | void Sampler::setupGL(const Context *rsc, bool npot) |
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 | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 70 | |
Jason Sams | 243e3fb | 2009-05-28 16:16:24 -0700 | [diff] [blame] | 71 | }; |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 72 | |
Jason Sams | 2978bfc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 73 | bool forceNonMip = false; |
| 74 | if (!rsc->ext_OES_texture_npot() && npot) { |
| 75 | forceNonMip = true; |
| 76 | } |
| 77 | |
| 78 | if ((mMinFilter == RS_SAMPLER_LINEAR_MIP_LINEAR) && forceNonMip) { |
| 79 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 80 | } else { |
| 81 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]); |
| 82 | } |
Jason Sams | 243e3fb | 2009-05-28 16:16:24 -0700 | [diff] [blame] | 83 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]); |
| 84 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]); |
| 85 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 86 | |
Jason Sams | 2978bfc | 2010-02-22 15:37:51 -0800 | [diff] [blame] | 87 | |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 88 | rsc->checkError("ProgramFragment::setupGL2 tex env"); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void Sampler::bindToContext(SamplerState *ss, uint32_t slot) |
| 92 | { |
| 93 | ss->mSamplers[slot].set(this); |
| 94 | mBoundSlot = slot; |
| 95 | } |
| 96 | |
| 97 | void Sampler::unbindFromContext(SamplerState *ss) |
| 98 | { |
| 99 | int32_t slot = mBoundSlot; |
| 100 | mBoundSlot = -1; |
| 101 | ss->mSamplers[slot].clear(); |
| 102 | } |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 103 | |
| 104 | void Sampler::serialize(OStream *stream) const |
| 105 | { |
| 106 | |
| 107 | } |
| 108 | |
| 109 | Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) |
| 110 | { |
| 111 | return NULL; |
| 112 | } |
| 113 | |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 114 | /* |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 115 | void SamplerState::setupGL() |
| 116 | { |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 117 | for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 118 | Sampler *s = mSamplers[ct].get(); |
| 119 | if (s) { |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 120 | s->setupGL(rsc); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 121 | } else { |
| 122 | glBindTexture(GL_TEXTURE_2D, 0); |
| 123 | } |
| 124 | } |
Jason Sams | 5dbfe93 | 2010-01-27 14:41:43 -0800 | [diff] [blame] | 125 | }*/ |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 126 | |
| 127 | //////////////////////////////// |
| 128 | |
| 129 | namespace android { |
| 130 | namespace renderscript { |
| 131 | |
| 132 | |
| 133 | void rsi_SamplerBegin(Context *rsc) |
| 134 | { |
| 135 | SamplerState * ss = &rsc->mStateSampler; |
| 136 | |
| 137 | ss->mMagFilter = RS_SAMPLER_LINEAR; |
| 138 | ss->mMinFilter = RS_SAMPLER_LINEAR; |
| 139 | ss->mWrapS = RS_SAMPLER_WRAP; |
| 140 | ss->mWrapT = RS_SAMPLER_WRAP; |
| 141 | ss->mWrapR = RS_SAMPLER_WRAP; |
| 142 | } |
| 143 | |
| 144 | void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value) |
| 145 | { |
| 146 | SamplerState * ss = &rsc->mStateSampler; |
| 147 | |
| 148 | switch(param) { |
| 149 | case RS_SAMPLER_MAG_FILTER: |
| 150 | ss->mMagFilter = value; |
| 151 | break; |
| 152 | case RS_SAMPLER_MIN_FILTER: |
| 153 | ss->mMinFilter = value; |
| 154 | break; |
| 155 | case RS_SAMPLER_WRAP_S: |
| 156 | ss->mWrapS = value; |
| 157 | break; |
| 158 | case RS_SAMPLER_WRAP_T: |
| 159 | ss->mWrapT = value; |
| 160 | break; |
| 161 | case RS_SAMPLER_WRAP_R: |
| 162 | ss->mWrapR = value; |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | |
| 168 | RsSampler rsi_SamplerCreate(Context *rsc) |
| 169 | { |
| 170 | SamplerState * ss = &rsc->mStateSampler; |
| 171 | |
| 172 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 173 | Sampler * s = new Sampler(rsc, |
| 174 | ss->mMagFilter, |
Jason Sams | 7ce033d | 2009-08-18 14:14:24 -0700 | [diff] [blame] | 175 | ss->mMinFilter, |
| 176 | ss->mWrapS, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 177 | ss->mWrapT, |
| 178 | ss->mWrapR); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 179 | s->incUserRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 180 | return s; |
| 181 | } |
| 182 | |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame] | 183 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 184 | }} |