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