blob: d89346e16cc98e7b10048a023d08dd707cc6954c [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
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{
Jason Sams2f2898c2009-05-28 16:16:24 -070056 GLenum trans[] = {
Jason Sams39c8bc72009-05-28 15:37:57 -070057 GL_NEAREST, //RS_SAMPLER_NEAREST,
58 GL_LINEAR, //RS_SAMPLER_LINEAR,
Jason Sams2f2898c2009-05-28 16:16:24 -070059 GL_LINEAR_MIPMAP_LINEAR, //RS_SAMPLER_LINEAR_MIP_LINEAR,
60 GL_REPEAT, //RS_SAMPLER_WRAP,
61 GL_CLAMP_TO_EDGE, //RS_SAMPLER_CLAMP
Jason Sams39c8bc72009-05-28 15:37:57 -070062
Jason Sams2f2898c2009-05-28 16:16:24 -070063 };
Jason Sams39c8bc72009-05-28 15:37:57 -070064
65
Jason Sams2f2898c2009-05-28 16:16:24 -070066 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, trans[mMinFilter]);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, trans[mMagFilter]);
68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, trans[mWrapS]);
69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, trans[mWrapT]);
Jason Sams326e0dd2009-05-22 14:03:28 -070070
71}
72
73void Sampler::bindToContext(SamplerState *ss, uint32_t slot)
74{
75 ss->mSamplers[slot].set(this);
76 mBoundSlot = slot;
77}
78
79void Sampler::unbindFromContext(SamplerState *ss)
80{
81 int32_t slot = mBoundSlot;
82 mBoundSlot = -1;
83 ss->mSamplers[slot].clear();
84}
85
86void SamplerState::setupGL()
87{
Jason Sams39c8bc72009-05-28 15:37:57 -070088 for (uint32_t ct=0; ct < RS_MAX_SAMPLER_SLOT; ct++) {
Jason Sams326e0dd2009-05-22 14:03:28 -070089 Sampler *s = mSamplers[ct].get();
90 if (s) {
91 s->setupGL();
92 } else {
93 glBindTexture(GL_TEXTURE_2D, 0);
94 }
95 }
96}
97
98////////////////////////////////
99
100namespace android {
101namespace renderscript {
102
103
104void rsi_SamplerBegin(Context *rsc)
105{
106 SamplerState * ss = &rsc->mStateSampler;
107
108 ss->mMagFilter = RS_SAMPLER_LINEAR;
109 ss->mMinFilter = RS_SAMPLER_LINEAR;
110 ss->mWrapS = RS_SAMPLER_WRAP;
111 ss->mWrapT = RS_SAMPLER_WRAP;
112 ss->mWrapR = RS_SAMPLER_WRAP;
113}
114
115void rsi_SamplerSet(Context *rsc, RsSamplerParam param, RsSamplerValue value)
116{
117 SamplerState * ss = &rsc->mStateSampler;
118
119 switch(param) {
120 case RS_SAMPLER_MAG_FILTER:
121 ss->mMagFilter = value;
122 break;
123 case RS_SAMPLER_MIN_FILTER:
124 ss->mMinFilter = value;
125 break;
126 case RS_SAMPLER_WRAP_S:
127 ss->mWrapS = value;
128 break;
129 case RS_SAMPLER_WRAP_T:
130 ss->mWrapT = value;
131 break;
132 case RS_SAMPLER_WRAP_R:
133 ss->mWrapR = value;
134 break;
135 }
136
137}
138
139RsSampler rsi_SamplerCreate(Context *rsc)
140{
141 SamplerState * ss = &rsc->mStateSampler;
142
143
144 Sampler * s = new Sampler(ss->mMagFilter,
145 ss->mMinFilter,
146 ss->mWrapS,
147 ss->mWrapT,
148 ss->mWrapR);
149 return s;
150}
151
Jason Sams39c8bc72009-05-28 15:37:57 -0700152void rsi_SamplerDestroy(Context *rsc, RsSampler vs)
153{
154 Sampler * s = static_cast<Sampler *>(vs);
155 s->decRef();
156
157}
158
159
Jason Sams326e0dd2009-05-22 14:03:28 -0700160}}