blob: 1d0d27080cbf2a4e0c594f180a1c261b062d4365 [file] [log] [blame]
Jason Sams326e0dd2009-05-22 14:03:28 -07001/*
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
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070017#ifndef ANDROID_RS_BUILD_FOR_HOST
Jason Sams326e0dd2009-05-22 14:03:28 -070018#include <GLES/gl.h>
19#include <GLES/glext.h>
Jason Sams326e0dd2009-05-22 14:03:28 -070020#include "rsContext.h"
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -070021#else
22#include "rsContextHostStub.h"
23#include <OpenGL/gl.h>
24#include <OpenGL/glext.h>
25#endif //ANDROID_RS_BUILD_FOR_HOST
26
Jason Sams326e0dd2009-05-22 14:03:28 -070027#include "rsSampler.h"
28
Jason Sams1aa5a4e2009-06-22 17:15:15 -070029
Jason Sams326e0dd2009-05-22 14:03:28 -070030using namespace android;
31using namespace android::renderscript;
32
33
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080034Sampler::Sampler(Context *rsc) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070035 // Should not get called.
36 rsAssert(0);
37}
38
Jason Samse514b452009-09-25 14:51:22 -070039Sampler::Sampler(Context *rsc,
40 RsSamplerValue magFilter,
Jason Sams326e0dd2009-05-22 14:03:28 -070041 RsSamplerValue minFilter,
42 RsSamplerValue wrapS,
43 RsSamplerValue wrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070044 RsSamplerValue wrapR,
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080045 float aniso) : ObjectBase(rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -070046 mMagFilter = magFilter;
47 mMinFilter = minFilter;
48 mWrapS = wrapS;
49 mWrapT = wrapT;
50 mWrapR = wrapR;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070051 mAniso = aniso;
Jason Sams326e0dd2009-05-22 14:03:28 -070052}
53
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080054Sampler::~Sampler() {
Jason Sams326e0dd2009-05-22 14:03:28 -070055}
56
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080057void Sampler::setupGL(const Context *rsc, const Allocation *tex) {
Jason Sams2f2898c2009-05-28 16:16:24 -070058 GLenum trans[] = {
Jason Sams39c8bc72009-05-28 15:37:57 -070059 GL_NEAREST, //RS_SAMPLER_NEAREST,
60 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams2f2898c2009-05-28 16:16:24 -070061 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
62 GL_REPEAT, //RS_SAMPLER_WRAP,
63 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Jason Sams2f2898c2009-05-28 16:16:24 -070064 };
Jason Sams39c8bc72009-05-28 15:37:57 -070065
Jason Sams4c5f99e2010-09-14 14:59:03 -070066 GLenum transNP[] = {
67 GL_NEAREST, //RS_SAMPLER_NEAREST,
68 GL_LINEAR, //RS_SAMPLER_LINEAR,
69 GL_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
70 GL_CLAMP_TO_EDGE, //RS_SAMPLER_WRAP,
71 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
72 };
Jason Samsef21edc2010-02-22 15:37:51 -080073
Jason Sams900f1612010-09-16 18:18:29 -070074 if (!rsc->ext_OES_texture_npot() && tex->getType()->getIsNp2()) {
Alex Sakhartchoukdc763f32010-10-27 14:10:07 -070075 if (tex->getHasGraphicsMipmaps() && rsc->ext_GL_NV_texture_npot_2D_mipmap()) {
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
77 } else {
78 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
79 }
Jason Sams4c5f99e2010-09-14 14:59:03 -070080 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, transNP[mMagFilter]);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, transNP[mWrapS]);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, transNP[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080083 } else {
Jason Sams900f1612010-09-16 18:18:29 -070084 if (tex->getHasGraphicsMipmaps()) {
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
86 } else {
87 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, transNP[mMinFilter]);
88 }
Jason Sams4c5f99e2010-09-14 14:59:03 -070089 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Samsef21edc2010-02-22 15:37:51 -080092 }
Jason Sams326e0dd2009-05-22 14:03:28 -070093
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070094 float anisoValue = rsMin(rsc->ext_texture_max_aniso(), mAniso);
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -080095 if (rsc->ext_texture_max_aniso() > 1.0f) {
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -070096 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisoValue);
97 }
98
Jason Sams4c5f99e2010-09-14 14:59:03 -070099 rsc->checkError("Sampler::setupGL2 tex env");
Jason Sams326e0dd2009-05-22 14:03:28 -0700100}
101
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800102void Sampler::bindToContext(SamplerState *ss, uint32_t slot) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700103 ss->mSamplers[slot].set(this);
104 mBoundSlot = slot;
105}
106
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800107void Sampler::unbindFromContext(SamplerState *ss) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700108 int32_t slot = mBoundSlot;
109 mBoundSlot = -1;
110 ss->mSamplers[slot].clear();
111}
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700112
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800113void Sampler::serialize(OStream *stream) const {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700114}
115
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800116Sampler *Sampler::createFromStream(Context *rsc, IStream *stream) {
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700117 return NULL;
118}
119
Jason Sams326e0dd2009-05-22 14:03:28 -0700120////////////////////////////////
121
122namespace android {
123namespace renderscript {
124
125
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800126void rsi_SamplerBegin(Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700127 SamplerState * ss = &rsc->mStateSampler;
128
129 ss->mMagFilter = RS_SAMPLER_LINEAR;
130 ss->mMinFilter = RS_SAMPLER_LINEAR;
131 ss->mWrapS = RS_SAMPLER_WRAP;
132 ss->mWrapT = RS_SAMPLER_WRAP;
133 ss->mWrapR = RS_SAMPLER_WRAP;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700134 ss->mAniso = 1.0f;
Jason Sams326e0dd2009-05-22 14:03:28 -0700135}
136
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800137void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700138 SamplerState * ss = &rsc->mStateSampler;
139
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800140 switch (param) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700141 case RS_SAMPLER_MAG_FILTER:
142 ss->mMagFilter = value;
143 break;
144 case RS_SAMPLER_MIN_FILTER:
145 ss->mMinFilter = value;
146 break;
147 case RS_SAMPLER_WRAP_S:
148 ss->mWrapS = value;
149 break;
150 case RS_SAMPLER_WRAP_T:
151 ss->mWrapT = value;
152 break;
153 case RS_SAMPLER_WRAP_R:
154 ss->mWrapR = value;
155 break;
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700156 default:
157 LOGE("Attempting to set invalid value on sampler");
158 break;
Jason Sams326e0dd2009-05-22 14:03:28 -0700159 }
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700160}
Jason Sams326e0dd2009-05-22 14:03:28 -0700161
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800162void rsi_SamplerSet2(Context *rsc, RsSamplerParam param, float value) {
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700163 SamplerState * ss = &rsc->mStateSampler;
164
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800165 switch (param) {
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700166 case RS_SAMPLER_ANISO:
167 ss->mAniso = value;
168 break;
169 default:
170 LOGE("Attempting to set invalid value on sampler");
171 break;
172 }
Jason Sams326e0dd2009-05-22 14:03:28 -0700173}
174
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800175RsSampler rsi_SamplerCreate(Context *rsc) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700176 SamplerState * ss = &rsc->mStateSampler;
177
Jason Samse514b452009-09-25 14:51:22 -0700178 Sampler * s = new Sampler(rsc,
179 ss->mMagFilter,
Jason Sams707aaf32009-08-18 14:14:24 -0700180 ss->mMinFilter,
181 ss->mWrapS,
Jason Sams326e0dd2009-05-22 14:03:28 -0700182 ss->mWrapT,
Alex Sakhartchouk1103d8e2010-09-30 11:36:37 -0700183 ss->mWrapR,
184 ss->mAniso);
Jason Sams9397e302009-08-27 20:23:34 -0700185 s->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700186 return s;
187}
188
Jason Sams326e0dd2009-05-22 14:03:28 -0700189}}