blob: f41f295bbbd84fa88f75dccf70450a4e936012f1 [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
Jason Samse514b452009-09-25 14:51:22 -070034Sampler::Sampler(Context *rsc) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070035{
Jason Samsf2649a92009-09-25 16:37:33 -070036 mAllocFile = __FILE__;
37 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070038 // Should not get called.
39 rsAssert(0);
40}
41
Jason Samse514b452009-09-25 14:51:22 -070042Sampler::Sampler(Context *rsc,
43 RsSamplerValue magFilter,
Jason Sams326e0dd2009-05-22 14:03:28 -070044 RsSamplerValue minFilter,
45 RsSamplerValue wrapS,
46 RsSamplerValue wrapT,
Jason Samse514b452009-09-25 14:51:22 -070047 RsSamplerValue wrapR) : ObjectBase(rsc)
Jason Sams326e0dd2009-05-22 14:03:28 -070048{
Jason Samsf2649a92009-09-25 16:37:33 -070049 mAllocFile = __FILE__;
50 mAllocLine = __LINE__;
Jason Sams326e0dd2009-05-22 14:03:28 -070051 mMagFilter = magFilter;
52 mMinFilter = minFilter;
53 mWrapS = wrapS;
54 mWrapT = wrapT;
55 mWrapR = wrapR;
56}
57
58Sampler::~Sampler()
59{
60}
61
Jason Samsef21edc2010-02-22 15:37:51 -080062void Sampler::setupGL(const Context *rsc, bool npot)
Jason Sams326e0dd2009-05-22 14:03:28 -070063{
Jason Sams2f2898c2009-05-28 16:16:24 -070064 GLenum trans[] = {
Jason Sams39c8bc72009-05-28 15:37:57 -070065 GL_NEAREST, //RS_SAMPLER_NEAREST,
66 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams2f2898c2009-05-28 16:16:24 -070067 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
68 GL_REPEAT, //RS_SAMPLER_WRAP,
69 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Jason Sams39c8bc72009-05-28 15:37:57 -070070
Jason Sams2f2898c2009-05-28 16:16:24 -070071 };
Jason Sams39c8bc72009-05-28 15:37:57 -070072
Jason Samsef21edc2010-02-22 15:37:51 -080073 bool forceNonMip = false;
74 if (!rsc->ext_OES_texture_npot() && npot) {
75 forceNonMip = true;
76 }
77
78 if ((mMinFilter == RS_SAMPLER_LINEAR_MIP_LINEAR) && forceNonMip) {
79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
80 } else {
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
82 }
Jason Sams2f2898c2009-05-28 16:16:24 -070083 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Sams326e0dd2009-05-22 14:03:28 -070086
Jason Samsef21edc2010-02-22 15:37:51 -080087
Jason Sams3eb28f02010-01-27 14:41:43 -080088 rsc->checkError("ProgramFragment::setupGL2 tex env");
Jason Sams326e0dd2009-05-22 14:03:28 -070089}
90
91void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
92{
93 ss->mSamplers[slot].set(this);
94 mBoundSlot = slot;
95}
96
97void Sampler::unbindFromContext(SamplerState *ss)
98{
99 int32_t slot = mBoundSlot;
100 mBoundSlot = -1;
101 ss->mSamplers[slot].clear();
102}
Alex Sakhartchoukfb6b6142010-05-21 12:53:13 -0700103
104void Sampler::serialize(OStream *stream) const
105{
106
107}
108
109Sampler *Sampler::createFromStream(Context *rsc, IStream *stream)
110{
111 return NULL;
112}
113
Jason Sams3eb28f02010-01-27 14:41:43 -0800114/*
Jason Sams326e0dd2009-05-22 14:03:28 -0700115void SamplerState::setupGL()
116{
Jason Sams39c8bc72009-05-28 15:37:57 -0700117 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -0700118 Sampler *s = mSamplers[ct].get();
119 if (s) {
Jason Sams3eb28f02010-01-27 14:41:43 -0800120 s->setupGL(rsc);
Jason Sams326e0dd2009-05-22 14:03:28 -0700121 } else {
122 glBindTexture(GL_TEXTURE_2D, 0);
123 }
124 }
Jason Sams3eb28f02010-01-27 14:41:43 -0800125}*/
Jason Sams326e0dd2009-05-22 14:03:28 -0700126
127////////////////////////////////
128
129namespace android {
130namespace renderscript {
131
132
133void rsi_SamplerBegin(Context *rsc)
134{
135 SamplerState * ss = &rsc->mStateSampler;
136
137 ss->mMagFilter = RS_SAMPLER_LINEAR;
138 ss->mMinFilter = RS_SAMPLER_LINEAR;
139 ss->mWrapS = RS_SAMPLER_WRAP;
140 ss->mWrapT = RS_SAMPLER_WRAP;
141 ss->mWrapR = RS_SAMPLER_WRAP;
142}
143
144void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
145{
146 SamplerState * ss = &rsc->mStateSampler;
147
148 switch(param) {
149 case RS_SAMPLER_MAG_FILTER:
150 ss->mMagFilter = value;
151 break;
152 case RS_SAMPLER_MIN_FILTER:
153 ss->mMinFilter = value;
154 break;
155 case RS_SAMPLER_WRAP_S:
156 ss->mWrapS = value;
157 break;
158 case RS_SAMPLER_WRAP_T:
159 ss->mWrapT = value;
160 break;
161 case RS_SAMPLER_WRAP_R:
162 ss->mWrapR = value;
163 break;
164 }
165
166}
167
168RsSampler rsi_SamplerCreate(Context *rsc)
169{
170 SamplerState * ss = &rsc->mStateSampler;
171
172
Jason Samse514b452009-09-25 14:51:22 -0700173 Sampler * s = new Sampler(rsc,
174 ss->mMagFilter,
Jason Sams707aaf32009-08-18 14:14:24 -0700175 ss->mMinFilter,
176 ss->mWrapS,
Jason Sams326e0dd2009-05-22 14:03:28 -0700177 ss->mWrapT,
178 ss->mWrapR);
Jason Sams9397e302009-08-27 20:23:34 -0700179 s->incUserRef();
Jason Sams326e0dd2009-05-22 14:03:28 -0700180 return s;
181}
182
Jason Sams39c8bc72009-05-28 15:37:57 -0700183
Jason Sams326e0dd2009-05-22 14:03:28 -0700184}}