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 | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame^] | 56 | GLenum translate[] = { |
| 57 | GL_NEAREST, //RS_SAMPLER_NEAREST, |
| 58 | GL_LINEAR, //RS_SAMPLER_LINEAR, |
| 59 | GL_LINEAR_MIP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR, |
| 60 | GL_WRAP, //RS_SAMPLER_WRAP, |
| 61 | GL_CLAMP_TO_EDGS, //RS_SAMPLER_CLAMP |
| 62 | |
| 63 | } |
| 64 | |
| 65 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 66 | //LOGE("setup gl"); |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame^] | 67 | switch(mMagFilter) { |
| 68 | case RS_SAMPLER_ |
| 69 | } |
| 70 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 71 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
| 72 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 73 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 74 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | void Sampler::bindToContext(SamplerState *ss, uint32_t slot) |
| 79 | { |
| 80 | ss->mSamplers[slot].set(this); |
| 81 | mBoundSlot = slot; |
| 82 | } |
| 83 | |
| 84 | void Sampler::unbindFromContext(SamplerState *ss) |
| 85 | { |
| 86 | int32_t slot = mBoundSlot; |
| 87 | mBoundSlot = -1; |
| 88 | ss->mSamplers[slot].clear(); |
| 89 | } |
| 90 | |
| 91 | void SamplerState::setupGL() |
| 92 | { |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame^] | 93 | for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 94 | Sampler *s = mSamplers[ct].get(); |
| 95 | if (s) { |
| 96 | s->setupGL(); |
| 97 | } else { |
| 98 | glBindTexture(GL_TEXTURE_2D, 0); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | //////////////////////////////// |
| 104 | |
| 105 | namespace android { |
| 106 | namespace renderscript { |
| 107 | |
| 108 | |
| 109 | void rsi_SamplerBegin(Context *rsc) |
| 110 | { |
| 111 | SamplerState * ss = &rsc->mStateSampler; |
| 112 | |
| 113 | ss->mMagFilter = RS_SAMPLER_LINEAR; |
| 114 | ss->mMinFilter = RS_SAMPLER_LINEAR; |
| 115 | ss->mWrapS = RS_SAMPLER_WRAP; |
| 116 | ss->mWrapT = RS_SAMPLER_WRAP; |
| 117 | ss->mWrapR = RS_SAMPLER_WRAP; |
| 118 | } |
| 119 | |
| 120 | void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value) |
| 121 | { |
| 122 | SamplerState * ss = &rsc->mStateSampler; |
| 123 | |
| 124 | switch(param) { |
| 125 | case RS_SAMPLER_MAG_FILTER: |
| 126 | ss->mMagFilter = value; |
| 127 | break; |
| 128 | case RS_SAMPLER_MIN_FILTER: |
| 129 | ss->mMinFilter = value; |
| 130 | break; |
| 131 | case RS_SAMPLER_WRAP_S: |
| 132 | ss->mWrapS = value; |
| 133 | break; |
| 134 | case RS_SAMPLER_WRAP_T: |
| 135 | ss->mWrapT = value; |
| 136 | break; |
| 137 | case RS_SAMPLER_WRAP_R: |
| 138 | ss->mWrapR = value; |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | RsSampler rsi_SamplerCreate(Context *rsc) |
| 145 | { |
| 146 | SamplerState * ss = &rsc->mStateSampler; |
| 147 | |
| 148 | |
| 149 | Sampler * s = new Sampler(ss->mMagFilter, |
| 150 | ss->mMinFilter, |
| 151 | ss->mWrapS, |
| 152 | ss->mWrapT, |
| 153 | ss->mWrapR); |
| 154 | return s; |
| 155 | } |
| 156 | |
Jason Sams | 02fb2cb | 2009-05-28 15:37:57 -0700 | [diff] [blame^] | 157 | void rsi_SamplerDestroy(Context *rsc, RsSampler vs) |
| 158 | { |
| 159 | Sampler * s = static_cast<Sampler *>(vs); |
| 160 | s->decRef(); |
| 161 | |
| 162 | } |
| 163 | |
| 164 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 165 | }} |