rsSampler.cpp revision 39c8bc7be5751ec52693d21abdf139c4dfd29a2c
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 27using namespace android; 28using namespace android::renderscript; 29 30 31Sampler::Sampler() 32{ 33 // Should not get called. 34 rsAssert(0); 35} 36 37Sampler::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 50Sampler::~Sampler() 51{ 52} 53 54void Sampler::setupGL() 55{ 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 66 //LOGE("setup gl"); 67 switch(mMagFilter) { 68 case RS_SAMPLER_ 69 } 70 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 78void Sampler::bindToContext(SamplerState *ss, uint32_t slot) 79{ 80 ss->mSamplers[slot].set(this); 81 mBoundSlot = slot; 82} 83 84void Sampler::unbindFromContext(SamplerState *ss) 85{ 86 int32_t slot = mBoundSlot; 87 mBoundSlot = -1; 88 ss->mSamplers[slot].clear(); 89} 90 91void SamplerState::setupGL() 92{ 93 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) { 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 105namespace android { 106namespace renderscript { 107 108 109void 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 120void 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 144RsSampler 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 157void rsi_SamplerDestroy(Context *rsc, RsSampler vs) 158{ 159 Sampler * s = static_cast<Sampler *>(vs); 160 s->decRef(); 161 162} 163 164 165}} 166