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