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